Error Handling

The Entity Detector API uses standard HTTP status codes and returns detailed error messages to help you diagnose issues.

Error Response Format

When an error occurs, the API returns a JSON object with the following structure:

json
{
  "error": "Bad Request",
  "message": "Text must be between 200 and 30000 characters",
  "code": "INVALID_TEXT_LENGTH",
  "details": {
    "minLength": 200,
    "maxLength": 30000,
    "actualLength": 50
  }
}

Error Fields

FieldDescription
errorHTTP status text
messageHuman-readable error description
codeMachine-readable error code
detailsAdditional context (optional)

HTTP Status Codes

Success (2xx)

CodeMeaning
200 OKRequest successful

Client Errors (4xx)

CodeMeaningAction
400Bad RequestCheck request body and parameters
401UnauthorizedCheck API token
403ForbiddenToken lacks required permissions
404Not FoundCheck endpoint URL
429Too Many RequestsWait and retry, or upgrade plan

Server Errors (5xx)

CodeMeaningAction
500Internal Server ErrorRetry later, contact support if persists
502Bad GatewayTemporary issue, retry later
503Service UnavailableService under maintenance

Error Codes Reference

Validation Errors

CodeDescriptionSolution
INVALID_TEXT_LENGTHText too short or too longEnsure text is 200-30,000 chars
INVALID_LANGUAGEInvalid language codeUse ISO 639-1 codes (en, es, etc.)
MISSING_TEXTText field is requiredInclude text in request body
INVALID_JSONMalformed JSON in requestValidate JSON syntax

Authentication Errors

CodeDescriptionSolution
INVALID_TOKENToken is invalid or revokedGenerate a new token
MISSING_TOKENNo Authorization headerAdd Bearer token header
TOKEN_EXPIREDToken has expiredGenerate a new token

Rate Limit Errors

CodeDescriptionSolution
QUOTA_EXCEEDEDDaily quota exhaustedWait for reset or upgrade plan

Error Handling Example

Here's a comprehensive example of handling different error types:

javascript
async function analyzeText(text) {
  const response = await fetch('https://api.entitydetector.com/v1/analyze', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.API_KEY}`
    },
    body: JSON.stringify({ text })
  });

  if (!response.ok) {
    const error = await response.json();

    switch (response.status) {
      case 400:
        console.error('Invalid request:', error.message);
        // Handle validation errors
        break;
      case 401:
        console.error('Authentication failed:', error.message);
        // Check API token
        break;
      case 429:
        console.error('Rate limited:', error.message);
        // Wait and retry
        const retryAfter = error.retryAfter || 60;
        await new Promise(r => setTimeout(r, retryAfter * 1000));
        return analyzeText(text); // Retry
      case 500:
        console.error('Server error:', error.message);
        // Log and alert, maybe retry
        break;
      default:
        console.error('Unknown error:', error);
    }

    throw new Error(error.message);
  }

  return response.json();
}

Troubleshooting

Common Issues

Getting 401 Unauthorized?

  • • Verify the Authorization header format: Bearer YOUR_TOKEN
  • • Check for typos in your token
  • • Ensure the token hasn't been revoked
  • • Make sure there's no extra whitespace around the token

Getting 400 Bad Request?

  • • Check that Content-Type: application/json is set
  • • Validate your JSON syntax
  • • Ensure text is between 200 and 30,000 characters
  • • Check for encoding issues with special characters

Getting 429 Too Many Requests?

  • • Check the retryAfter field in the error response
  • • Implement exponential backoff in your retry logic
  • • Consider upgrading your plan
  • • Cache responses to reduce API calls

Still Having Issues?

If you're experiencing errors that you can't resolve, please contact support with:

  • The full error response
  • Your request (with sensitive data redacted)
  • Timestamp of when the error occurred