-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace all errors with custom error types (#839)
Co-authored-by: Henrik Skog <henrikskog01@gmail.com>
- Loading branch information
1 parent
8b90cae
commit 852260d
Showing
43 changed files
with
563 additions
and
134 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { PROBLEM_DETAILS } from "./http-problem-details" | ||
|
||
/** | ||
* Represents a problem details type. | ||
* | ||
* This implementation does not yet support: | ||
* - instance member | ||
* - extension members | ||
* | ||
* @see https://datatracker.ietf.org/doc/html/rfc9457#name-the-problem-details-json-ob | ||
*/ | ||
export type ProblemDetail = Pick<ApplicationError, "type" | "status" | "title"> | ||
|
||
/** | ||
* Exception type modelled after RFC9457 Problem Details for HTTP APIs | ||
* | ||
* @see https://tools.ietf.org/html/rfc7807 | ||
* | ||
* This implementation does not yet support: | ||
* - instance member | ||
* - extension members | ||
* | ||
* All exceptions thrown by the application modules should be of this type in | ||
* order to be detailed enough for the client to understand the problem and | ||
* to be able to handle it properly. | ||
*/ | ||
export class ApplicationError extends Error { | ||
public readonly type: string | ||
public readonly status: number | ||
public readonly title: string | ||
public readonly detail?: string | ||
|
||
constructor(problemType: ProblemDetail, detail?: string) { | ||
const { type, status, title } = problemType | ||
|
||
super(detail ?? title) | ||
|
||
this.type = type | ||
this.status = status | ||
this.title = title | ||
this.detail = detail | ||
} | ||
} | ||
|
||
export class IllegalStateError extends ApplicationError { | ||
constructor(detail: string) { | ||
super(PROBLEM_DETAILS.IllegalState, detail) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ProblemDetail } from "./error" | ||
|
||
const RFC_REGISTRY_BASE = "https://datatracker.ietf.org/doc/html/rfc9110#name" | ||
|
||
type ProblemDetails = { | ||
BadRequest: ProblemDetail | ||
NotFound: ProblemDetail | ||
UnprocessableContent: ProblemDetail | ||
InternalServerError: ProblemDetail | ||
IllegalState: ProblemDetail | ||
} | ||
|
||
/** | ||
* All the supported problem details types for the application, in accordance with RFC 9457. | ||
* If problem type is a locator, the client can dereference it to get more information about the problem. | ||
* | ||
* When adding new problem details types, | ||
* @see https://datatracker.ietf.org/doc/html/rfc9457#name-defining-new-problem-types | ||
* | ||
*/ | ||
export const PROBLEM_DETAILS: ProblemDetails = { | ||
BadRequest: { | ||
status: 400, | ||
title: "Bad Request", | ||
type: `${RFC_REGISTRY_BASE}-400-bad-request`, | ||
}, | ||
NotFound: { | ||
status: 404, | ||
title: "Not Found", | ||
type: `${RFC_REGISTRY_BASE}-404-not-found`, | ||
}, | ||
UnprocessableContent: { | ||
status: 422, | ||
title: "Unprocessable Content", | ||
type: `${RFC_REGISTRY_BASE}-422-unprocessable-content`, | ||
}, | ||
InternalServerError: { | ||
status: 500, | ||
title: "Internal Server Error", | ||
type: `${RFC_REGISTRY_BASE}-500-internal-server-error`, | ||
}, | ||
IllegalState: { | ||
status: 500, | ||
title: "Invalid state reached", | ||
type: "/problem/illegal-state", // TODO: create page for this that describes how to resolve the problem and change this to a URL to that page. (https://datatracker.ietf.org/doc/html/rfc9457#section-4-10) | ||
}, | ||
} |
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,3 +1,16 @@ | ||
export * from "./errors/errors" | ||
export * from "./error" | ||
export * from "./utils/db-utils" | ||
export * from "./modules/core" | ||
|
||
export * from "./modules/article/article-error" | ||
export * from "./modules/committee/committee-error" | ||
export * from "./modules/company/company-error" | ||
export * from "./modules/event/attendee-error" | ||
export * from "./modules/event/event-error" | ||
export * from "./modules/job-listing/job-listing-error" | ||
export * from "./modules/offline/offline-error" | ||
export * from "./modules/mark/mark-error" | ||
export * from "./modules/mark/personal-mark-error" | ||
export * from "./modules/payment/payment-error" | ||
export * from "./modules/payment/refund-request-error" | ||
export * from "./modules/payment/product-error" |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApplicationError } from "../../error" | ||
import { PROBLEM_DETAILS } from "../../http-problem-details" | ||
|
||
export class ArticleNotFoundError extends ApplicationError { | ||
constructor(id: string) { | ||
super(PROBLEM_DETAILS.NotFound, `Article with ID:${id} not found`) | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApplicationError } from "../../error" | ||
import { PROBLEM_DETAILS } from "../../http-problem-details" | ||
|
||
export class CommitteeNotFoundError extends ApplicationError { | ||
constructor(id: string) { | ||
super(PROBLEM_DETAILS.NotFound, `Committee with ID:${id} not found`) | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApplicationError } from "../../error" | ||
import { PROBLEM_DETAILS } from "../../http-problem-details" | ||
|
||
export class CompanyNotFoundError extends ApplicationError { | ||
constructor(id: string) { | ||
super(PROBLEM_DETAILS.NotFound, `Company with ID:${id} not found`) | ||
} | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ApplicationError } from "../../error" | ||
import { PROBLEM_DETAILS } from "../../http-problem-details" | ||
|
||
export class AttendeeNotFoundError extends ApplicationError { | ||
constructor(id: string) { | ||
super(PROBLEM_DETAILS.NotFound, `Attendee with ID:${id} not found`) | ||
} | ||
} |
Oops, something went wrong.
852260d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
invoicification – ./apps/invoicification
invoicification-dotkom.vercel.app
invoicification-git-main-dotkom.vercel.app
invoicification.vercel.app
faktura.online.ntnu.no