Skip to content

Commit

Permalink
fix: error checking (#345)
Browse files Browse the repository at this point in the history
* 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.

* fix: Wrong error check
  • Loading branch information
terran6 authored May 11, 2023
1 parent b12c830 commit 1ea757b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/feedback/Fetching.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -37,7 +37,7 @@ export const WithFetching = (props: WithFetchingProps) => {
sx={{ position: "absolute" /* to overwrite */ }}
/>
) : undefined,
error ? <Wrong>{getErrorMessage(error)}</Wrong> : undefined
isError(error) ? <Wrong>{getErrorMessage(error)}</Wrong> : undefined
)}
</>
)
Expand Down
14 changes: 12 additions & 2 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import axios, { AxiosError } from "axios"

export const getErrorMessage = (
error?: Error | AxiosError | unknown
error?: Error | AxiosError | object | unknown
): string | undefined => {
if (!error) return
if (!isError(error)) return

if (axios.isAxiosError(error))
return (error as AxiosError<any>).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
}
}

0 comments on commit 1ea757b

Please sign in to comment.