DEV Community

Cover image for Handle API errors by code, not by message
Eugeniya Ivanova for Publora

Posted on Edited on Originally published at docs.publora.com

Handle API errors by code, not by message

The first version of most API integrations handles errors by reading the message: if (body.error.includes('character limit')). It works until someone rewords the message.

The Publora API gives every error you can act on a stable code. Messages may change; codes don't. Here is how I'd structure error handling around them, with real responses.

Where codes show up

A code can sit in five places, and a handler that only checks the top level misses most of them:

  • top-level code on 4xx responses (IDEMPOTENCY_KEY_CONFLICT, POST_NOT_EDITABLE, plan limits);
  • validation.errors[].code when a post fails a platform's rules at scheduling time;
  • warnings[].code on a successful 200, for things the API accepted but you should know about;
  • per URL in mediaResults[], when you attach media by link;
  • per connection in issues, on the analytics routes.

A validation error, for real

I tried to schedule an Instagram post with no image. The API answered with HTTP 400:

{
  "error": "Validation failed",
  "validation": {
    "valid": false,
    "errors": [
      {
        "code": "MEDIA_REQUIRED",
        "message": "Instagram posts require media",
        "platform": "instagram",
        "field": "media",
        "severity": "error",
        "retryable": false,
        "nextAction": "ATTACH_REQUIRED_MEDIA"
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Every validation error names the platform and the field, so one request to five networks tells you exactly which network refused and why. When I sent 340 characters to Bluesky and an image to YouTube in one post, I got three entries back: CONTENT_TOO_LONG with currentValue: 340 and maxValue: 300, VIDEO_REQUIRED and IMAGES_NOT_SUPPORTED. Nothing was scheduled.

All validation codes are non-retryable with the same request. Retrying them in a loop just returns the same error.

Three buckets

Most handling comes down to sorting the code into one of three buckets:

type Action = 'fix-input' | 'retry' | 'reread-then-retry';

const RETRY = new Set(['IDEMPOTENCY_IN_FLIGHT', 'MEDIA_URL_RATE_LIMITED',
  'MEDIA_UPLOAD_PENDING', 'MEDIA_VALIDATION_PENDING']);
const REREAD = new Set(['POST_GROUP_VERSION_CONFLICT', 'POST_PUBLISH_IN_PROGRESS']);

export function classify(code: string): Action {
  if (RETRY.has(code)) return 'retry';
  if (REREAD.has(code)) return 'reread-then-retry';
  return 'fix-input';
}
Enter fullscreen mode Exit fullscreen mode
  • Retry the identical request, honouring Retry-After where the response sends it, and keep the same Idempotency-Key. That way a retry that races the original can't create a second post.
  • Re-read, then retry: someone else changed the post first. Fetch it with GET /get-post, rebase your edit, then send it again.
  • Fix the input: everything else. Show the user the message, and use platform and field to point at the exact thing to change.

Plan limits (POST_LIMIT_REACHED, SCHEDULE_HORIZON_REACHED and friends) land in "fix input" too: no retry will help until the account changes.

Don't ignore warnings

A 200 can still carry warnings[]. The array is omitted when empty, so check that it exists before reading it. For example, a scheduledTime a couple of minutes in the past (clock skew) is clamped to server time and comes back with SCHEDULED_TIME_COERCED. The post is fine, but your clock isn't.

The full list

Every code, where it appears, whether it's retryable and how to recover is in the API error code catalog. It's long, so I'd keep it open next to your handler rather than read it top to bottom.


Written with Claude.

Do you branch on error codes in your integrations, or still on messages? I'm curious how many APIs you work with actually give you stable codes.

Top comments (0)