import MessageSource from "../property/MessageSource"; import Bean from "../decorators/Bean"; import {__default_lang} from "../config/ApplicationEnvironment"; import Autowired from "../decorators/Autowired"; import StringUtil from "../utils/StringUtil"; export class TSException extends Error { public code?:string = "SYS9999"; public msg?:string = "Unable to process request. Please check your system."; public detail?:string = ""; public param?:any; public lang?:string = __default_lang; constructor(message?: string) { super(message); if (!message && message != undefined) { this.message = message; } } } export class TSExceptionForNoRollback extends TSException { constructor(message?: string) { super(message); } } export default class TSExceptionBuilder { private _code?:string = "SYS9999"; private _msg?:string = "Unable to process request. Please check your system."; private _detail?:string = ""; private _stack?:string[] = []; private _param?:any; public _lang?:string = __default_lang; @Autowired("MessageSource") public messageSource?: MessageSource; public code(_code: string) : TSExceptionBuilder { this._code = _code; return this; } public msg(_msg: string) : TSExceptionBuilder{ this._msg = _msg; return this; } public detail(_detail: string) : TSExceptionBuilder{ this._detail = _detail; return this; } public stack(_stack: string[]) : TSExceptionBuilder{ this._stack = _stack; return this; } public param(_param: string[]) : TSExceptionBuilder{ this._param = _param; return this; } public lang(_lang: string) : TSExceptionBuilder{ this._lang = _lang; return this; } public build(...args:any[]) : TSException { let ex = new TSException(); if(this._code != undefined && this._code != null) ex.code = this._code; if(this.messageSource != null) { this._msg = this.messageSource.getMessage(this._code, this._lang); } if(this._msg != undefined && this._msg != null) { if(args) { this._msg = StringUtil.format(this._msg, ...args); } ex.msg = this._msg; } ex.detail = this._detail; ex.param = this._param; return ex; } public noRollback() : TSExceptionForNoRollback { let ex = new TSExceptionForNoRollback(); if(this._code != undefined && this._code != null) ex.code = this._code; if(this.messageSource != null) { this._msg = this.messageSource.getMessage(this._code, this._lang); } if(this._msg != undefined && this._msg != null) ex.msg = this._msg; ex.detail = this._detail; ex.param = this._param; return ex; } }