Angular2 ExceptionHandler-检查错误对象是否为Response [英] Angular2 ExceptionHandler - check if error object is Response

查看:72
本文介绍了Angular2 ExceptionHandler-检查错误对象是否为Response的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用以下代码来处理异常:

I am currently using the following code to handle exceptions:

@Injectable()
export class CustomExceptionHandler extends ExceptionHandler {
call(error, stackTrace = null, reason = null) {
    console.warn(error);
}

代码工作正常,我可以在控制台中看到错误.错误是在@ angular/core中定义的Response对象.但是,错误参数为"any".我无法更改错误类型(例如error:Response),因为它不一定是Response对象,可以是任何东西.

The code works fine, I can see the error in the console. The error is a Response object, defined in @angular/core. However, the error parameter is 'any'. I can't change the error type (e.g. error:Response), because it wont necessarily be a Response object, it could be anything.

我想使用(Error instanceof Response),但是那行不通,因为错误是一种对象,这很有意义.

I wanted to use (error instanceof Response), but that wont work, because error is a type of object, which makes sense.

更新

因此,事实证明(错误instanceof响应)确实可以正常工作.由于某些原因,当您使用VS Code调试打字稿时,它似乎不起作用.我放了一块手表,它总是返回false.也许是因为我没有在运行时检查

So it turns out (error instanceof Response) does work after all. For some reason it doesn't appear to work when you're debugging the typescript using VS Code. I put a watch on it and it always returned false. Maybe it's because i'm not checking at run-time

无论如何,重要的是,在Angular2 Response对象的上下文中, instanceof 可以正常工作,因为它们确实具有构造函数

Anyway, important thing is that in the context of Angular2 Response objects, instanceof will work just fine as they do have a constructor

感谢@DanSimon帮助缩小出现问题的地方,并提供了其他方法来检查对象的类型!

Thank to @DanSimon for helping to narrow down what was going wrong, and for providing other ways to check the type of an object!

推荐答案

有趣的问题.由于TypeScript实际上只是编译为JS,因此类实际上并不存在.我可以想到两种检查方法,可以使用用户定义的类型防护"或实例类型的防护".用户定义的类型防护只是检查对象的属性/功能以确定其类型的一种理想方式,优点是TS不会抱怨缺少属性/功能".仅当对象具有构造函数时才使用"instanceof",因为它正在比较两个对象的构造函数.因此,如果您的对象没有构造函数,那是不可行的.您可以在此处找到文档.

Interesting question. Since TypeScript is really just compiling down to JS, classes don't really exist. I can think of two ways you could check, you can use a "User-Defined Type Guard" or use the "instanceof type guard". The User-Defined Type Guard is just a fancy way of checking a property/function of the object to determine its Type, the advantage is TS won't complain about a "missing property/function". Using the "instanceof" works but only if the Object has a constructor as it's comparing the two Object's constructors. So if your Object doesn't have a constructor it's a no go. You can find the documentation for these two here.

我还制作了 plunker 来演示它们的工作方式.结果将加载到控制台中,因此请使用浏览器调试工具查看结果.所有工作都在"ngOnChanges"功能的"app/child.component.ts"上完成.

I also made a plunker demonstrating how they work. The results will load into the console, so use your browsers debug tool to see the results. All the work is being done on "app/child.component.ts" in the "ngOnChanges" function.

ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
  console.log();
  console.log("Custom function isSimpleChange:", this.isSimpleChange(changes));
  console.log("Using instanceof SimpleChange:", (changes instanceof SimpleChange));
  console.log("Using Object.prototype.toString.call():", Object.prototype.toString.call(changes));
  console.log("Logging the object itself:",changes);
  console.log("-----------------------------------------------------------------------------------------");
  let testExample = new ExampleObject();
  console.log("Custom function isExampleObject:", this.isExampleObject(testExample));
  console.log("Using instanceof ExampleObject:", (testExample instanceof ExampleObject));
  console.log("Using Object.prototype.toString.call():" + Object.prototype.toString.call(testExample));
  console.log(testExample);


  this._result = changes.value.currentValue;
  this._changes++;
}

您可能需要同时使用原始类型的"typeof"和对象的完整检查.因此,使用"typeof"来检查基本类型"string","number","boolean".然后使用"instanceof"查看构造函数是否匹配,最后使用"User-Defined Type Guard"来查找特定的属性/函数".

You will probably need to leverage both and also "typeof" for your primitive types to do a complete inspection of the object. So using "typeof" to check for basic types "string","number","boolean". Then using "instanceof" to see if the constructors match, finally using a "User-Defined Type Guard" to look for specific "properties/functions".

这篇关于Angular2 ExceptionHandler-检查错误对象是否为Response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆