import type { TranslationKey } from "@/translations"; // A response that isn't ok. Anything else fetch throws is a network error. export class HttpError extends Error { status: number; constructor(status: number) { super(`HTTP ${status}`); this.name = "HttpError"; this.status = status; } } type AuthErrorKey = Extract; // Said instead of any request's own failure message: the user has to act before anything works. export const authErrorKey = (status?: number): AuthErrorKey | undefined => { if (status === 401) return "error_signed_out"; if (status === 403) return "error_email_not_verified"; return undefined; }; export const requestErrorKey = ( err: unknown, serverKey: K, networkKey: K, ): K | AuthErrorKey => { if (!(err instanceof HttpError)) return networkKey; return authErrorKey(err.status) ?? serverKey; };