I've a class of FooError
class FooError{
code: number;
message: string;
data: Object;
}
In Angular2, I've do a http post in a Service as follows:
export class FooService{
...some other code...
get(){
this._http.post(url, data, options)
.map(res => res.json())
.map(data => {
if(data.error){
throw data.error
}
return data.result
})
....
Now, since any Type of Errors can be thrown when subscribe to this post Observable.
E.g.
Response(error object class type) will be return if a) server's dead, b) backend behind proxy is dead c)etc.
How can I cast "data.error" to FoorError class so I can differentiate the errors that I would received after call.
What I had tried and doesn't work
let error:FooError = <FooError> data.error;
throw error;
I've also tried Javascript way, which also does work
Object.create(FooError, data.error)