From b641af43340f6efb1e10bab85f75a0500c04e630 Mon Sep 17 00:00:00 2001 From: Manuel Alessandro Collazo Date: Wed, 19 Apr 2023 10:05:45 +0200 Subject: [PATCH 1/2] fix: error checking Error received may be empty object which is truthy in JavaScript. Check for falsy values OR empty objects when deciding if an error is truthy. --- src/utils/error.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils/error.ts b/src/utils/error.ts index 527e02060..02536407b 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -1,9 +1,14 @@ import axios, { AxiosError } from "axios" export const getErrorMessage = ( - error?: Error | AxiosError | unknown + error?: Error | AxiosError | object | unknown ): string | undefined => { - if (!error) return + if ( + !error || + (Object.getPrototypeOf(error) === Object.prototype && + Object.keys(error as object).length === 0) + ) + return if (axios.isAxiosError(error)) return (error as AxiosError).response?.data?.message ?? error.message From a85bc8eebd337885e76195aba563c51b47203107 Mon Sep 17 00:00:00 2001 From: Manuel Alessandro Collazo Date: Mon, 24 Apr 2023 10:59:59 +0200 Subject: [PATCH 2/2] fix: Wrong error check --- src/components/feedback/Fetching.tsx | 4 ++-- src/utils/error.ts | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/components/feedback/Fetching.tsx b/src/components/feedback/Fetching.tsx index 5543dd63b..d1ee60805 100644 --- a/src/components/feedback/Fetching.tsx +++ b/src/components/feedback/Fetching.tsx @@ -1,6 +1,6 @@ import { PropsWithChildren, ReactNode, useState } from "react" import LinearProgress from "@mui/material/LinearProgress" -import { getErrorMessage } from "utils/error" +import { getErrorMessage, isError } from "utils/error" import useTimeout from "utils/hooks/useTimeout" import { Card } from "../layout" import Wrong from "./Wrong" @@ -37,7 +37,7 @@ export const WithFetching = (props: WithFetchingProps) => { sx={{ position: "absolute" /* to overwrite */ }} /> ) : undefined, - error ? {getErrorMessage(error)} : undefined + isError(error) ? {getErrorMessage(error)} : undefined )} ) diff --git a/src/utils/error.ts b/src/utils/error.ts index 02536407b..2dc5e4616 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -3,15 +3,20 @@ import axios, { AxiosError } from "axios" export const getErrorMessage = ( error?: Error | AxiosError | object | unknown ): string | undefined => { - if ( - !error || - (Object.getPrototypeOf(error) === Object.prototype && - Object.keys(error as object).length === 0) - ) - return + if (!isError(error)) return if (axios.isAxiosError(error)) return (error as AxiosError).response?.data?.message ?? error.message if (error instanceof Error) return error.message } + +export const isError = ( + error?: Error | AxiosError | object | unknown +): boolean => { + if (!error || JSON.stringify(error) === "{}") { + return false + } else { + return true + } +}