Search code examples
typescriptangular-promiseangular-httpclientangular-httperror-logging

How can I disable error logging in Promise reject?


I am working with HttpClient and promises. I used httpclient.get to receive data from backend, which works all fine, but I have problem wit the errors I get. In fact I need the errors for later elaboration so I retrun them aswell, but i dont want the response to be logged as erorr log.

return new Promise<any>((resolve, reject) => {
            this.httpClient.get(url, {"some parametres"}).subscribe(
                response => {
                    resolve(response.body);
                },
                error => {
                    reject(error);
                }
            )
        });

So as you can see i just want the error data to be forwarded. Anything in this script works out fine. The only problem is that in my html page the console gets bursted with the error message from the rejection of the following form:

 Error: Uncaught (in promise): HttpErrorResponse: {"some error message"}
    {"some Traceback"}

Is there a way of errorhandling or disabling of logging, such that i dont get all the error logs?

Thanks!


Solution

  • Maybe in this way:

    function myPromise(parms) { //create a function who return a Promise
        return new Promise<any>((resolve, reject) => {
                    this.httpClient.get(url, {"some parametres"}).subscribe(
                        response => {
                            resolve(response.body);
                        },
                        error => {
                            reject(error);
                        }
                    )
                });
        }
    
        myPromise(args) //then, call that function and use "then" and "catch"
           .then((success)=> {
             console.log(success); //when success 
           })
           .catch((error) => {
             console.log(error) //when error
           });