From d5cb1fe03c0a6cf543bc8710a5e8152c5e83f0b4 Mon Sep 17 00:00:00 2001 From: Sergey Smolnikov Date: Wed, 15 Jan 2025 16:20:34 +0100 Subject: [PATCH 1/2] Got missing error message --- src/store/utils/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/store/utils/index.ts b/src/store/utils/index.ts index bdd6fe837..42262b89e 100644 --- a/src/store/utils/index.ts +++ b/src/store/utils/index.ts @@ -30,10 +30,11 @@ export function getFetchErrorData(error: ManagedErrors): { if (IsRTKQueryError(error)) { const code = isFetchBaseQueryError(error) ? error.status : IsParsingError(error) ? error.originalStatus : undefined; + let errorMessage = error?.error??error?.data?.error; return { code: code, - error: isFetchBaseQueryError(error) ? "server error" : error.error, - message: "failed to fetch data: " + (code ? getReasonPhrase(code) : "unknown") + error: errorMessage, + message: "failed to fetch data: " + (code ? getReasonPhrase(code) : "unknown") + (errorMessage ? ". " + errorMessage : "") } } From 95fe2fcf1224aff5ea641f2bb4292c5a2dad3a7a Mon Sep 17 00:00:00 2001 From: Richard87 Date: Thu, 16 Jan 2025 09:05:23 +0100 Subject: [PATCH 2/2] add IsRadixHttpError and wrapped line on newlines in Alerts --- src/components/alert/style.css | 1 + .../create-application-form/index.tsx | 3 +- src/store/utils/index.ts | 32 +++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/components/alert/style.css b/src/components/alert/style.css index 5bf332649..91cb69327 100644 --- a/src/components/alert/style.css +++ b/src/components/alert/style.css @@ -3,6 +3,7 @@ display: inline-block; padding: var(--eds_spacing_medium); position: relative; + white-space: pre-line; } .alert.icon { display: grid; diff --git a/src/components/create-application-form/index.tsx b/src/components/create-application-form/index.tsx index 205bbe359..cdf815094 100644 --- a/src/components/create-application-form/index.tsx +++ b/src/components/create-application-form/index.tsx @@ -200,7 +200,8 @@ export default function CreateApplicationForm({ onCreated }: Props) { /> {creationState.isError && ( - Failed to create application.{' '} + Failed to create application. +
{getFetchErrorMessage(creationState.error)}
)} diff --git a/src/store/utils/index.ts b/src/store/utils/index.ts index 42262b89e..eb059fc0e 100644 --- a/src/store/utils/index.ts +++ b/src/store/utils/index.ts @@ -28,13 +28,20 @@ export function getFetchErrorData(error: ManagedErrors): { } } + if (IsRadixHttpError(error)) { + return { + code: error.status, + error: error.data.error ?? "unknown server error", + message: error.data.message + } + } + if (IsRTKQueryError(error)) { const code = isFetchBaseQueryError(error) ? error.status : IsParsingError(error) ? error.originalStatus : undefined; - let errorMessage = error?.error??error?.data?.error; return { code: code, - error: errorMessage, - message: "failed to fetch data: " + (code ? getReasonPhrase(code) : "unknown") + (errorMessage ? ". " + errorMessage : "") + error: isFetchBaseQueryError(error) ? "server error" : error.error, + message: "failed to fetch data: " + (code ? getReasonPhrase(code) : "unknown") } } @@ -60,6 +67,25 @@ export function getFetchErrorMessage( return getFetchErrorData(error).message; } +type RadixHttpError = { + status: number; + data: { + message: string + error?: string + type: "server"|"missing"|"user"|"forbidden" + } +} +function IsRadixHttpError(e: any): e is RadixHttpError { + if (typeof e !== "object" || e == null) return false; + + if (!("data" in e && "status" in e)) return false; + + if (typeof e.data !== "object" || e.data == null) return false; + + if ("error" in e.data && "message" in e.data && "type" in e.data) return true; + + return false; +} function IsRTKQueryError(e: any): e is FetchBaseQueryError { return isFetchBaseQueryError(e) || IsFetchError(e) || IsParsingError(e) || IsTimeoutError(e) || IsCustomError(e)