File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -31,7 +31,21 @@ export async function apiRequest(path, options = {}) {
3131 const data = await response . json ( ) . catch ( ( ) => null )
3232
3333 if ( ! response . ok ) {
34- const message = data ?. error || data ?. detail || 'Request failed'
34+ let message = data ?. error || data ?. detail
35+
36+ // Handle DRF field-level validation errors, e.g.:
37+ // {"username": ["A user with that username already exists."]}
38+ if ( ! message && data && typeof data === 'object' ) {
39+ const firstErrorList = Object . values ( data ) . find (
40+ ( value ) => Array . isArray ( value ) && value . length > 0
41+ )
42+ const firstError = Object . values ( data ) . find (
43+ ( value ) => typeof value === 'string' && value
44+ )
45+ message = firstErrorList ?. [ 0 ] || firstError
46+ }
47+
48+ message = message || 'Request failed'
3549 throw new Error ( message )
3650 }
3751
You can’t perform that action at this time.
0 commit comments