fix(app): toon API-fout bij opnieuw toewijzen shift in snackbar
Made-with: Cursor
This commit is contained in:
26
apps/app/src/lib/apiErrors.ts
Normal file
26
apps/app/src/lib/apiErrors.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { isAxiosError } from 'axios'
|
||||
|
||||
/**
|
||||
* Human-readable message from Laravel API validation / exception responses.
|
||||
*/
|
||||
export function getApiErrorMessage(error: unknown, fallback: string): string {
|
||||
if (!isAxiosError(error)) return fallback
|
||||
|
||||
const data = error.response?.data as Record<string, unknown> | undefined
|
||||
if (!data) return fallback
|
||||
|
||||
const errors = data.errors
|
||||
if (errors && typeof errors === 'object' && errors !== null && !Array.isArray(errors)) {
|
||||
for (const value of Object.values(errors as Record<string, unknown>)) {
|
||||
if (Array.isArray(value) && value.length > 0 && typeof value[0] === 'string')
|
||||
return value[0]
|
||||
|
||||
if (typeof value === 'string') return value
|
||||
}
|
||||
}
|
||||
|
||||
const message = data.message
|
||||
if (typeof message === 'string' && message.trim() !== '') return message
|
||||
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user