@@ -12,7 +12,7 @@ type handleErrorPayload = {
1212 message ?: string
1313}
1414
15- function extractErrorMessage ( error : any ) : string | undefined {
15+ export function extractErrorMessage ( error : any ) : string | undefined {
1616 const raw = error ?. response ?. _data ?. message ?? error ?. data ?. message ?? error ?. cause ?. response ?. _data ?. message ?? error ?. message
1717 if ( Array . isArray ( raw ) ) {
1818 const joined = raw . map ( ( item ) => `${ item } ` . trim ( ) ) . filter ( Boolean ) . join ( ', ' )
@@ -25,6 +25,59 @@ function extractErrorMessage(error: any): string | undefined {
2525 return undefined
2626}
2727
28+ /**
29+ * Recupere la map `validations` d'une reponse API.
30+ *
31+ * Selon l'appelant (`$http` direct, `useHttp`/`useAsyncData`, erreur re-emise), la charge utile
32+ * est accessible via `response._data`, `data`, ou la meme chose sous `cause` : on teste les
33+ * quatre emplacements, comme le fait deja `extractErrorMessage`.
34+ */
35+ export function extractValidations ( error : any ) : Record < string , unknown > | undefined {
36+ const validations =
37+ error ?. response ?. _data ?. validations ??
38+ error ?. data ?. validations ??
39+ error ?. cause ?. response ?. _data ?. validations ??
40+ error ?. cause ?. data ?. validations
41+
42+ return validations && typeof validations === 'object' ? validations : undefined
43+ }
44+
45+ /**
46+ * Construit un message d'erreur lisible a partir d'une reponse API :
47+ * le `message` renvoye par l'API, complete des `validations` par champ quand elles existent.
48+ */
49+ export function formatApiErrorMessage ( error : any , fallback : string ) : string {
50+ const parts : string [ ] = [ ]
51+ const message = extractErrorMessage ( error )
52+ if ( message ) parts . push ( message )
53+
54+ const validations = extractValidations ( error )
55+ if ( validations ) {
56+ for ( const [ field , detail ] of Object . entries ( validations ) ) {
57+ if ( field === 'message' ) continue
58+ const text = typeof detail === 'string' ? detail : flattenValidationDetail ( detail )
59+ if ( text ) parts . push ( `${ field } : ${ text } ` )
60+ }
61+ }
62+
63+ return parts . length ? parts . join ( ' - ' ) : fallback
64+ }
65+
66+ function flattenValidationDetail ( detail : any ) : string {
67+ if ( typeof detail === 'string' ) return detail
68+ if ( Array . isArray ( detail ) ) return detail . map ( flattenValidationDetail ) . filter ( Boolean ) . join ( ', ' )
69+ if ( detail && typeof detail === 'object' ) {
70+ return Object . entries ( detail )
71+ . map ( ( [ key , value ] ) => {
72+ const text = flattenValidationDetail ( value )
73+ return text ? `${ key } : ${ text } ` : ''
74+ } )
75+ . filter ( Boolean )
76+ . join ( ', ' )
77+ }
78+ return ''
79+ }
80+
2881export function useErrorHandling ( ) : useErrorHandlingReturnType {
2982 function handleMfaRequiredIfNeeded ( error : any ) : boolean {
3083 const statusCode = error ?. response ?. _data ?. statusCode || error ?. data ?. statusCode
0 commit comments