Urgent.News

What's breaking now, across hundreds of outlets.

Tech

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,…

Most API integrations currently handle errors by parsing the message contained within the response body. This approach can break if the message changes, even if the underlying error code remains the same. In contrast, Publora's API assigns stable error codes to each error that can be acted upon. These codes remain consistent even if the accompanying message changes.

The article suggests structuring error handling around these stable codes, which can appear in five different locations within an API response. A handler that only checks for the presence of an error message in the top-level response may miss errors that are nested within validation errors, warnings, or media results.

Key examples include:

1. Top-level codes on 4xx responses such as IDEMPOTENCY_KEY_CONFLICT, POST_NOT_EDITABLE, and plan limits.

2. Validation errors in validation.errors[].code when a post fails platform-specific rules during scheduling.

3. Warnings[].code for successful responses that contain additional information the API wants the caller to be aware of.

4. Per URL entries in mediaResults[] when attaching media by link.

5. Per connection entries in issues[] on analytics routes.

The article provides a concrete example of a validation error encountered when attempting to schedule an Instagram post without an image. The API returned an HTTP 400 response containing detailed validation error information, including the specific code (MEDIA_REQUIRED), platform (instagram), and suggested action (ATTACH_REQUIRED_MEDIA). This level of granularity allows developers to precisely identify and address the issue.

The article outlines three main categories for handling these error codes:

1. Action = 'fix-input': This covers all other validation errors where the issue lies with the input data provided by the caller. The system should inform the user of the problem and point them to the specific platform and field that needs to be corrected.

2. Action = 'retry': Certain error codes, such as IDEMPOTENCY_IN_FLIGHT, MEDIA_URL_RATE_LIMITED, MEDIA_UPLOAD_PENDING, and MEDIA_VALIDATION_PENDING, indicate issues that can be resolved by retrying the identical request after the specified time, respecting any Retry-After headers. This approach ensures that retries do not create duplicate records.

3. Action = 'reread-then-retry': Some error codes, like POST_GROUP_VERSION_CONFLICT and POST_PUBLISH_IN_PROGRESS, suggest that another user has modified the resource since the original request was made. In these cases, the system should fetch the updated resource using a GET request, update it accordingly, and then retry the original request.

The article emphasizes that 200 responses can still contain warnings[], which should be checked for existence before being parsed. For instance, if a scheduled time is adjusted due to clock skew, the API will return a SCHEDULED_TIME_COERCED warning alongside the successful response.

The full list of error codes, their locations within the response, retryability status, and recommended recovery actions is available in the API error code catalog. The author recommends keeping this catalog readily accessible for reference when implementing error handling logic in API integrations.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

The `template` Data Type in ThingsDB

ThingsDB has long supported template strings using backtick syntax ( `Hello {name}!` ). While convenient for inline string interpolation, template strings are evaluated immediately at runtime, which…

  • template data type supports template strings in ThingsDB
  • Templates validated, stored, and rendered efficiently
  • Rendered using .render() method with valid variable names

Next.js Protected Routes: Why a Session Cookie Check in proxy.ts Isn't Enough

I asked an AI assistant for a simple thing: a team dashboard in a Next.js 16 app (Better Auth + Drizzle + Postgres), visible only to signed-in users. It looked done.

  • Proxy.ts checks session cookie presence but doesn't verify authenticity
  • Fake cookie allows access to dashboard with user data
  • Implement session check closer to data access layer for security

A first-year SaaS discount is a price increase with a delayed invoice

A 50% off first year looks generous at checkout. On a $240 annual plan sold at $120, the customer has agreed to a $120 jump at renewal, often billed automatically.

  • First-year SaaS discount appears as price increase with delayed invoice
  • 80 customers buying discounted annual plan generate $9,600 in year-one cash
  • Additional 80 customers needed to offset discount and marketing costs

More from Monday, 30 November 2026 →