mapCredentialUpdateError()
mapCredentialUpdateError maps a failure thrown by updateMeCredentials() onto the field that caused it, so a caller can attach the message to the right input instead of a form-level banner for everything.
Signature
mapCredentialUpdateError(error: unknown): CredentialUpdateErrorResult
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
error | unknown | ✅ | The value thrown by updateMeCredentials. |
Returns
CredentialUpdateErrorResult
| Property | Type | Description |
|---|---|---|
field | 'newPassword' | null | The field to attach the error to, or null for a form-level error. 'newPassword' only for a 400 (the new value failing a server-side check); every other status, 403 included, has no single field to blame here, since this form collects no current-value field for one to attach to. |
message | string (optional) | A server-supplied message, already human-readable. Shown as-is when present. |
messageKey | string | The i18n key to fall back to when message is absent. Always set, so a caller can resolve text with a plain message ?? t(messageKey). |
Usage
import { mapCredentialUpdateError, updateMeCredentials } from '@thunderid/browser'
try {
await updateMeCredentials({ baseUrl, payload: { password: newValue }, fetcher })
} catch (caughtError) {
const { field, message, messageKey } = mapCredentialUpdateError(caughtError)
const text = message ?? t(messageKey)
if (field) {
fieldErrors[field] = text // 'newPassword'
} else {
formError = text
}
}
Related
updateMeCredentials(): The call whose thrown error this function maps