Recipe

Map errors to user-facing messages

Users should never see a raw API error. Branch on the stable error.code, keep the developer-facing message in your logs, and show a clear, human sentence — pointing at the exact field when param is present.

2 min read

codeYour switch key
Log messageNot show it
paramPoint at the field

Switch on code

Maintain a small map from code to user copy.

const COPY = {
  consent_required: 'Please tick the consent box to continue.',
  invalid_email:    'That email address doesn’t look right.',
  rate_limited:     'You’re going a bit fast — try again in a moment.',
};
showUser(COPY[code] || 'Something went wrong. Please try again.');

Keep raw detail internal

Log status, code, param and message for yourself; the user gets your curated copy. The envelope is for you, not them.

Guide the fix

When param is present, highlight that field in your form so the user knows exactly what to correct.

Frequently asked questions

Why not surface error.message directly?

It targets developers and may be reworded. Map the stable code to your own tone-appropriate copy so wording changes never leak to users.

What about unknown codes?

Fall back to a generic message plus the retry class from the HTTP status. Log the unknown code so you can add copy for it later.

Funding for UK limited companies

Credicorp lends to your company, not to you personally — short-term working capital with no personal guarantee. See what your business could access.