Files
MetacourseFrontend/src/app/utils/error.utils.ts
T
2026-05-28 13:23:53 +00:00

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;
}