Polish UI, harden company creation, and add error pages
This commit is contained in:
@@ -2,6 +2,27 @@ import axios from "axios";
|
||||
import { getAuthToken } from "./auth";
|
||||
import { clearAuthToken } from "./auth";
|
||||
|
||||
export function getApiErrorMessage(error: any, fallback = "Request failed.") {
|
||||
const data = error?.response?.data;
|
||||
if (typeof data === "string" && data.trim()) return data.trim();
|
||||
if (typeof data?.message === "string" && data.message.trim()) return data.message.trim();
|
||||
if (Array.isArray(data?.errors)) {
|
||||
const first = data.errors.find((value: unknown) => typeof value === "string" && value.trim());
|
||||
if (first) return first;
|
||||
}
|
||||
if (data?.errors && typeof data.errors === "object") {
|
||||
for (const value of Object.values(data.errors)) {
|
||||
if (Array.isArray(value)) {
|
||||
const first = value.find((item: unknown) => typeof item === "string" && item.trim());
|
||||
if (first) return first;
|
||||
}
|
||||
if (typeof value === "string" && value.trim()) return value.trim();
|
||||
}
|
||||
}
|
||||
if (typeof error?.message === "string" && error.message.trim()) return error.message.trim();
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const envBaseUrl = process.env.REACT_APP_API_BASE_URL;
|
||||
const defaultBaseUrl =
|
||||
window.location.hostname === "localhost"
|
||||
|
||||
Reference in New Issue
Block a user