Skip to content

Commit

Permalink
Added extensions to problem details (#32)
Browse files Browse the repository at this point in the history
* Added extensions to problem details

* feat: made detail/instance optional (as per RFC)

* chore: updated yarn lockfile, made 'husky install' part of prep script
  • Loading branch information
tobias-at-tibber authored Mar 6, 2023
1 parent a8e19e5 commit 96ac162
Show file tree
Hide file tree
Showing 4 changed files with 6,059 additions and 6,152 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "gts clean",
"compile": "tsc",
"fix": "gts fix && sortier \"./**/*.{ts,tsx,js,jsx}\"",
"prepare": "yarn run compile",
"prepare": "yarn run compile && husky install",
"pretest": "yarn run compile",
"posttest": "yarn run lint",
"release": "semantic-release",
Expand Down
19 changes: 13 additions & 6 deletions src/errors/ProblemDetailsError.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import {HttpError} from './HttpError';

export type ProblemDetailsArgs = {
detail: string;
instance: string;
detail?: string;
instance?: string;
statusCode: number;
title: string;
type: string;
extensions?: {
[k: string]: unknown;
};
};

export class ProblemDetailsError extends HttpError {
public detail: string;
public type: string;
public instance: string;
public title: string;
public detail?: string;
public instance?: string;
public extensions?: {
[k: string]: unknown;
};

constructor(args: ProblemDetailsArgs) {
const {detail, instance, statusCode, title, type} = args;
const {detail, instance, statusCode, title, type, extensions} = args;

super(detail, statusCode);
super(detail ?? title, statusCode);

this.detail = detail;
this.type = type;
this.instance = instance;
this.title = title;
this.extensions = extensions ?? {};
}
}
12 changes: 10 additions & 2 deletions src/jsonMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@ export const jsonMiddleware: JsonMiddleware = (
* additional details as a JSON payload.
*/
if (err instanceof ProblemDetailsError) {
const {detail, httpStatus: status, instance, title, type} = err;
const {
detail,
httpStatus: status,
instance,
title,
type,
extensions,
} = err;
const response = res
.status(status)
.contentType('application/problem+json')
.json({
.send({
...(extensions ?? {}),
detail,
instance,
status,
Expand Down
Loading

0 comments on commit 96ac162

Please sign in to comment.