Backend API
Error Handling
API error responses, status codes, and error handling patterns
Overview
The API uses standard HTTP status codes and consistent error response formats for all endpoints.
Error Response Format
All error responses follow this structure:
| Field | Type | Description |
|---|---|---|
message | string | Human-readable error description |
code | string | Machine-readable error code (optional) |
details | object | Additional error details (optional) |
HTTP Status Codes
Success Codes
| Code | Status | Description |
|---|---|---|
| 200 | OK | Request successful |
| 201 | Created | Resource created successfully |
| 204 | No Content | Request successful, no content returned |
Client Error Codes
| Code | Status | Description |
|---|---|---|
| 400 | Bad Request | Invalid request data |
| 401 | Unauthorized | Authentication required |
| 403 | Forbidden | Insufficient permissions |
| 404 | Not Found | Resource not found |
| 409 | Conflict | Resource already exists |
| 422 | Unprocessable Entity | Validation failed |
| 429 | Too Many Requests | Rate limit exceeded |
Server Error Codes
| Code | Status | Description |
|---|---|---|
| 500 | Internal Server Error | Unexpected server error |
| 502 | Bad Gateway | Upstream service error |
| 503 | Service Unavailable | Service temporarily unavailable |
Common Error Responses
400 Bad Request
Invalid or missing request data:
401 Unauthorized
Authentication required or invalid token:
403 Forbidden
Insufficient permissions:
404 Not Found
Resource not found:
409 Conflict
Resource already exists:
422 Unprocessable Entity
Validation errors with details:
429 Too Many Requests
Rate limit exceeded:
500 Internal Server Error
Unexpected server error:
Validation Errors
The API uses SecurityValidator from @hireable/shared for input validation:
Validation Rules
| Validator | Error Messages |
|---|---|
validateEmail | "Email is required", "Invalid email format", "Email is too long" |
validateFirstName | "First name is required", "First name must be at least 2 characters", "First name contains invalid characters" |
validateLastName | "Last name is required", "Last name must be at least 2 characters" |
validateCompany | "Company is required", "Company must be at least 2 characters" |
validatePassword | "Password is required", "Password must be at least 8 characters" |
validateAgree | "Agreement is required", "Agreement must be a boolean" |
Frontend Error Handling
API Client Error
useApi Hook Error Handling
Form Error Handling
Error Boundary
Use React Error Boundaries for unexpected errors:
Logging
Server-side errors are logged for debugging:
Best Practices
- Always return consistent error format - Use the standard error response structure
- Use appropriate status codes - Match the error type to the correct HTTP status
- Provide helpful messages - Error messages should help users understand what went wrong
- Don't expose internal details - Avoid leaking stack traces or internal errors to clients
- Log errors server-side - Log detailed errors for debugging while returning safe messages to clients
- Handle errors at the right level - Use try/catch at appropriate boundaries