Skip to content

Instantly share code, notes, and snippets.

@ciscoheat
Last active October 31, 2022 21:11
Show Gist options
  • Select an option

  • Save ciscoheat/cca1f769c0e7cbf9dfb7d7bc25653c5c to your computer and use it in GitHub Desktop.

Select an option

Save ciscoheat/cca1f769c0e7cbf9dfb7d7bc25653c5c to your computer and use it in GitHub Desktop.

Revisions

  1. ciscoheat revised this gist Oct 31, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion APIError.ts
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ export class APIError extends Error implements Problem {
    detail?: string;
    instance?: string;

    constructor(details: RFC7807) {
    constructor(details: Problem) {
    super([details.title, details.detail].join('\n'));

    this.type = details.type ?? 'about:blank';
  2. ciscoheat revised this gist Oct 31, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion APIError.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import { Problem } from './Problem'
    import type { Problem } from './Problem'

    /**
    * Javascript error class based on RFC7807.
  3. ciscoheat revised this gist Oct 31, 2022. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions APIError.ts
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    import { RFC7807 } from './RFC7807'
    import { Problem } from './Problem'

    /**
    * Javascript error class based on RFC7807.
    * @link https://www.rfc-editor.org/rfc/rfc7807
    */
    export class APIError extends Error implements RFC7807 {
    export class APIError extends Error implements Problem {
    type: string;
    title?: string;
    status?: number;
    2 changes: 1 addition & 1 deletion RFC7807.ts → Problem.ts
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * Interface for a RFC7807 Problem Details Object.
    * @see https://www.rfc-editor.org/rfc/rfc7807
    */
    export interface 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".
    */
  4. ciscoheat created this gist Oct 31, 2022.
    23 changes: 23 additions & 0 deletions APIError.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import { RFC7807 } from './RFC7807'

    /**
    * Javascript error class based on RFC7807.
    * @link https://www.rfc-editor.org/rfc/rfc7807
    */
    export class APIError extends Error implements RFC7807 {
    type: string;
    title?: string;
    status?: number;
    detail?: string;
    instance?: string;

    constructor(details: RFC7807) {
    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;
    }
    }
    30 changes: 30 additions & 0 deletions RFC7807.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /**
    * Interface for a RFC7807 Problem Details Object.
    * @see https://www.rfc-editor.org/rfc/rfc7807
    */
    export interface RFC7807 {
    /**
    * 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;
    }