asma-core-helpers 0.42.2 → 0.43.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/helpers/_processServerError.d.ts +4 -2
- package/lib/helpers/_processServerError.d.ts.map +1 -1
- package/lib/helpers/_processServerError.js +10 -3
- package/lib/helpers/_processServerError.js.map +1 -1
- package/lib/helpers/httpErrorMessage.d.ts +9 -0
- package/lib/helpers/httpErrorMessage.d.ts.map +1 -0
- package/lib/helpers/httpErrorMessage.js +121 -0
- package/lib/helpers/httpErrorMessage.js.map +1 -0
- package/lib/helpers/httpErrorMessages.d.ts +7 -0
- package/lib/helpers/httpErrorMessages.d.ts.map +1 -0
- package/lib/helpers/httpErrorMessages.js +45 -0
- package/lib/helpers/httpErrorMessages.js.map +1 -0
- package/lib/validation/index.d.ts +1 -1
- package/lib/validation/index.d.ts.map +1 -1
- package/lib/validation/index.js +1 -1
- package/lib/validation/index.js.map +1 -1
- package/lib/validation/messages.d.ts +136 -24
- package/lib/validation/messages.d.ts.map +1 -1
- package/lib/validation/messages.js +140 -23
- package/lib/validation/messages.js.map +1 -1
- package/package.json +1 -1
- package/src/helpers/_processServerError.ts +19 -3
- package/src/helpers/httpErrorMessage.test.ts +269 -0
- package/src/helpers/httpErrorMessage.ts +106 -0
- package/src/helpers/httpErrorMessages.ts +43 -0
- package/src/validation/index.ts +3 -0
- package/src/validation/messages.test.ts +60 -4
- package/src/validation/messages.ts +202 -25
|
@@ -1,39 +1,156 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Default NB/EN validation catalog for ASMA forms (ASMA-7729).
|
|
2
|
+
* Default NB/EN validation catalog for ASMA forms (ASMA-7729, ASMA-8073).
|
|
3
|
+
* EN copy follows Figma "Admin panel → Error messages" (node 11381:172081).
|
|
4
|
+
* Placeholders use `{name}` and are filled by `formatValidationMessage`.
|
|
3
5
|
* Apps override via shallow merge — translations stay in app i18n.
|
|
4
6
|
*/
|
|
5
7
|
export const validationMessages = {
|
|
6
8
|
nb: {
|
|
7
|
-
required: '
|
|
8
|
-
invalid_email: '
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
required: 'Obligatorisk',
|
|
10
|
+
invalid_email: 'Skriv inn en gyldig e-postadresse med domene.',
|
|
11
|
+
email_missing_at: 'Skriv inn en e-postadresse med @.',
|
|
12
|
+
invalid_name: 'Bruk bokstaver, mellomrom, bindestrek eller apostrof.',
|
|
13
|
+
invalid_phone: 'Skriv inn et gyldig telefonnummer, for eksempel +47 12 34 56 78.',
|
|
14
|
+
invalid_personal_number: 'Skriv inn et gyldig personnummer.',
|
|
15
|
+
personal_number_length: 'Skriv inn 11 sifre.',
|
|
16
|
+
personal_number_no_spaces: 'Skriv inn 11 sifre uten mellomrom.',
|
|
17
|
+
personal_number_birth_date: 'Skriv inn et gyldig personnummer.',
|
|
18
|
+
personal_number_enter: 'Skriv inn personnummeret ditt.',
|
|
19
|
+
duplicate_email: 'Skriv inn en annen e-postadresse.',
|
|
20
|
+
duplicate_journal_id: 'Skriv inn en annen journal-ID.',
|
|
21
|
+
duplicate_personal_number: 'Skriv inn et annet personnummer.',
|
|
22
|
+
duplicate_username: 'Skriv inn et annet brukernavn.',
|
|
23
|
+
duplicate_category: 'Skriv inn en annen kategori.',
|
|
24
|
+
min_length: 'Skriv inn minst {n} tegn.',
|
|
25
|
+
max_length: 'Skriv inn maks {n} tegn.',
|
|
26
|
+
length_between: 'Skriv inn mellom {min} og {max} tegn.',
|
|
27
|
+
digits_only: 'Skriv inn bare sifre.',
|
|
28
|
+
letters_numbers_only: 'Bruk bare bokstaver og tall.',
|
|
29
|
+
subdomain_chars: 'Bruk bare bokstaver, tall og bindestrek.',
|
|
30
|
+
invalid_subdomain: 'Skriv inn et gyldig underdomene.',
|
|
31
|
+
date_invalid_format: 'Skriv inn en gyldig dato i formatet DD.MM.ÅÅÅÅ.',
|
|
32
|
+
date_invalid_day: 'Skriv inn en gyldig dag.',
|
|
33
|
+
date_invalid_month: 'Skriv inn en gyldig måned.',
|
|
34
|
+
date_year_range: 'Skriv inn et år mellom {min} og {max}.',
|
|
35
|
+
date_invalid: 'Skriv inn en gyldig dato.',
|
|
36
|
+
date_after_start: 'Velg en dato på eller etter startdatoen.',
|
|
37
|
+
date_unavailable: 'Velg en annen dato.',
|
|
38
|
+
date_today_or_future: 'Velg dagens dato eller en senere dato.',
|
|
39
|
+
date_today_or_earlier: 'Velg dagens dato eller en tidligere dato.',
|
|
40
|
+
date_of_birth_invalid_format: 'Skriv inn en gyldig fødselsdato i formatet DD.MM.ÅÅÅÅ.',
|
|
41
|
+
date_of_birth_invalid: 'Skriv inn en gyldig fødselsdato.',
|
|
42
|
+
date_of_birth_not_future: 'Skriv inn en fødselsdato som er i dag eller tidligere.',
|
|
43
|
+
time_invalid_format: 'Skriv inn et gyldig klokkeslett i formatet TT:mm.',
|
|
44
|
+
time_after_start: 'Velg et klokkeslett på eller etter starttidspunktet.',
|
|
45
|
+
hour_invalid: 'Skriv inn en gyldig time.',
|
|
46
|
+
hour_range: 'Skriv inn en time mellom {min} og {max}.',
|
|
47
|
+
minutes_quarter: 'Skriv inn 0, 15, 30 eller 45 minutter.',
|
|
48
|
+
time_greater_than_zero: 'Skriv inn en tid som er større enn 0.',
|
|
49
|
+
select_at_least_one_recipient: 'Velg minst én mottaker.',
|
|
50
|
+
category_from_list: 'Velg en kategori fra listen.',
|
|
51
|
+
customer_required: 'Velg en kunde.',
|
|
52
|
+
patient_invalid: 'Velg en gyldig pasient.',
|
|
53
|
+
file_required: 'Velg en fil å laste opp.',
|
|
54
|
+
file_download_required: 'Velg en fil å laste ned.',
|
|
55
|
+
file_count_max: 'Velg opptil {n} filer.',
|
|
56
|
+
file_single: 'Velg én fil.',
|
|
57
|
+
file_empty: 'Denne filen er tom. Velg en annen fil.',
|
|
58
|
+
file_type: 'Velg en støttet filtype.',
|
|
59
|
+
file_media_type: 'Velg en støttet bilde- eller videofil.',
|
|
60
|
+
file_size_max: 'Velg en fil som ikke er større enn {size}.',
|
|
61
|
+
code_invalid: 'Skriv inn en gyldig kode.',
|
|
62
|
+
code_request_new: 'Be om en ny kode.',
|
|
63
|
+
code_wait: 'Vent {n} minutter før du ber om en ny kode.',
|
|
64
|
+
phone_not_linked: 'Skriv inn et telefonnummer som er knyttet til en konto.',
|
|
65
|
+
no_portal_access: 'Kontakt administratoren din for å få tilgang til denne portalen.',
|
|
66
|
+
sign_in_method_unavailable: 'Denne innloggingsmetoden er ikke tilgjengelig for organisasjonen din.',
|
|
67
|
+
credentials_invalid: 'Skriv inn riktig e-postadresse og passord.',
|
|
68
|
+
invitation_code_invalid: 'Skriv inn en gyldig invitasjonskode på 25 tegn.',
|
|
69
|
+
refresh_and_try_again: 'Last inn siden på nytt og prøv igjen.',
|
|
70
|
+
check_and_try_again: 'Sjekk svaret ditt og prøv igjen.',
|
|
71
|
+
reduce_data_and_try_again: 'Reduser mengden data og prøv igjen.',
|
|
72
|
+
try_again: 'Prøv igjen.',
|
|
18
73
|
},
|
|
19
74
|
en: {
|
|
20
|
-
required: '
|
|
21
|
-
invalid_email: '
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
75
|
+
required: 'Required',
|
|
76
|
+
invalid_email: 'Enter a valid email address with a domain.',
|
|
77
|
+
email_missing_at: 'Enter an email address with an @.',
|
|
78
|
+
invalid_name: 'Use letters, spaces, hyphens, or apostrophes.',
|
|
79
|
+
invalid_phone: 'Enter a valid phone number, like +47 12 34 56 78.',
|
|
80
|
+
invalid_personal_number: 'Enter a valid personal number.',
|
|
81
|
+
personal_number_length: 'Enter 11 digits.',
|
|
82
|
+
personal_number_no_spaces: 'Enter 11 digits without spaces.',
|
|
83
|
+
personal_number_birth_date: 'Enter a valid personal number.',
|
|
84
|
+
personal_number_enter: 'Enter your personal number.',
|
|
85
|
+
duplicate_email: 'Enter a different email address.',
|
|
86
|
+
duplicate_journal_id: 'Enter a different journal ID.',
|
|
87
|
+
duplicate_personal_number: 'Enter a different personal number.',
|
|
88
|
+
duplicate_username: 'Enter a different username.',
|
|
89
|
+
duplicate_category: 'Enter a different category.',
|
|
90
|
+
min_length: 'Enter at least {n} characters.',
|
|
91
|
+
max_length: 'Enter no more than {n} characters.',
|
|
92
|
+
length_between: 'Enter between {min} and {max} characters.',
|
|
93
|
+
digits_only: 'Enter digits only.',
|
|
94
|
+
letters_numbers_only: 'Use letters and numbers only.',
|
|
95
|
+
subdomain_chars: 'Use letters, numbers, and hyphens only.',
|
|
96
|
+
invalid_subdomain: 'Enter a valid subdomain.',
|
|
97
|
+
date_invalid_format: 'Enter a valid date in DD.MM.YYYY format.',
|
|
98
|
+
date_invalid_day: 'Enter a valid day.',
|
|
99
|
+
date_invalid_month: 'Enter a valid month.',
|
|
100
|
+
date_year_range: 'Enter a year between {min} and {max}.',
|
|
101
|
+
date_invalid: 'Enter a valid date.',
|
|
102
|
+
date_after_start: 'Select a date on or after the start date.',
|
|
103
|
+
date_unavailable: 'Select another date.',
|
|
104
|
+
date_today_or_future: 'Select today or a future date.',
|
|
105
|
+
date_today_or_earlier: 'Select today or an earlier date.',
|
|
106
|
+
date_of_birth_invalid_format: 'Enter a valid date of birth in DD.MM.YYYY format.',
|
|
107
|
+
date_of_birth_invalid: 'Enter a real date of birth.',
|
|
108
|
+
date_of_birth_not_future: "Enter a date of birth that's today or earlier.",
|
|
109
|
+
time_invalid_format: 'Enter a valid time in HH:mm format.',
|
|
110
|
+
time_after_start: 'Select a time at or after the start time.',
|
|
111
|
+
hour_invalid: 'Enter a valid hour.',
|
|
112
|
+
hour_range: 'Enter an hour between {min} and {max}.',
|
|
113
|
+
minutes_quarter: 'Enter 0, 15, 30, or 45 minutes.',
|
|
114
|
+
time_greater_than_zero: 'Enter a time greater than 0.',
|
|
115
|
+
select_at_least_one_recipient: 'Select at least one recipient.',
|
|
116
|
+
category_from_list: 'Select a category from the list.',
|
|
117
|
+
customer_required: 'Select a customer.',
|
|
118
|
+
patient_invalid: 'Select a valid patient.',
|
|
119
|
+
file_required: 'Select a file to upload.',
|
|
120
|
+
file_download_required: 'Select a file to download.',
|
|
121
|
+
file_count_max: 'Select up to {n} files.',
|
|
122
|
+
file_single: 'Select one file.',
|
|
123
|
+
file_empty: 'This file is empty. Select another file.',
|
|
124
|
+
file_type: 'Select a supported file type.',
|
|
125
|
+
file_media_type: 'Select a supported image or video file.',
|
|
126
|
+
file_size_max: 'Select a file no larger than {size}.',
|
|
127
|
+
code_invalid: 'Enter a valid code.',
|
|
128
|
+
code_request_new: 'Request a new code.',
|
|
129
|
+
code_wait: 'Wait {n} minutes before requesting a new code.',
|
|
130
|
+
phone_not_linked: 'Enter a phone number linked to an account.',
|
|
131
|
+
no_portal_access: 'Contact your administrator for access to this portal.',
|
|
132
|
+
sign_in_method_unavailable: "This sign-in method isn't available for your organization.",
|
|
133
|
+
credentials_invalid: 'Enter the correct email and password.',
|
|
134
|
+
invitation_code_invalid: 'Enter a valid 25-character invitation code.',
|
|
135
|
+
refresh_and_try_again: 'Refresh the page and try again.',
|
|
136
|
+
check_and_try_again: 'Check your answer and try again.',
|
|
137
|
+
reduce_data_and_try_again: 'Reduce the amount of data and try again.',
|
|
138
|
+
try_again: 'Try again.',
|
|
31
139
|
},
|
|
32
140
|
};
|
|
33
141
|
/** Shallow-merge overrides over the locale catalog (per-app i18n). */
|
|
34
142
|
export function getValidationMessages(locale, overrides) {
|
|
35
143
|
return { ...validationMessages[locale], ...overrides };
|
|
36
144
|
}
|
|
145
|
+
/** Fills `{name}` placeholders; unknown placeholders stay visible so a missing param is caught in review. */
|
|
146
|
+
export function formatValidationMessage(template, params) {
|
|
147
|
+
if (!params)
|
|
148
|
+
return template;
|
|
149
|
+
return template.replace(/\{(\w+)\}/g, (match, key) => (key in params ? String(params[key]) : match));
|
|
150
|
+
}
|
|
151
|
+
export function getValidationMessage(locale, key, params) {
|
|
152
|
+
return formatValidationMessage(validationMessages[locale][key], params);
|
|
153
|
+
}
|
|
37
154
|
/**
|
|
38
155
|
* REQ-003: actionable "what's wrong — how to fix" composition.
|
|
39
156
|
* e.g. errorWithHint('Invalid email', 'name@carasent.com') → 'Invalid email — name@carasent.com'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/validation/messages.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/validation/messages.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAwEH,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAC9B,EAAE,EAAE;QACA,QAAQ,EAAE,cAAc;QACxB,aAAa,EAAE,+CAA+C;QAC9D,gBAAgB,EAAE,mCAAmC;QACrD,YAAY,EAAE,uDAAuD;QACrE,aAAa,EAAE,kEAAkE;QACjF,uBAAuB,EAAE,mCAAmC;QAC5D,sBAAsB,EAAE,qBAAqB;QAC7C,yBAAyB,EAAE,oCAAoC;QAC/D,0BAA0B,EAAE,mCAAmC;QAC/D,qBAAqB,EAAE,gCAAgC;QACvD,eAAe,EAAE,mCAAmC;QACpD,oBAAoB,EAAE,gCAAgC;QACtD,yBAAyB,EAAE,kCAAkC;QAC7D,kBAAkB,EAAE,gCAAgC;QACpD,kBAAkB,EAAE,8BAA8B;QAClD,UAAU,EAAE,2BAA2B;QACvC,UAAU,EAAE,0BAA0B;QACtC,cAAc,EAAE,uCAAuC;QACvD,WAAW,EAAE,uBAAuB;QACpC,oBAAoB,EAAE,8BAA8B;QACpD,eAAe,EAAE,0CAA0C;QAC3D,iBAAiB,EAAE,kCAAkC;QACrD,mBAAmB,EAAE,iDAAiD;QACtE,gBAAgB,EAAE,0BAA0B;QAC5C,kBAAkB,EAAE,4BAA4B;QAChD,eAAe,EAAE,wCAAwC;QACzD,YAAY,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,CAAA;AAC/B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import { getHttpErrorMessage } from './httpErrorMessage.js'
|
|
2
|
+
import type { HttpErrorMessageOptions } from './httpErrorMessages.js'
|
|
3
|
+
|
|
4
|
+
export type { HttpErrorMessageOptions } from './httpErrorMessages.js'
|
|
5
|
+
|
|
6
|
+
export function _processServerError(
|
|
7
|
+
error: string | Record<string, any>,
|
|
8
|
+
options: HttpErrorMessageOptions = {},
|
|
9
|
+
): string {
|
|
2
10
|
console['error'](error)
|
|
3
11
|
|
|
4
|
-
|
|
12
|
+
const httpMessage = getHttpErrorMessage(error, options)
|
|
13
|
+
if (httpMessage !== undefined) return httpMessage
|
|
14
|
+
|
|
15
|
+
let errorMessage = _getServerErrorMessage(error, options)
|
|
5
16
|
|
|
6
17
|
/* if (i18n[errorMessage]) {
|
|
7
18
|
;(errorMessage = (window as any).strings)[errorMessage]
|
|
@@ -11,7 +22,12 @@ export function _processServerError(error: string | Record<string, any>): string
|
|
|
11
22
|
return errorMessage.slice(0, 250)
|
|
12
23
|
}
|
|
13
24
|
|
|
14
|
-
export function _getServerErrorMessage(
|
|
25
|
+
export function _getServerErrorMessage(
|
|
26
|
+
error: Record<string, any> | string,
|
|
27
|
+
options: HttpErrorMessageOptions = {},
|
|
28
|
+
): string {
|
|
29
|
+
const httpMessage = getHttpErrorMessage(error, options)
|
|
30
|
+
if (httpMessage !== undefined) return httpMessage
|
|
15
31
|
if (typeof error == 'string') {
|
|
16
32
|
return error
|
|
17
33
|
} else if (error['data'] && error['data']['Message']) {
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { _getServerErrorMessage, _processServerError } from './_processServerError.js'
|
|
5
|
+
|
|
6
|
+
function fetchError(data: unknown, status = 422): Record<string, unknown> {
|
|
7
|
+
return {
|
|
8
|
+
data,
|
|
9
|
+
response: new Response(null, { status }),
|
|
10
|
+
message: 'Editor request failed for /private with status 422',
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('HTTP error messages through the shared entry point', () => {
|
|
15
|
+
it('uses an explicit public message without depending on a client class', () => {
|
|
16
|
+
for (const name of ['EditorResponseError', 'BunjsOpenApiResponseError', 'AxiosError']) {
|
|
17
|
+
assert.equal(
|
|
18
|
+
_getServerErrorMessage({ ...fetchError({ publicMessage: 'Choose another title.' }), name }),
|
|
19
|
+
'Choose another title.',
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('keeps a one-word public message', () => {
|
|
25
|
+
assert.equal(_getServerErrorMessage(fetchError({ publicMessage: 'Expired' })), 'Expired')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('does not decide whether diagnostic text is public from its spelling', () => {
|
|
29
|
+
const result = _getServerErrorMessage(
|
|
30
|
+
fetchError({ message: 'duplicate key value violates unique constraint qnr_title_key' }, 400),
|
|
31
|
+
)
|
|
32
|
+
assert.doesNotMatch(result, /duplicate key|qnr_title_key|request failed/)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('reads an Axios response without mutating the original diagnostics', () => {
|
|
36
|
+
const data = { publicMessage: 'Select a context.', requestId: 'request-1' }
|
|
37
|
+
const error = { response: { status: 422, data }, message: 'Request failed with status code 422' }
|
|
38
|
+
assert.equal(_getServerErrorMessage(error), 'Select a context.')
|
|
39
|
+
assert.equal(error.response.data, data)
|
|
40
|
+
assert.equal(data.requestId, 'request-1')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('retains constraints for every field rather than checking a field whitelist', () => {
|
|
44
|
+
assert.equal(
|
|
45
|
+
_getServerErrorMessage(fetchError({ errors: { consent_title: ['Must contain at most 80 characters.'] } })),
|
|
46
|
+
'Consent title: Must contain at most 80 characters.',
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('allows field names that also happen to be protocol property names', () => {
|
|
51
|
+
assert.equal(
|
|
52
|
+
_getServerErrorMessage(fetchError({ errors: { message: ['Write a shorter message.'] } })),
|
|
53
|
+
'Message: Write a shorter message.',
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('retains all validation reasons and paths', () => {
|
|
58
|
+
assert.equal(
|
|
59
|
+
_getServerErrorMessage(
|
|
60
|
+
fetchError({
|
|
61
|
+
errors: {
|
|
62
|
+
title: ['Choose a different title.', 'Must contain at most 80 characters.'],
|
|
63
|
+
'questions.2.custom_field': ['Select an option.'],
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
),
|
|
67
|
+
'Title: Choose a different title.; Title: Must contain at most 80 characters.; Questions → 3 → Custom field: Select an option.',
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('reads the legacy Laravel field-map response', () => {
|
|
72
|
+
assert.equal(
|
|
73
|
+
_getServerErrorMessage(fetchError({ 'template.context': ['validation.required'] })),
|
|
74
|
+
'Template → Context: Required',
|
|
75
|
+
)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('reads structured validator issues without replacing their explanation', () => {
|
|
79
|
+
assert.equal(
|
|
80
|
+
_getServerErrorMessage(
|
|
81
|
+
fetchError(
|
|
82
|
+
{
|
|
83
|
+
issues: [
|
|
84
|
+
{
|
|
85
|
+
code: 'too_big',
|
|
86
|
+
path: ['template', 'title'],
|
|
87
|
+
message: 'Must contain at most 80 characters.',
|
|
88
|
+
maximum: 80,
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
400,
|
|
93
|
+
),
|
|
94
|
+
),
|
|
95
|
+
'Template → Title: Must contain at most 80 characters.',
|
|
96
|
+
)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('reads the existing serialized Hono/Zod response', () => {
|
|
100
|
+
assert.equal(
|
|
101
|
+
_getServerErrorMessage(
|
|
102
|
+
fetchError(
|
|
103
|
+
{
|
|
104
|
+
success: false,
|
|
105
|
+
error: {
|
|
106
|
+
name: 'ZodError',
|
|
107
|
+
message: JSON.stringify([{ path: ['new_field'], message: 'Choose a different value.' }]),
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
400,
|
|
111
|
+
),
|
|
112
|
+
),
|
|
113
|
+
'New field: Choose a different value.',
|
|
114
|
+
)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('preserves a root-level validation message', () => {
|
|
118
|
+
assert.equal(
|
|
119
|
+
_getServerErrorMessage(fetchError({ issues: [{ path: [], message: 'Select at least one recipient.' }] })),
|
|
120
|
+
'Select at least one recipient.',
|
|
121
|
+
)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('does not confuse a diagnostic array with field validation', () => {
|
|
125
|
+
assert.doesNotMatch(_getServerErrorMessage(fetchError({ error: ['SELECT secret FROM database'] })), /SELECT/)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
for (const data of [
|
|
129
|
+
null,
|
|
130
|
+
undefined,
|
|
131
|
+
'',
|
|
132
|
+
{ error: { name: 'ZodError', message: '{broken' } },
|
|
133
|
+
{ publicMessage: {} },
|
|
134
|
+
]) {
|
|
135
|
+
it('handles an unusable HTTP error body', () => {
|
|
136
|
+
assert.match(_getServerErrorMessage(fetchError(data)), /Check|check/)
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
it('keeps 5xx diagnostics out of the user message', () => {
|
|
141
|
+
assert.doesNotMatch(
|
|
142
|
+
_getServerErrorMessage(fetchError({ message: 'SQL failed', errors: { sql: ['private query'] } }, 500)),
|
|
143
|
+
/SQL|query/,
|
|
144
|
+
)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('honors an explicit private marker even when diagnostics resemble validation', () => {
|
|
148
|
+
assert.doesNotMatch(
|
|
149
|
+
_getServerErrorMessage(fetchError({ publicMessage: null, errors: { sql: ['private query'] } })),
|
|
150
|
+
/query|Sql/,
|
|
151
|
+
)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('handles direct error envelopes and unknown status codes', () => {
|
|
155
|
+
assert.equal(
|
|
156
|
+
_getServerErrorMessage({ statusCode: 409, publicMessage: 'This version was changed by someone else.' }),
|
|
157
|
+
'This version was changed by someone else.',
|
|
158
|
+
)
|
|
159
|
+
assert.doesNotMatch(_getServerErrorMessage(fetchError(null, 418)), /Missing field|request failed/)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('preserves the non-HTTP GraphQL and text compatibility paths', () => {
|
|
163
|
+
assert.equal(_getServerErrorMessage({ errors: [{ message: 'GraphQL explanation' }] }), 'GraphQL explanation')
|
|
164
|
+
assert.equal(_getServerErrorMessage('Existing message'), 'Existing message')
|
|
165
|
+
assert.equal(
|
|
166
|
+
_getServerErrorMessage({ message: "field 'deleted_at' not found" }),
|
|
167
|
+
"field 'deleted_at' not found",
|
|
168
|
+
)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it('preserves the older Axios public-response adapter', () => {
|
|
172
|
+
assert.equal(
|
|
173
|
+
_getServerErrorMessage({ response: { status: 422, data: { Message: 'Legacy public reason' } } }),
|
|
174
|
+
'Legacy public reason',
|
|
175
|
+
)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it('uses the selected interface language for fallback and required validation text', () => {
|
|
179
|
+
for (const locale of ['no', 'nb', 'nb-NO', 'NO']) {
|
|
180
|
+
assert.equal(
|
|
181
|
+
_getServerErrorMessage(fetchError(null), { locale }),
|
|
182
|
+
'Kontroller feltene i skjemaet og prøv igjen.',
|
|
183
|
+
)
|
|
184
|
+
assert.equal(
|
|
185
|
+
_getServerErrorMessage(fetchError({ errors: { title: ['validation.required'] } }), { locale }),
|
|
186
|
+
'Title: Obligatorisk',
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
assert.equal(
|
|
190
|
+
_getServerErrorMessage(fetchError(null, 401), { locale: 'no' }),
|
|
191
|
+
'Økten din er utløpt. Logg inn på nytt.',
|
|
192
|
+
)
|
|
193
|
+
assert.equal(
|
|
194
|
+
_getServerErrorMessage(fetchError(null), { locale: 'en-GB' }),
|
|
195
|
+
'Check the form fields and try again.',
|
|
196
|
+
)
|
|
197
|
+
assert.equal(
|
|
198
|
+
_getServerErrorMessage(fetchError({ publicMessage: 'Expired' }), { locale: 'no' }),
|
|
199
|
+
'Expired',
|
|
200
|
+
)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('keeps nested validation constraints from segment error envelopes', () => {
|
|
204
|
+
assert.equal(
|
|
205
|
+
_getServerErrorMessage(fetchError({
|
|
206
|
+
code: 'domain_validation',
|
|
207
|
+
message: 'Request validation failed.',
|
|
208
|
+
details: { issues: [{ path: ['title'], message: 'Must contain at most 80 characters.' }] },
|
|
209
|
+
}, 400)),
|
|
210
|
+
'Title: Must contain at most 80 characters.',
|
|
211
|
+
)
|
|
212
|
+
assert.doesNotMatch(
|
|
213
|
+
_getServerErrorMessage(fetchError({
|
|
214
|
+
publicMessage: null,
|
|
215
|
+
details: { issues: [{ path: ['sql'], message: 'private constraint' }] },
|
|
216
|
+
}, 400)),
|
|
217
|
+
/private|constraint/,
|
|
218
|
+
)
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('keeps complete HTTP explanations at the actual snackbar entry point', () => {
|
|
222
|
+
const publicMessage = 'Choose another title. '.repeat(20)
|
|
223
|
+
const result = _processServerError(fetchError({ publicMessage }))
|
|
224
|
+
assert.equal(result, publicMessage.trim())
|
|
225
|
+
assert.ok(result.length > 250)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('preserves legacy Axios text/array and HTTP GraphQL adapters', () => {
|
|
229
|
+
assert.equal(
|
|
230
|
+
_getServerErrorMessage({ response: { status: 403, data: 'You may not edit this.' } }),
|
|
231
|
+
'You may not edit this.',
|
|
232
|
+
)
|
|
233
|
+
assert.equal(
|
|
234
|
+
_getServerErrorMessage({ response: { status: 400, data: [{ message: 'First problem' }] } }),
|
|
235
|
+
'First problem',
|
|
236
|
+
)
|
|
237
|
+
assert.equal(
|
|
238
|
+
_getServerErrorMessage(fetchError({ Message: 'Legacy public reason' })),
|
|
239
|
+
'Legacy public reason',
|
|
240
|
+
)
|
|
241
|
+
assert.equal(
|
|
242
|
+
_getServerErrorMessage({
|
|
243
|
+
response: new Response(null, { status: 400 }),
|
|
244
|
+
graphQLErrors: [{ message: "field 'deleted_at' not found" }],
|
|
245
|
+
message: "[GraphQL] field 'deleted_at' not found",
|
|
246
|
+
}),
|
|
247
|
+
"[GraphQL] field 'deleted_at' not found",
|
|
248
|
+
)
|
|
249
|
+
assert.doesNotMatch(
|
|
250
|
+
_getServerErrorMessage({ response: { status: 500, data: 'SELECT private FROM database' } }),
|
|
251
|
+
/SELECT|private/,
|
|
252
|
+
)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('honors explicit public intent for 5xx and deduplicates validation reasons', () => {
|
|
256
|
+
assert.equal(
|
|
257
|
+
_getServerErrorMessage(fetchError({ publicMessage: ' Retry later. ' }, 503)),
|
|
258
|
+
'Retry later.',
|
|
259
|
+
)
|
|
260
|
+
assert.equal(
|
|
261
|
+
_getServerErrorMessage(fetchError({ errors: { title: ['Required', 'Required'] } }, 400)),
|
|
262
|
+
'Title: Required',
|
|
263
|
+
)
|
|
264
|
+
assert.equal(
|
|
265
|
+
_getServerErrorMessage(fetchError({ publicMessage: null }, 400)),
|
|
266
|
+
'Check the form fields and try again.',
|
|
267
|
+
)
|
|
268
|
+
})
|
|
269
|
+
})
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type ValidationLocale, validationMessages } from '../validation/messages.js'
|
|
2
|
+
|
|
3
|
+
import { httpErrorLocale, type HttpErrorMessageOptions, httpFallback } from './httpErrorMessages.js'
|
|
4
|
+
|
|
5
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
6
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function nonEmptyText(value: unknown): string | undefined {
|
|
10
|
+
return typeof value === 'string' && value.trim() ? value.trim() : undefined
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function fieldLabel(path: unknown): string {
|
|
14
|
+
const parts = Array.isArray(path) ? path : typeof path === 'string' ? path.split(/[.[\]]/).filter(Boolean) : []
|
|
15
|
+
return parts
|
|
16
|
+
.map((part: unknown) => {
|
|
17
|
+
if (typeof part === 'number' || (typeof part === 'string' && /^\d+$/.test(part))) {
|
|
18
|
+
return String(Number(part) + 1)
|
|
19
|
+
}
|
|
20
|
+
if (typeof part !== 'string') return ''
|
|
21
|
+
const words = part.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/[_-]+/g, ' ')
|
|
22
|
+
return words.charAt(0).toUpperCase() + words.slice(1)
|
|
23
|
+
})
|
|
24
|
+
.filter(Boolean)
|
|
25
|
+
.join(' → ')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function validationMessage(path: unknown, message: unknown, locale: ValidationLocale): string | undefined {
|
|
29
|
+
const reason = message === 'validation.required' ? validationMessages[locale].required : nonEmptyText(message)
|
|
30
|
+
if (!reason) return undefined
|
|
31
|
+
const label = fieldLabel(path)
|
|
32
|
+
return label ? `${label}: ${reason}` : reason
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function validatorIssues(body: Record<string, unknown>): unknown[] {
|
|
36
|
+
if (Array.isArray(body['issues'])) return body['issues']
|
|
37
|
+
if (Array.isArray(body['errors'])) return body['errors']
|
|
38
|
+
const details = body['details']
|
|
39
|
+
if (isRecord(details) && Array.isArray(details['issues'])) return details['issues']
|
|
40
|
+
const error = body['error']
|
|
41
|
+
if (!isRecord(error) || error['name'] !== 'ZodError') return []
|
|
42
|
+
if (Array.isArray(error['issues'])) return error['issues']
|
|
43
|
+
if (typeof error['message'] !== 'string') return []
|
|
44
|
+
try {
|
|
45
|
+
const issues: unknown = JSON.parse(error['message'])
|
|
46
|
+
return Array.isArray(issues) ? issues : []
|
|
47
|
+
} catch {
|
|
48
|
+
return []
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function validationMessagesFromBody(body: Record<string, unknown>, locale: ValidationLocale): string[] {
|
|
53
|
+
const messages = validatorIssues(body).flatMap((issue) => {
|
|
54
|
+
if (!isRecord(issue)) return []
|
|
55
|
+
const message = validationMessage(issue['path'], issue['message'], locale)
|
|
56
|
+
return message ? [message] : []
|
|
57
|
+
})
|
|
58
|
+
// Laravel field maps are a legacy public validation contract. Other arrays in an error
|
|
59
|
+
// envelope (for example `error: [...]`) are diagnostics, not field errors.
|
|
60
|
+
const fields = isRecord(body['errors']) ? body['errors'] : body
|
|
61
|
+
for (const [path, reasons] of Object.entries(fields)) {
|
|
62
|
+
if (fields === body && ['error', 'errors', 'issues', 'message', 'publicMessage'].includes(path)) continue
|
|
63
|
+
if (!Array.isArray(reasons) || !reasons.every((reason) => typeof reason === 'string')) continue
|
|
64
|
+
for (const reason of reasons) {
|
|
65
|
+
const message = validationMessage(path, reason, locale)
|
|
66
|
+
if (message) messages.push(message)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return [...new Set(messages)]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Read fetch/Axios failures by their response contract, independently of client error classes.
|
|
74
|
+
* `publicMessage` is explicitly displayable; `message` remains a diagnostic field. Legacy
|
|
75
|
+
* Laravel and Hono/Zod validation payloads keep their field paths and actual constraints.
|
|
76
|
+
* Undefined leaves non-HTTP errors to the existing GraphQL/text compatibility handling.
|
|
77
|
+
*/
|
|
78
|
+
export function getHttpErrorMessage(error: unknown, options: HttpErrorMessageOptions = {}): string | undefined {
|
|
79
|
+
if (!isRecord(error)) return undefined
|
|
80
|
+
const response = isRecord(error['response']) ? error['response'] : undefined
|
|
81
|
+
const status: unknown = response?.['status'] ?? error['statusCode']
|
|
82
|
+
if (typeof status !== 'number' || !Number.isInteger(status) || status < 400 || status > 599) return undefined
|
|
83
|
+
const locale = httpErrorLocale(options)
|
|
84
|
+
const body: unknown = response?.['data'] ?? error['data'] ?? error
|
|
85
|
+
if (Array.isArray(body) && (status === 400 || status === 422)) {
|
|
86
|
+
const validation = validationMessagesFromBody({ issues: body }, locale)
|
|
87
|
+
if (validation.length) return validation.join('; ')
|
|
88
|
+
}
|
|
89
|
+
if (isRecord(body)) {
|
|
90
|
+
const message = nonEmptyText(body['publicMessage'])
|
|
91
|
+
if (message) return message
|
|
92
|
+
if (Object.hasOwn(body, 'publicMessage')) return httpFallback(status, locale)
|
|
93
|
+
if (status < 500 && nonEmptyText(body['Message'])) return nonEmptyText(body['Message'])
|
|
94
|
+
if (status === 400 || status === 422) {
|
|
95
|
+
const validation = validationMessagesFromBody(body, locale)
|
|
96
|
+
if (validation.length) return validation.join('; ')
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// Preserve established legacy adapters (including text/array Axios responses and urql),
|
|
100
|
+
// while new private envelopes and all unmarked 5xx failures use a safe fallback.
|
|
101
|
+
if (status < 500 && (!isRecord(body) || !Object.hasOwn(body, 'statusCode'))) {
|
|
102
|
+
if (response?.['data']) return undefined
|
|
103
|
+
if (Array.isArray(error['graphQLErrors']) && error['graphQLErrors'].length) return undefined
|
|
104
|
+
}
|
|
105
|
+
return httpFallback(status, locale)
|
|
106
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ValidationLocale } from '../validation/messages.js'
|
|
2
|
+
|
|
3
|
+
export type HttpErrorMessageOptions = { locale?: string }
|
|
4
|
+
|
|
5
|
+
const httpErrorMessages = {
|
|
6
|
+
en: {
|
|
7
|
+
session: 'Your session has expired. Sign in again.',
|
|
8
|
+
permission: 'You do not have permission to do this.',
|
|
9
|
+
missing: 'This no longer exists. Reload the page and try again.',
|
|
10
|
+
conflict: 'This action conflicts with the current state. Reload the page and try again.',
|
|
11
|
+
tooLarge: 'The request is too large. Reduce its size and try again.',
|
|
12
|
+
rateLimit: 'Too many requests. Wait a little and try again.',
|
|
13
|
+
validation: 'Check the form fields and try again.',
|
|
14
|
+
unexpected: 'Something went wrong. Try again, and contact support if it keeps happening.',
|
|
15
|
+
},
|
|
16
|
+
nb: {
|
|
17
|
+
session: 'Økten din er utløpt. Logg inn på nytt.',
|
|
18
|
+
permission: 'Du har ikke tilgang til å gjøre dette.',
|
|
19
|
+
missing: 'Dette finnes ikke lenger. Last inn siden på nytt og prøv igjen.',
|
|
20
|
+
conflict: 'Handlingen er i konflikt med gjeldende status. Last inn siden på nytt og prøv igjen.',
|
|
21
|
+
tooLarge: 'Forespørselen er for stor. Reduser innholdet og prøv igjen.',
|
|
22
|
+
rateLimit: 'For mange forespørsler. Vent litt og prøv igjen.',
|
|
23
|
+
validation: 'Kontroller feltene i skjemaet og prøv igjen.',
|
|
24
|
+
unexpected: 'Noe gikk galt. Prøv igjen, og kontakt brukerstøtte hvis problemet fortsetter.',
|
|
25
|
+
},
|
|
26
|
+
} satisfies Record<ValidationLocale, Record<string, string>>
|
|
27
|
+
|
|
28
|
+
export function httpErrorLocale(options: HttpErrorMessageOptions): ValidationLocale {
|
|
29
|
+
const language = options.locale?.toLowerCase().split(/[-_]/)[0]
|
|
30
|
+
return language === 'no' || language === 'nb' ? 'nb' : 'en'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function httpFallback(status: number, locale: ValidationLocale): string {
|
|
34
|
+
const messages = httpErrorMessages[locale]
|
|
35
|
+
if (status === 401) return messages.session
|
|
36
|
+
if (status === 403) return messages.permission
|
|
37
|
+
if (status === 404) return messages.missing
|
|
38
|
+
if (status === 409) return messages.conflict
|
|
39
|
+
if (status === 413) return messages.tooLarge
|
|
40
|
+
if (status === 429) return messages.rateLimit
|
|
41
|
+
if (status === 400 || status === 422) return messages.validation
|
|
42
|
+
return messages.unexpected
|
|
43
|
+
}
|