-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(SPV-1136): handle error on parsing error content in custom error …
…component.
- Loading branch information
1 parent
2036558
commit 9662c9d
Showing
2 changed files
with
49 additions
and
21 deletions.
There are no files selected for viewing
52 changes: 40 additions & 12 deletions
52
src/components/CustomErrorComponent/CustomErrorComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,53 @@ | ||
import fallingMan from '/falling-man.png'; | ||
import { ErrorResponse } from '@bsv/spv-wallet-js-client'; | ||
import {ErrorResponse} from '@bsv/spv-wallet-js-client'; | ||
|
||
export interface CustomErrorComponentProps { | ||
error: ErrorResponse | Error; | ||
} | ||
|
||
export const CustomErrorComponent = ({ error }: CustomErrorComponentProps) => { | ||
const isErrorResponse = (error: ErrorResponse | Error): error is ErrorResponse => error instanceof ErrorResponse; | ||
interface ApiErrorInfo { | ||
status: number; | ||
statusText: string; | ||
message: string; | ||
} | ||
|
||
const extractApiError = (error: ErrorResponse | Error): ApiErrorInfo | null => { | ||
let errorMessage = ''; | ||
if (error instanceof ErrorResponse) { | ||
try { | ||
errorMessage = JSON.parse(error.content).message; | ||
} catch (error) { | ||
console.log('Got unexpected error when parsing the content of the error response', error); | ||
} | ||
|
||
return { | ||
status: error.response.status, | ||
statusText: error.response.statusText, | ||
message: errorMessage, | ||
}; | ||
} | ||
return null; | ||
}; | ||
|
||
const ApiError = ({apiError}: { apiError: ApiErrorInfo }) => ( | ||
<> | ||
<p className={'first-letter:capitalize'}> {apiError.message}</p> | ||
<p> | ||
{apiError.status} - {apiError.statusText} | ||
</p> | ||
</> | ||
); | ||
|
||
const ErrorMessage = ({error}: { error: Error }) => <p>{error.message}</p>; | ||
|
||
export const CustomErrorComponent = ({error}: CustomErrorComponentProps) => { | ||
const apiError = extractApiError(error); | ||
|
||
return ( | ||
<div className="flex flex-col w-full h-[80vh] justify-center items-center"> | ||
<img src={fallingMan} alt="error image" /> | ||
<img src={fallingMan} alt="error image"/> | ||
<p className="mt-4">Something went wrong</p> | ||
<p className={'first-letter:capitalize'}> | ||
{isErrorResponse(error) ? JSON.parse(error.content).message : error.message} | ||
</p> | ||
{isErrorResponse(error) && ( | ||
<p> | ||
{error.response.status} - {error.response.statusText} | ||
</p> | ||
)} | ||
{apiError ? <ApiError apiError={apiError}/> : <ErrorMessage error={error}/>} | ||
</div> | ||
); | ||
}; |
18 changes: 9 additions & 9 deletions
18
src/components/WebhookErrorComponent/WebhookErrorComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import {ErrorResponse} from '@bsv/spv-wallet-js-client'; | ||
import {ErrorComponentProps} from '@tanstack/react-router'; | ||
import {CustomErrorComponent} from '@/components'; | ||
import {Ban} from 'lucide-react'; | ||
import { ErrorResponse } from '@bsv/spv-wallet-js-client'; | ||
import { ErrorComponentProps } from '@tanstack/react-router'; | ||
import { CustomErrorComponent } from '@/components'; | ||
import { Ban } from 'lucide-react'; | ||
|
||
const WebhooksDisabled = () => ( | ||
<div className="flex flex-col w-full h-[80vh] justify-center items-center"> | ||
<p> | ||
<Ban className="size-44 text-gray-500"/> | ||
<Ban className="size-44 text-gray-500" /> | ||
</p> | ||
<p className="mt-4 text-gray-500">Notification feature is not enabled</p> | ||
</div> | ||
); | ||
|
||
export const WebhookErrorComponent = ({error}: ErrorComponentProps) => { | ||
let errorCode = ""; | ||
export const WebhookErrorComponent = ({ error }: ErrorComponentProps) => { | ||
let errorCode = ''; | ||
if (error instanceof ErrorResponse) { | ||
try { | ||
errorCode = JSON.parse(error.content).code; | ||
} catch (error) { | ||
console.log("Got unexpected error when parsing the content of the error response", error); | ||
console.log('Got unexpected error when parsing the content of the error response', error); | ||
} | ||
} | ||
|
||
return errorCode === 'error-notifications-disabled' ? <WebhooksDisabled/> : <CustomErrorComponent error={error}/>; | ||
return errorCode === 'error-notifications-disabled' ? <WebhooksDisabled /> : <CustomErrorComponent error={error} />; | ||
}; |