import { readStringProperty } from '@admin/utils/error/read-string-property' import { isRecord } from '@admin/utils/shared/is-record' interface ErrorMessageMap { [code: string]: string } const POSTGRES_MESSAGES: ErrorMessageMap = { '23502': 'A required value is missing.', '23503': 'This is still in use somewhere else.', '23505': 'That value is already in use.' } const AUTH_MESSAGES: ErrorMessageMap = { INVALID_EMAIL_OR_PASSWORD: 'That email or password is incorrect.', PASSWORD_TOO_SHORT: 'That password is too short.', USER_ALREADY_EXISTS: 'That email is already in use.' } export function recognize(error: unknown): string | undefined { const authCode = readStringProperty(isRecord(error) ? error.body : undefined, 'code') if (authCode && AUTH_MESSAGES[authCode]) return AUTH_MESSAGES[authCode] const sqlState = readStringProperty(error, 'code') if (sqlState && POSTGRES_MESSAGES[sqlState]) return POSTGRES_MESSAGES[sqlState] return undefined }