How to handle error for response Type blob in HttpRequest

后端 未结 5 2002

I am calling an http request using httpClient and using response Type as \'blob\' but the problem is when it goes in error block the response type remains \'blob\'.This is c

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 23:44

    The code from SplitterAlex worked for me but I needed the error object and the status code too. Thats why I adjusted the parseErrorBlob method a little bit.

    public parseErrorBlob(err: HttpErrorResponse): Observable {
    const reader: FileReader = new FileReader();
    const obs = new Observable((observer: any) => {
      reader.onloadend = (e) => {
        const messageObject = JSON.parse(reader.result as string);
        observer.error({
          error : {
            message : messageObject.message
          },
          message : messageObject.message,
          status : err.status
        });
        observer.complete();
      };
    });
    reader.readAsText(err.error);
    return obs;
    

    }

提交回复
热议问题