Try to do a better job of handling aspnet errors
This commit is contained in:
@@ -2,15 +2,30 @@ import { createContext, useContext, useEffect, useState } from "react";
|
|||||||
import { jwtDecode } from "jwt-decode";
|
import { jwtDecode } from "jwt-decode";
|
||||||
import { API } from "~/api/api";
|
import { API } from "~/api/api";
|
||||||
|
|
||||||
export function getApiErrorMessage(error: any): string {
|
export const getApiErrorMessage = (error: any): string => {
|
||||||
if (error && Array.isArray(error.errors) && error.errors.length > 0) {
|
if (error && error.errors && typeof error.errors === 'object') {
|
||||||
return error.errors.map((e: any) => e.description).join('\n');
|
// 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 {
|
export interface User {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user