Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jul 28, 2021
1 parent 8ede464 commit 1fa284f
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/app/src/Route/RouteNotFound.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class RouteNotFound extends Error {
import {NotFound} from "@bunt/util";

export class RouteNotFound extends NotFound {
constructor(route: string) {
super(`Route "${route}" not found`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/input/src/Assertion/AssertionTypeError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ILogable, IReadableError} from "@bunt/util";
import {ILogable, IReadableError, ValidationError} from "@bunt/util";
import {TypeAbstract} from "../TypeAbstract";

export interface IReadableTypeError {
Expand All @@ -7,7 +7,7 @@ export interface IReadableTypeError {
payload: unknown;
}

export class AssertionTypeError extends Error implements IReadableError, ILogable<IReadableTypeError> {
export class AssertionTypeError extends ValidationError implements IReadableError, ILogable<IReadableTypeError> {
readonly #payload: unknown;
readonly #type: TypeAbstract<any>;

Expand Down
3 changes: 2 additions & 1 deletion packages/util/src/Exception/AssertionError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {isFunction} from "../is";
import {ILogable} from "../Logger";
import {ValidationError} from "./ValidationError";

export class AssertionError extends Error implements ILogable<Record<any, any>> {
export class AssertionError extends ValidationError implements ILogable<Record<any, any>> {
public readonly details?: any;

constructor(message?: string, details?: unknown | (() => unknown)) {
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/Exception/NotFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class NotFound extends Error {}
1 change: 1 addition & 0 deletions packages/util/src/Exception/PermissionError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class PermissionError extends Error {}
1 change: 1 addition & 0 deletions packages/util/src/Exception/ValidationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class ValidationError extends Error {}
3 changes: 3 additions & 0 deletions packages/util/src/Exception/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from "./interfaces";
export * from "./PermissionError";
export * from "./ValidationError";
export * from "./AssertionError";
export * from "./NotFound";
export * from "./functions";
10 changes: 6 additions & 4 deletions packages/web/src/WebServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Application, IRoute, RouteNotFound} from "@bunt/app";
import {Application, IRoute} from "@bunt/app";
import {ActionAny, Context, ContextArg, Heartbeat, IDisposable, IRunnable, unit, Unit} from "@bunt/unit";
import {assert, AssertionError, Ctor, logger, Logger} from "@bunt/util";
import {assert, Ctor, logger, Logger, PermissionError, ValidationError} from "@bunt/util";
import {NotFound} from "@bunt/util/dist/Exception/NotFound";
import * as http from "http";
import {IncomingMessage, ServerResponse} from "http";
import {Socket} from "net";
Expand All @@ -22,8 +23,9 @@ export class WebServer<C extends Context> extends Application<C>
this.#options = options ?? {};

this.setExceptionResponseHeaders(
[AssertionError, {code: 400}],
[RouteNotFound, {code: 404, status: "Not found"}],
[PermissionError, {code: 403, status: "Permission denied"}],
[ValidationError, {code: 400, status: "Validation error"}],
[NotFound, {code: 404, status: "Not found"}],
);
}

Expand Down

0 comments on commit 1fa284f

Please sign in to comment.