xclade
GraphQL API

Error Handling

GraphQL error codes and handling

Error Handling

The GraphQL API uses standardized error codes for consistent error handling.

Error Response Format

{
  "errors": [
    {
      "message": "Human-readable error message",
      "extensions": {
        "code": "ERROR_CODE",
        "additionalInfo": "Optional additional context"
      },
      "path": ["field", "path", "to", "error"],
      "locations": [{ "line": 2, "column": 3 }]
    }
  ],
  "data": null
}

Common Error Codes

CodeDescriptionHTTP Equivalent
UNAUTHENTICATEDAuthentication required but not provided401
FORBIDDENUser lacks permission for the requested operation403
NOT_FOUNDRequested resource does not exist404
INVALID_DATARequest contains invalid data400
RATE_LIMIT_EXCEEDEDToo many requests in time window429
DEPTH_LIMIT_EXCEEDEDQuery nesting exceeds allowed depth400
COMPLEXITY_LIMIT_EXCEEDEDQuery complexity exceeds limit400
FIELD_LIMIT_EXCEEDEDToo many fields selected400
BATCH_LIMIT_EXCEEDEDToo many operations in batch400
QUERY_TOO_LARGEQuery string exceeds size limit413
CSRF_PROTECTION_FAILEDMissing required security header400
QUERY_TIMEOUTQuery execution exceeded time limit408
INTERNAL_SERVER_ERRORUnexpected server error500

Handling Errors in Client Code

async function executeQuery(query, variables) {
  try {
    const response = await fetch('/graphql', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`,
        'x-graphql-request': 'true',
      },
      body: JSON.stringify({ query, variables }),
    });
    
    const result = await response.json();
    
    if (result.errors) {
      // Handle GraphQL errors
      for (const error of result.errors) {
        const code = error.extensions?.code;
        
        switch (code) {
          case 'UNAUTHENTICATED':
            // Redirect to login
            window.location.href = '/login';
            break;
            
          case 'FORBIDDEN':
            // Show permission error
            showError('You do not have permission for this action');
            break;
            
          case 'RATE_LIMIT_EXCEEDED':
            const retryAfter = error.extensions.retryAfter;
            showError(`Rate limit exceeded. Please retry in ${retryAfter} seconds`);
            break;
            
          case 'NOT_FOUND':
            showError('The requested resource was not found');
            break;
            
          default:
            showError(error.message);
        }
      }
      return null;
    }
    
    return result.data;
  } catch (error) {
    // Handle network errors
    console.error('Network error:', error);
    showError('Unable to connect to the server');
    return null;
  }
}

On this page