All files / src/utils errors.ts

100% Statements 5/5
100% Branches 9/9
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51    48x                                                                                   109x 109x 14x   95x    
// Backend error codes that must never be shown to the user as-is — map each to
// an i18n key so callers with a translation function can localize it.
export const API_ERROR_CODE_TO_I18N_KEY: Record<string, string> = {
  INSUFFICIENT_CREDITS: 'credits.insufficientError',
  ENTITY_TOO_LARGE: 'entity.tooLarge',
  ITEM_TOO_LARGE: 'item.tooLarge',
  FLOW_SETTINGS_TOO_LARGE: 'flow.settingsTooLarge',
  TOO_MANY_FIELDS: 'entity.tooManyFields',
  TOO_MANY_FORMS: 'entity.tooManyForms',
  FIELD_NAME_TAKEN: 'entity.fieldNameTaken',
  REASON_TOO_LONG: 'admin.reasonTooLong',
  INVALID_SORT_FIELD: 'item.invalidSortField',
  INVALID_SORT_PARAM: 'item.invalidSortParam',
  INVALID_FILTER_FIELD: 'item.invalidFilterField',
  INVALID_FILTERS_PARAM: 'item.invalidFiltersParam',
  COMMENTS_DISABLED: 'comment.disabled',
  COMMENT_BLOCKED: 'comment.blocked',
  MODERATION_QUOTA_REQUIRED: 'moderation.quotaRequired',
  TASK_DECIDED: 'moderation.errors.decided',
  ALREADY_VOTED: 'moderation.errors.alreadyVoted',
  NOT_REVIEWABLE: 'moderation.errors.notReviewable',
  OWN_CONTENT: 'moderation.errors.ownContent',
  SLUG_TAKEN: 'spacePages.slugTaken',
  PAGE_MODIFIED: 'spacePages.pageModified',
  TOO_MANY_PAGES: 'spacePages.tooManyPages',
  PAGE_TOO_LARGE: 'spacePages.pageTooLarge',
  HOME_PAGE: 'spacePages.homePageKept',
  DATA_BLOCK_INVALID: 'spacePages.data.invalid',
  DATA_BLOCK_FORBIDDEN: 'spacePages.data.forbidden',
  ACCOUNT_RESTRICTED: 'report.restricted',
  REPORT_DECIDED: 'report.decided',
  CONTENT_NOT_REMOVED: 'report.notRemoved',
  REPORTING_SUSPENDED: 'report.reportingSuspended',
  NOT_WARNED: 'report.admin.reporting.notWarned',
  APPEAL_EXPIRED: 'appeal.expired',
  APPEAL_DECIDED: 'appeal.decided'
}
 
// Backend errors surface as response.data.error (our API's error code/message) or
// response.data.message (generic Express/validation errors); err.message covers
// network failures where no response was received at all. When a translation
// function is passed and the error code is a known one, it's localized instead
// of leaking the raw backend code to the UI.
export function getErrorMessage(err: any, fallback: string, t?: (key: string) => string): string {
  const code = err?.response?.data?.error
  if (t && code && API_ERROR_CODE_TO_I18N_KEY[code]) {
    return t(API_ERROR_CODE_TO_I18N_KEY[code])
  }
  return code || err?.response?.data?.message || err?.message || fallback
}