16 lines
501 B
TypeScript
16 lines
501 B
TypeScript
import { ApiError, ApiErrorItem } from '../services/api.service';
|
|
|
|
export function getErrorMessage(err: ApiError, fallback: string): string {
|
|
const errors = err?.errors;
|
|
if (!errors) return fallback;
|
|
|
|
// Array format: [{ propertyName, errorMessage }]
|
|
if (Array.isArray(errors)) {
|
|
return errors[0]?.errorMessage ?? fallback;
|
|
}
|
|
|
|
// Dictionary format: { field: string[] }
|
|
const first = Object.values(errors).flat()[0];
|
|
return (typeof first === 'string' ? first : null) ?? fallback;
|
|
}
|