0

I am having difficulties figuring out how to add a custom 500 error message for an exception.

When the response has the status of Internal Server Error, I receive a backend response that looks like this:

errors: [
    0: {
      errorCode: null
      exceptionMessage: null
      language: null
      message: "BAPIException with type: E id:  messageNumber: 000 message: Keine Barcodedaten gefunden (ZPORH_BARCODELISTE_CSV)"
      position: null
      reason: null
      subject: null
      subjectType: null,
      type: "BWPErpIntegrationBapiError"
    }
]

My internal server handler looks like this so far:

responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;

  // tslint:disable-next-line:variable-name
  handleError(_request: HttpRequest<any>, response: HttpErrorResponse): void {
    if (response.status === this.responseStatus) {
      // console.log(response.error);
      if (response.error?.type === 'BWPErpIntegrationBapiError') {
        this.globalMessageService.add(
          response.error?.message,
          GlobalMessageType.MSG_TYPE_ERROR
        );
      } else {
        // console.log(response.status);

        this.globalMessageService.add(
          { key: 'httpHandlers.internalServerError' },
          GlobalMessageType.MSG_TYPE_ERROR
        );
      }
    }
  }

The problem I am facing is that the response I get from the backend is not the same as the HttpErrorResponse from Angular, and I do not know how to approach this problem. I tried to extend the HttpErrorResponse and add an errors property that has that array with values and create an interceptor for it but console logging the response.errors[0] was always null.

Also the way I get this BWPErpIntegrationBapiError exception is by trying to download and CSV that also has a response.error value of Blob.

Any help would be appreciated!

1 Answer 1

0

I managed to find a solution by parsing the blob data, I will only need to add another if statement for when there is no Blob type data.

 responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;

  // tslint:disable-next-line:variable-name
  handleError(_request: HttpRequest<any>, response: any): void {
    if (response.status === this.responseStatus) {
      if (response.error instanceof Blob) {
        response.error.text().then(text => {
          const errors = JSON.parse(text).errors;
          console.log(errors);

          if (errors[0].type === 'BWPErpIntegrationBapiError') {
            this.globalMessageService.add(
              errors[0].message,
              GlobalMessageType.MSG_TYPE_ERROR
            );
          }
        });
      } else {
        this.globalMessageService.add(
          { key: 'httpHandlers.internalServerError' },
          GlobalMessageType.MSG_TYPE_ERROR
        );
      }
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

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.