如何在angular中全局访问/抛出自定义错误
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);
我应该如何在角集成自定义错误?我可以在angular中的任何地方抛出相同的自定义错误,无论是在服务、控制器还是类似的地方。我当然不想每次都"依赖注入"CustomError。
throw CustomError('some message');
或者,如果我在思考,我也可以简单地:
throw {name : "CustomError", message : "some message"};
任何想法吗?
在我的app.js - bootstrap文件中,我现在有这样的内容:
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!");
}
]);
相关文章:
- 是否排除节点中错误堆栈的第一行?/节点中的自定义错误类型
- 如何访问npm模块抛出的自定义错误对象[error:[object object]]
- 登录时显示自定义错误消息
- AJAX自定义错误处理代码问题
- jQuery验证自定义错误
- 来自元素属性的自定义错误消息:jQuery 验证插件
- 使用 $.post 对用户进行身份验证以返回自定义错误/成功消息
- 自定义错误显示在自定义位置,而不是每个元素旁边
- Jquery/php 输出自定义错误消息
- 通过jQuery在Html.ValidationMessageFor中显示自定义错误
- 在V8中从C++代码创建自定义错误类
- 自定义HTML5表单验证最初未显示自定义错误
- JQuery验证器自定义错误位置不起作用
- 如何在Sails.js中通过Hand构建非自定义错误
- 在Babel下调用super()时,由于自定义错误,无法输出堆栈
- 自定义错误和bluebird's与ErrorClass的捕获会导致意外行为
- 自定义错误格式
- 自定义错误对象与阿波罗服务器
- 如何在jquery验证插件中添加自定义错误替换消息
- 如何捕获一个错误,并在数据库中保存错误细节和重定向到自定义错误页面在经典asp