函数重载
export class CustomException extends HttpException {
constructor(param: ExceptionParam);
constructor(param: string);
constructor(param: ExceptionParam | string) {
if (typeof param === 'string') {
// 处理字符串参数的逻辑
const message = param;
const code = '500';
super(message, code);
} else {
// 处理ExceptionParam参数的逻辑
let message: string;
let code: any;
// ... 处理ExceptionParam的逻辑
super(message, code);
}
this.name = 'CustomException';
}
}
静态工厂
export class CustomException extends HttpException {
constructor(message: string, code: any) {
super(message, code);
this.name = 'CustomException';
}
static fromExceptionParam(param: ExceptionParam) {
let message: string;
let code: any;
// ... 处理ExceptionParam的逻辑
return new CustomException(message, code);
}
static fromStringParam(param: string) {
const message = param;
const code = '500';
return new CustomException(message, code);
}
}