3

I want to log exceptions generated in typescript to a log file (text file). I want to log information like description,message,name,number,stack. I have written Model, API Controller and logic to write the data into text file.

However I want to map Error Response generated after get, post,put operation in typescript. I have written below Interface

export interface TypeScriptException {
    Description: string;
    Message: string;
    Name: string;
    Number: string;
    ErrorStack: string;
}

When handleError method is called after Post in below example, how do I assign values from Error Response to my TypeScriptException object?

post(url: string, model: any): Observable<any> {

    let body = JSON.stringify(model);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this._http.post(url, body, options)
        .map((response: Response) => <any>response.json())
        .catch(this.handleError);
}

private handleError(error: Response) {

    let typeScriptException: TypeScriptException;

    //What to do here

}
3
  • What exactly do you receive as a part of the error? Commented Nov 23, 2018 at 7:31
  • 1
    If error return object. you can convert using new Error(error) . I Haven't reseted this. Commented Nov 23, 2018 at 7:42
  • if you are trying to handle http errors , then you always has an best practice to create a custom ErrorInterceptor from HttpInterceptor which can be injected into providers Commented Nov 23, 2018 at 8:26

2 Answers 2

1

In case of error, you get an instance of type HttpErrorResponse. You can extract fields from this instance and assign it to the typeScriptException variable that you've defined. You will have to leverage the details that are provided in this instance and set the fields on typeScriptException accordingly.

Here's what an instance of HttpErrorResponse looks like:

enter image description here

Keeping all the fields it has in mind, you can assign some of them to your typeScriptException, something along the lines of this:

private handleError(error: HttpErrorResponse) {
  let typeScriptException: TypeScriptException;
  let { message, name, ok, status, statusText, url } = error;
  //What to do here?
  typeScriptException = {
    Description: `${name} - ${message}`, // OR SOMETHING
    Message: message
    Name: name,
    Number: status, // OR SOMETHING
    ErrorStack: statusText // OR SOMETHING
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Getting compile error "Type 'Response' has no property 'message' and no string index signature." and "Type 'Response' has no property 'name' and no string index signature."
My bad. Use (error: HttpErrorResponse) instead of (error: Response)
I'm getting Cannot find name 'HttpErrorResponse'
@FiddleFreak You need to import { HttpErrorResponse } from '@angular/common/http';
0

In my case I use throw method to throw new Error

import {Observable} from "rxjs"

private handleError(res: HttpErrorResponse) {
  Observable.throw(new Error({message:res.message,code:res.status}));
}

or if you want to get all property of Response

private handleError(res: HttpErrorResponse) {
  Observable.throw(new Error({...res}));
}

1 Comment

what is the type of Rx?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.