Skip to content

Commit

Permalink
feat: set default expose option for client- & server-error
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 7, 2023
1 parent 0bd38ea commit 4baf64d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/http/build/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function saveFile(content, filePath) {
/**
* Load template (.tpl) file from disk.
* @param file
* @return Promise<string>
*/
export async function loadTemplate(file) {
const tplPath = path.isAbsolute(file) ?
Expand Down
5 changes: 4 additions & 1 deletion packages/http/src/errors/base/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Input } from '../../types';
import { HTTPError, isHTTPError } from './http';

export class ClientError extends HTTPError {

constructor(...input: Input[]) {
super({ expose: true }, ...input);
}
}

export function isClientError(input: unknown): input is ClientError {
Expand Down
5 changes: 4 additions & 1 deletion packages/http/src/errors/base/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Input } from '../../types';
import { HTTPError, isHTTPError } from './http';

export class ServerError extends HTTPError {

constructor(...input: Input[]) {
super({ expose: false }, ...input);
}
}

export function isServerError(error: unknown): error is ServerError {
Expand Down
12 changes: 11 additions & 1 deletion packages/http/test/unit/module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line max-classes-per-file
import {
HTTPError, InternalServerError, NotFoundError, isClientError, isHTTPError, isServerError,
ClientError, HTTPError, InternalServerError, NotFoundError, ServerError, isClientError, isHTTPError, isServerError,
} from '../../src';

describe('src/module.ts', () => {
Expand All @@ -25,13 +25,23 @@ describe('src/module.ts', () => {
expect(error.statusCode).toEqual(500);
});

it('should create client error with default props', () => {
const error = new ClientError();
expect(error.expose).toBeTruthy();
});

it('should recognize client error', () => {
const error = new NotFoundError();
expect(isClientError(error)).toBeTruthy();
expect(isServerError(error)).toBeFalsy();
expect(isHTTPError(error)).toBeTruthy();
});

it('should create server error with default props', () => {
const error = new ServerError();
expect(error.expose).toBeFalsy();
});

it('should recognize server error', () => {
const error = new InternalServerError();
expect(isClientError(error)).toBeFalsy();
Expand Down

0 comments on commit 4baf64d

Please sign in to comment.