Skip to content

Commit

Permalink
fix: web server error response
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Sep 7, 2022
1 parent ad87e98 commit 94fd67a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/util/src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AssertionError} from "./Exception";
import {isFunction, isInstanceOf} from "./is";
import {isFunction, isInstanceOf, isString} from "./is";

export type AssertionDetailsAllowType = string | Record<any, any> | null | number;
export type AssertionDetails = (() => AssertionDetailsAllowType) | AssertionDetailsAllowType;
Expand All @@ -19,6 +19,12 @@ export function assert(expr: unknown, message?: AssertionMessage, details?: Asse
}
}

export function validateTrue(result: true | string, details?: AssertionDetails): void {
if (isString(result)) {
throw createAssertionError(result, details);
}
}

export function fails(expr: unknown, message?: AssertionMessage, details?: AssertionDetails): void {
if (expr) {
throw createAssertionError(message, details);
Expand Down
5 changes: 2 additions & 3 deletions packages/web/src/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,12 @@ export class WebServer<C extends Context> extends Application<C> implements IDis
assert(request.validate(this), "Validate request failed");
await request.respond(await this.run(request));
} catch (reason) {
await request.respond(reason);
if (reason instanceof ResponseAbstract) {
await request.respond(reason);

return;
}

throw reason;
this.captureException(reason);
}
}

Expand Down

0 comments on commit 94fd67a

Please sign in to comment.