Error Responses

This comprehensive guide covers all error responses in the Blog API, including status codes, error formats, troubleshooting steps, and best practices for error handling.

Error Response Format

The API uses consistent JSON error responses with the following structure:

Standard Error Response

{
  "code": "ErrorCode",
  "message": "Human-readable error description"
}

Validation Error Response

{
  "code": "ValidationError",
  "errors": {
    "fieldName": {
      "type": "field",
      "value": "invalid-value",
      "msg": "Specific field error message",
      "path": "fieldName",
      "location": "body"
    }
  }
}

HTTP Status Codes

2xx Success Codes

Code
Meaning
Usage

200

OK

Successful GET, POST (non-creation), PUT requests

201

Created

Successful resource creation (POST)

204

No Content

Successful DELETE requests

4xx Client Error Codes

400 Bad Request

Usage: Invalid request data, validation failures, business logic violations

Common Error Codes:

  • ValidationError: Input validation failed

  • BadRequest: General bad request (e.g., already liked post)

Examples:

Validation Error:

Business Logic Error:

Troubleshooting:

  1. Validate all required fields are provided

  2. Check field formats (email, URLs, etc.)

  3. Verify field length constraints

  4. Review business logic constraints


401 Unauthorized

Usage: Authentication issues - missing, invalid, or expired tokens

Error Code: AuthenticationError

Common Scenarios:

  • Missing Authorization header

  • Invalid JWT token format

  • Expired access token

  • Invalid refresh token

Examples:

Missing Token:

Expired Token:

Invalid Token:

Troubleshooting:

  1. Ensure Authorization header is included: Authorization: Bearer <token>

  2. Check if access token has expired - try refreshing

  3. Verify token format is valid JWT

  4. Re-authenticate if refresh token is also expired

Code Example:


403 Forbidden

Usage: Authorization issues - insufficient permissions

Error Code: AuthorizationError

Common Scenarios:

  • User role insufficient for action (regular user trying admin operations)

  • Attempting to access/modify resources belonging to other users

  • Admin registration with non-whitelisted email

Examples:

Insufficient Role:

Admin Registration Denied:

Resource Access Denied:

Troubleshooting:

  1. Check user role requirements for the endpoint

  2. Verify user owns the resource they're trying to modify

  3. Confirm admin email is whitelisted for admin registration

  4. Review API documentation for required permissions


404 Not Found

Usage: Requested resource doesn't exist

Error Code: NotFound

Common Scenarios:

  • Blog post with given ID/slug doesn't exist

  • User with given ID doesn't exist

  • Comment with given ID doesn't exist

  • Invalid endpoint URL

Examples:

Resource Not Found:

User Not Found:

Troubleshooting:

  1. Verify the resource ID/slug is correct

  2. Check if resource was deleted

  3. Ensure endpoint URL is correct

  4. Confirm resource is accessible with current user permissions


413 Payload Too Large

Usage: Uploaded file exceeds size limits

Error Code: ValidationError

Scenarios:

  • Blog banner image larger than 2MB

  • Request body too large

Example:

Troubleshooting:

  1. Compress images before upload

  2. Check file size limits in documentation

  3. Use appropriate image formats (PNG, JPG, WebP)

  4. Consider image optimization tools


429 Too Many Requests

Usage: Rate limit exceeded

Headers: Includes rate limit information

  • RateLimit-Limit: Total requests allowed

  • RateLimit-Remaining: Requests remaining in current window

  • RateLimit-Reset: Seconds until limit resets

  • Retry-After: Seconds to wait before retrying

Example Response:

Troubleshooting:

  1. Implement request rate limiting in client

  2. Use exponential backoff for retries

  3. Check rate limit headers to understand limits

  4. Consider caching responses to reduce API calls


5xx Server Error Codes

500 Internal Server Error

Usage: Unexpected server errors

Error Code: ServerError

Example:

Troubleshooting:

  1. Retry the request after a brief delay

  2. Check API status page for known issues

  3. Contact support if error persists

  4. Implement proper error logging in your application

Error Handling Best Practices

Client-Side Error Handling

React Error Handling Example

Retry Logic with Exponential Backoff

Error Code Reference

Authentication Errors (AuthenticationError)

Message
Cause
Solution

"Access denied, no token provided"

Missing Authorization header

Add Bearer token to request

"Invalid token"

Malformed JWT

Check token format and generation

"Token has expired"

Expired access token

Refresh token or re-authenticate

"Invalid refresh token"

Expired/invalid refresh token

Re-authenticate user

Authorization Errors (AuthorizationError)

Message
Cause
Solution

"Access denied, insufficient permissions"

User role insufficient

Check role requirements

"You cannot register as an admin"

Non-whitelisted admin registration

Use whitelisted email or register as user

"You can only delete your own comments"

Trying to delete others' content

Verify resource ownership

Validation Errors (ValidationError)

Field
Common Messages
Solution

email

"Invalid email address"

Use valid email format

password

"Password must be at least 8 characters long"

Increase password length

title

"Title is required"

Provide blog title

content

"Content is required"

Provide blog content

banner_image

"File size must be less than 2MB"

Compress image file

Business Logic Errors (BadRequest)

Message
Cause
Solution

"You already liked this blog"

Duplicate like attempt

Check like status before liking

"Like not found"

Trying to unlike non-liked post

Verify post is liked

"Blog not found"

Invalid blog ID/slug

Check blog exists

Testing Error Scenarios

Monitoring and Logging

This comprehensive error handling guide ensures robust client-side error management and provides clear troubleshooting paths for common issues encountered when using the Blog API.

Last updated