简体   繁体   English

Angular2 ExceptionHandler-检查错误对象是否为Response

[英]Angular2 ExceptionHandler - check if error object is 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);
}

The code works fine, I can see the error in the console. 该代码工作正常,我可以在控制台中看到错误。 The error is a Response object, defined in @angular/core. 错误是在@ angular / core中定义的Response对象。 However, the error parameter is 'any'. 但是,错误参数为“ any”。 I can't change the error type (eg error:Response), because it wont necessarily be a Response object, it could be anything. 我不能更改错误类型(例如error:Response),因为它不一定是Response对象,可以是任何东西。

I wanted to use (error instanceof Response), but that wont work, because error is a type of object, which makes sense. 我想使用(Error instanceof Response),但是那行不通,因为错误是一种对象,这很有意义。

UPDATE 更新

So it turns out (error instanceof Response) does work after all. 因此,事实证明(error instanceof Response)确实可以工作。 For some reason it doesn't appear to work when you're debugging the typescript using VS Code. 由于某些原因,当您使用VS Code调试打字稿时,它似乎不起作用。 I put a watch on it and it always returned false. 我放了一块手表,它总是返回false。 Maybe it's because i'm not checking at run-time 也许是因为我没有在运行时检查

Anyway, important thing is that in the context of Angular2 Response objects, instanceof will work just fine as they do have a constructor 无论如何,重要的是,在Angular2 Response对象的上下文中, instanceof可以正常工作,因为它们确实具有构造函数

Thank to @DanSimon for helping to narrow down what was going wrong, and for providing other ways to check the type of an object! 感谢@DanSimon帮助缩小问题范围,并提供其他方法来检查对象的类型!

Interesting question. 有趣的问题。 Since TypeScript is really just compiling down to JS, classes don't really exist. 由于TypeScript实际上只是编译为JS,所以类实际上并不存在。 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". 用户定义的类型防护只是检查对象的属性/功能以确定其类型的一种好方法,优点是TS不会抱怨“缺少属性/功能”。 Using the "instanceof" works but only if the Object has a constructor as it's comparing the two Object's constructors. 仅当对象具有构造函数时才使用“ instanceof”,因为它在比较两个对象的构造函数。 So if your Object doesn't have a constructor it's a no go. 因此,如果您的对象没有构造函数,那是不可行的。 You can find the documentation for these two here . 您可以在此处找到这两个文档。

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”功能的“ app / child.component.ts”上完成。

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++;
}

You will probably need to leverage both and also "typeof" for your primitive types to do a complete inspection of the object. 您可能需要同时利用原始类型的“ typeof”和对对象的完整检查。 So using "typeof" to check for basic types "string","number","boolean". 因此,使用“ typeof”来检查基本类型“ 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". 然后使用“ instanceof”查看构造函数是否匹配,最后使用“ User-Defined Type Guard”查找特定的“ properties / functions”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM