Skip to content

Commit

Permalink
fix: exceptions support null message
Browse files Browse the repository at this point in the history
  • Loading branch information
bingtsingw committed Jul 6, 2023
1 parent 5286c75 commit 31f101b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
59 changes: 59 additions & 0 deletions src/cuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// import crypto from 'crypto';
// import os from 'os';

// const blockSize = 4;
// const base = 36;
// const discreteValues = Math.pow(base, blockSize);
// const limit = Math.pow(2, 32) - 1;

// const pad = (num: number | string, size: number) => {
// const s = '000000000' + num;
// return s.substring(s.length - size);
// };

// const padSize = 2;
// const hostname = os.hostname();
// const pid = pad(process.pid.toString(36), padSize);
// const hostId = pad(
// hostname
// .split('')
// .reduce((prev, char) => {
// return Number(prev) + char.charCodeAt(0);
// }, Number(hostname.length) + 36)
// .toString(36),
// padSize,
// );
// const fingerprint = () => {
// return pid + hostId;
// };

// let c = 0;
// const safeCounter = () => {
// c = c < discreteValues ? c : 0;
// c++;
// return c - 1;
// };

// const randomBlock = () => {
// const ramdom = Math.abs(crypto.randomBytes(4).readInt32BE() / limit);
// return pad(((ramdom * discreteValues) << 0).toString(base), blockSize);
// };

// export const HelperCuid = (date: Date) => {
// // hard-coded allows for sequential access
// const letter = 'c';

// // warning: this exposes the exact date and time.
// const timestamp = date.getTime().toString(base);

// // Prevent same-machine collisions.
// const counter = pad(safeCounter().toString(base), blockSize);

// // reduce different-machine collisions
// const print = fingerprint();

// // Grab some more chars from Math.random()
// const random = randomBlock() + randomBlock();

// return letter + timestamp + counter + print + random;
// };
2 changes: 1 addition & 1 deletion src/exception/401.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class UnauthorizedException extends BaseException {
public constructor(messages: unknown, code = 'UnauthorizedException') {
public constructor(messages?: unknown, code = 'UnauthorizedException') {
super(401, messages, code);
}
}
4 changes: 2 additions & 2 deletions src/exception/403.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BaseException } from './base';

export class ForbiddenException extends BaseException {
public constructor(messages: unknown, code = 'ForbiddenException') {
public constructor(messages?: unknown, code = 'ForbiddenException') {
super(403, messages, code);
}
}

export class NeedVipException extends BaseException {
public constructor(messages: unknown, code = 'NeedVip') {
public constructor(messages?: unknown, code = 'NeedVip') {
super(403, messages, code);
}
}
2 changes: 1 addition & 1 deletion src/exception/404.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseException } from './base';

export class NotFoundException extends BaseException {
public constructor(messages: unknown, code = 'NotFoundException') {
public constructor(messages?: unknown, code = 'NotFoundException') {
super(404, messages, code);
}
}
5 changes: 3 additions & 2 deletions src/exception/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export class BaseException extends Error {
super();
this.status = status;

this.code = snakeCase(`E ${startCase(replace(code, /Exception$/, ''))}`).toUpperCase();
this._messages = messages;
const normalizedCode = startCase(replace(code, /Exception$/, ''));
this.code = snakeCase(`E ${normalizedCode}`).toUpperCase();
this._messages = messages ?? normalizedCode;
this.message = this.getFirstMessage();

Error.captureStackTrace(this, this.constructor);
Expand Down

0 comments on commit 31f101b

Please sign in to comment.