Skip to content

Commit

Permalink
fix: handle axioserror response data being blob
Browse files Browse the repository at this point in the history
  • Loading branch information
TheChristophe committed Jul 16, 2024
1 parent 0eead1d commit fa6ed48
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions frontend/components/ErrorBox/BasicErrorDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import { isAxiosError } from 'axios';
import { type FC } from 'react';
import { type AxiosResponse, isAxiosError } from 'axios';
import { type FC, type ReactNode, useEffect, useState } from 'react';
import LoadingSpinner from '../LoadingSpinner';

type ResponseContentsProps = {
response: AxiosResponse<unknown>;
};
const ResponseContents: FC<ResponseContentsProps> = ({ response }) => {
const [message, setMessage] = useState<ReactNode>();

useEffect(() => {
if (typeof response.data === 'string') {
setMessage(response.data);
} else if (response.data instanceof Blob) {
response.data
.text()
.then((text) => setMessage(text))
.catch((error) =>
setMessage(`(Failed to process server error blob text: "${error.toString()}")`)
);
} else {
setMessage('(Failed to read server error message)');
}
}, [response]);

return message ?? <LoadingSpinner />;
};

type BasicErrorDisplayProps = {
error: unknown;
};
const BasicErrorDisplay: FC<BasicErrorDisplayProps> = ({ error }) => {
return isAxiosError(error) ? (
const BasicErrorDisplay = ({ error }: BasicErrorDisplayProps) =>
isAxiosError(error) ? (
<>
{error.code !== undefined && (
<>
<p>The server did not respond as expected:</p>
{error.response && (
<pre className="max-w-[80ch] overflow-x-scroll rounded-md bg-amber-200 p-2">
{JSON.stringify(error.response?.data, null, 2)}
<ResponseContents response={error.response} />
</pre>
)}
{!error.response && <p>No server response.</p>}
Expand All @@ -23,6 +48,5 @@ const BasicErrorDisplay: FC<BasicErrorDisplayProps> = ({ error }) => {
) : (
<p>An unknown error occured.</p>
);
};

export default BasicErrorDisplay;

0 comments on commit fa6ed48

Please sign in to comment.