import { ResolvedErrorReason } from "./reason.mjs"; import { BadRequest, ErrorInfo } from "./detail.mjs"; import { DefaultNamespace, Namespace, TFunction } from "i18next"; //#region src/error/parse.d.ts declare function getErrorInfo(data: unknown): ErrorInfo | null; /** * @example For axios: * * const { t } = useTranslation('error'); * * const { message } = getErrorMessage(error.response.data, t); * toast.error(message); */ declare function getErrorMessage(data: unknown, t: TFunction): { reason: ResolvedErrorReason | null; message: string; }; /** * Whether an error body carries `ErrorInfo` with one of the given reasons. Meant for the cases a * component wants to handle itself (inline field error, retry, redirect) so a global error handler * can skip them. * * @example With a react-query MutationCache: * * const login = useMutation({ * mutationFn: api.auth.loginEmail, * meta: { handledReasons: ['INVALID_VERIFICATION_CODE'] }, * onError: (e) => { * if (isErrorReason(e.data, 'INVALID_VERIFICATION_CODE')) setError('code', { message }); * }, * }); * * new MutationCache({ * onError: (e, _v, _c, mutation) => { * if (isErrorReason(e.data, ...(mutation.meta?.handledReasons ?? []))) return; * toast.error(getErrorMessage(e.data, t).message); * }, * }); */ declare function isErrorReason(data: unknown, ...reasons: ResolvedErrorReason[]): boolean; /** * @example For react-hook-form: * * const { setError } = useForm(); * const fieldViolations = getFieldViolations(error.response.data); * fieldViolations.forEach((violation) => { * setError(violation.field, { message: violation.description }); * }); */ declare function getFieldViolations(data: unknown): BadRequest['fieldViolations']; //#endregion export { getErrorInfo, getErrorMessage, getFieldViolations, isErrorReason }; //# sourceMappingURL=parse.d.mts.map