Accessing HTTP Error Response Body from HttpInterceptor in Angular

前端 未结 2 2028
难免孤独
难免孤独 2020-12-30 05:11

I have an HttpInterceptor to catch errors and display them in a modal. Besides error code and message, I would also like to show the body of the response which actually hold

2条回答
  •  悲&欢浪女
    2020-12-30 05:36

    To make full use of Typescript I usually create an interface that extends HttpErrorResponse:

    interface APIErrorResponse extends HttpErrorResponse {
       error: {
          id?: string
          links?: { about: string }
          status: string
          code?: string
          title: string
          detail: string
          source?: {
             pointer?: string
             parameter?: string
          }
          meta?: any
          }
    }
    

    After that, just assign APIErrorResponse instead of HttpErrorResponse to your error object and access your server's custom error as mentioned above: error.error

提交回复
热议问题