Mariusz Klochowicz
3 years ago
4 changed files with 53 additions and 10 deletions
@ -0,0 +1,28 @@ |
|||
import { HttpError } from "./HttpError"; |
|||
|
|||
// A generic way of creating an error toast
|
|||
// TODO: Don't use any (`toast: typeof useToast` did not work :( )
|
|||
export default function createErrorToast(toast: any, e: any) { |
|||
if (e instanceof HttpError) { |
|||
const description = e.detail ? e.detail : ""; |
|||
|
|||
toast({ |
|||
title: "Error: " + e.title, |
|||
description, |
|||
status: "error", |
|||
duration: 10000, |
|||
isClosable: true, |
|||
}); |
|||
} else { |
|||
console.log(e); |
|||
const description = typeof e === "string" ? e : JSON.stringify(e); |
|||
|
|||
toast({ |
|||
title: "Error", |
|||
description, |
|||
status: "error", |
|||
duration: 10000, |
|||
isClosable: true, |
|||
}); |
|||
} |
|||
} |
@ -0,0 +1,18 @@ |
|||
// A wrapper to parse RFC 7807
|
|||
// Pass result of `await response.json()` into the constructor.
|
|||
export class HttpError extends Error { |
|||
title: string; |
|||
detail?: string; |
|||
|
|||
// FIXME: Constructor can't be async, so we can't pass `Response` here
|
|||
constructor(json_resp: any) { |
|||
let title = json_resp.title; |
|||
super(title); |
|||
this.title = title; |
|||
if (json_resp.detail) { |
|||
this.detail = json_resp.detail; |
|||
} |
|||
|
|||
Object.setPrototypeOf(this, HttpError.prototype); |
|||
} |
|||
} |
Loading…
Reference in new issue