为什么我的响应错误与错误拦截器捕获的错误不同?

Why is my response error different from the one caught by error interceptor?

提问人:ericute 提问时间:6/15/2023 最后编辑:ericute 更新时间:7/26/2023 访问量:25

问:

我有这个 API,如果要下载的文件大小超过最大设置,它会引发异常。

@GET
    @Path("files")
    public Response getFile(@QueryParam("filePath") String filePath) throws IOException {
        File file = new File(filePath);

        long fileSizeKb = Math.max(file.length() / 1024, 1);

        if (fileSizeKb > APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB) {
            String msg = MessageBundle.getMessage(MessageKeys.ERR_FILE_SIZE_EXCEEDED, String.valueOf(fileSizeKb),
                    String.valueOf(APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB));
            System.out.println("msg: " + msg);
            throw new GeneralException(msg,
                    new FileSizeLimitExceededException(msg, fileSizeKb, APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB));
        }

        // Read file content into byte array
        byte[] fileBytes = readFileContent(file);

        return Response.ok(fileBytes).header("Content-Disposition", "attachment; filename=" + file.getName()).build();
    }

它由这个角度函数调用:

downloadFile(filePath: string) {
    this.migrationService.getFile(filePath).subscribe({
      next: (response) => {
        const fileContent: Uint8Array = new Uint8Array(response);
        const fileContentStr = new TextDecoder().decode(fileContent);

        const forwardSplit = filePath.split("/");
        const backwardSplit = forwardSplit[forwardSplit.length - 1].split("\\");
        let fileName = backwardSplit[backwardSplit.length - 1];

        const element = document.createElement("a");
        element.setAttribute(
          "href",
          "data:text/cvs;charset=utf-8," + encodeURIComponent(fileContentStr)
        );
        element.setAttribute("download", fileName);
        element.style.display = "none";
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
      },
      error: (err) => {
        console.log("err", err);
      },
    });
  }

到目前为止一切都很好。当我强制错误时,我得到这个作为响应。这是我需要的,所以我可以正确显示它。enter image description here

但是,这是我在 downloadFile 函数的控制台日志中看到的。enter image description here

为什么我没有收到响应对象,我该如何解决它?

Angular 错误处理 httpresponse

评论


答:

0赞 ericute 6/15/2023 #1

看到这个问题与我遇到的问题非常相似。它让我知道我必须做什么。

我更新了函数的错误处理。我处理了arrayBuffer,类似于读取文件内容时。这一次,传入 error 对象。downloadFile

downloadFile(filePath: string) {
    this.migrationService.getFile(filePath).subscribe({
      next: (response) => {
        const fileContent: Uint8Array = new Uint8Array(response);
        const fileContentStr = new TextDecoder().decode(fileContent);
        const forwardSplit = filePath.split("/");
        const backwardSplit = forwardSplit[forwardSplit.length - 1].split("\\");
        let fileName = backwardSplit[backwardSplit.length - 1];
        const element = document.createElement("a");
        element.setAttribute(
          "href",
          "data:text/cvs;charset=utf-8," + encodeURIComponent(fileContentStr)
        );
        element.setAttribute("download", fileName);
        element.style.display = "none";
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
      },
      error: (err) => {
        const buffer = new Uint8Array(err.error);
        const bufferStr = new TextDecoder().decode(buffer);
        const errorResponse = JSON.parse(bufferStr);
        const config = new MatDialogConfig();
        this.dialogRef = this.dialog.open(ErrorMessageComponent, config);
        this.dialogRef.componentInstance.msgObj = {
          errorMessage: errorResponse.statusText,
        };
      },
    });
  }