diff --git a/Client/app/context/AuthContext.tsx b/Client/app/context/AuthContext.tsx index 74d4397..de13c28 100644 --- a/Client/app/context/AuthContext.tsx +++ b/Client/app/context/AuthContext.tsx @@ -2,15 +2,30 @@ import { createContext, useContext, useEffect, useState } from "react"; import { jwtDecode } from "jwt-decode"; import { API } from "~/api/api"; -export function getApiErrorMessage(error: any): string { - if (error && Array.isArray(error.errors) && error.errors.length > 0) { - return error.errors.map((e: any) => e.description).join('\n'); +export const getApiErrorMessage = (error: any): string => { + if (error && error.errors && typeof error.errors === 'object') { + // Handles ASP.NET Core validation problem details + // e.g., {"errors":{"Email":["The Email field is required."]}} + const messages = Object.values(error.errors).flat(); + if (messages.length > 0) { + return (messages as string[]).join(' '); + } } - if (error && error.detail) { - return error.detail; + + if (error && error.title) { + return error.title; } - return "An unexpected error occurred."; -} + + if (error && error.message) { + return error.message; + } + + if (typeof error === 'string') { + return error; + } + + return 'An unexpected error occurred.'; +}; export interface User { id: string;