Currently, I am utilizing IONIC version 3.0.1 and endeavoring to identify the error type that is being returned from the server. Here are some examples of the error responses:
// HTTP Errors
{"error":"unauthorized","error_description":"long error description"}
// Java Exception
{"timestamp":00000000000,"status":500,"error":"Internal Server Error","exception":"java.lang.Exception","message":"Java Exception","path":"/app/login"}
To address these different errors, I have crafted specific classes for each:
// For HTTP Error
export class HTTPError {
constructor() {
}
public error: string;
public error_description: string;
}
// For Java Error
export class JavaError {
constructor() {
}
public error: string;
public exception: string;
public message: string;
public path: string;
public status: number;
public timestamp: Date;
}
My goal is to pinpoint the error type using the instanceof keyword within my code:
...
let error: any = JSON.parse(response);
if (error instanceof HTTPError) {
// Condition Block 1
} else if (error instanceof JavaError) {
// Condition Block 2
} else {
// Condition Block 3
}
...
Despite efforts, every response leads to the execution of Condition Block 3. The conditions within the if statements persistently return false.
The question remains - what could be the issue in my approach?