Last active
October 31, 2022 21:11
-
-
Save ciscoheat/cca1f769c0e7cbf9dfb7d7bc25653c5c to your computer and use it in GitHub Desktop.
Javascript APIError class based on RFC7807
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
import type { Problem } from './Problem' | |
/** | |
* Javascript error class based on RFC7807. | |
* @link https://www.rfc-editor.org/rfc/rfc7807 | |
*/ | |
export class APIError extends Error implements Problem { | |
type: string; | |
title?: string; | |
status?: number; | |
detail?: string; | |
instance?: string; | |
constructor(details: Problem) { | |
super([details.title, details.detail].join('\n')); | |
this.type = details.type ?? 'about:blank'; | |
this.title = details.title; | |
this.status = details.status; | |
this.detail = details.detail; | |
this.instance = details.instance; | |
} | |
} |
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
/** | |
* Interface for a RFC7807 Problem Details Object. | |
* @see https://www.rfc-editor.org/rfc/rfc7807 | |
*/ | |
export interface Problem { | |
/** | |
* A URI reference [RFC3986] that identifies the problem type. This specification encourages that, when dereferenced, it provide human-readable documentation for the problem type (e.g., using HTML [W3C.REC-html5-20141028]). When this member is not present, its value is assumed to be "about:blank". | |
*/ | |
type?: string; | |
/** | |
* A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization (e.g., using proactive content negotiation; see [RFC7231, Section 3.4]). | |
*/ | |
title?: string; | |
/** | |
* The HTTP status code ([RFC7231], Section 6) generated by the origin server for this occurrence of the problem. | |
*/ | |
status?: number; | |
/** | |
* A human-readable explanation specific to this occurrence of the problem. | |
*/ | |
detail?: string; | |
/** | |
* A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced. | |
*/ | |
instance?: string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment