[英]How to globally access/throw a custom error in angular?
我想創建自己的自定義錯誤,如下所示:
function CustomError(message) {
Error.captureStackTrace(this);
this.message = message;
this.name = "SomeCustomError";
}
CustomError.prototype = Object.create(Error.prototype);
我應該如何在角度中整合自定義錯誤? 無論在服務,控制器還是類似的地方,我都可以在任何角度拋出相同的自定義錯誤。 我當然不想每次都“依賴注入” CustomError。
throw CustomError('some message');
或者,如果我正在考慮,我也可以簡單地:
throw {name : "CustomError", message : "some message"};
有什么想法嗎?
在我的app.js-引導文件中,我現在有了類似的內容:
angular.module('myApp')
.run(['$window',
function($window) {
function CustomError(message) {
Error.captureStackTrace(this);
this.message = message;
this.name = "SomeCustomError";
}
CustomError.prototype = Object.create(Error.prototype);
$window.CustomError = CustomError;
// From here on, I can throw it anywhere:
// throw new CustomError("D'oh!");
}
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.