Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

Custom Error Message Class

export class CustomError {
	name: string = '';
	_statusCode: number = 500;
	status: 'error' | 'fail' = 'error';
	stack: string = '';
	isOperational: boolean = true;
	constructor(public message: string, code: number) {
		this.statusCode = code;
		Error.captureStackTrace(this, this.constructor);
	}
	set statusCode(num: number) {
		this.status = num.toString().startsWith('4') ? 'fail' : 'error';
		this._statusCode = num;
	}
	get statusCode() {
		return this._statusCode;
	}
}
//new CustomError(message, statusCode)
 
PREVIOUS NEXT
Tagged: #Custom #Error #Message #Class
ADD COMMENT
Topic
Name
7+4 =