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

huangapple go评论99阅读模式
英文:

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

问题

I have this API that throws an exception if the file size to be downloaded exceeds the max set.

  1. @GET
  2. @Path("files")
  3. public Response getFile(@QueryParam("filePath") String filePath) throws IOException {
  4. File file = new File(filePath);
  5. long fileSizeKb = Math.max(file.length() / 1024, 1);
  6. if (fileSizeKb > APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB) {
  7. String msg = MessageBundle.getMessage(MessageKeys.ERR_FILE_SIZE_EXCEEDED, String.valueOf(fileSizeKb),
  8. String.valueOf(APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB));
  9. System.out.println("msg: " + msg);
  10. throw new GeneralException(msg,
  11. new FileSizeLimitExceededException(msg, fileSizeKb, APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB));
  12. }
  13. // Read file content into byte array
  14. byte[] fileBytes = readFileContent(file);
  15. return Response.ok(fileBytes).header("Content-Disposition", "attachment; filename=" + file.getName()).build();
  16. }

It is being called by this angular function:

  1. downloadFile(filePath: string) {
  2. this.migrationService.getFile(filePath).subscribe({
  3. next: (response) => {
  4. const fileContent: Uint8Array = new Uint8Array(response);
  5. const fileContentStr = new TextDecoder().decode(fileContent);
  6. const forwardSplit = filePath.split("/");
  7. const backwardSplit = forwardSplit[forwardSplit.length - 1].split("\\");
  8. let fileName = backwardSplit[backwardSplit.length - 1];
  9. const element = document.createElement("a");
  10. element.setAttribute(
  11. "href",
  12. "data:text/cvs;charset=utf-8," + encodeURIComponent(fileContentStr)
  13. );
  14. element.setAttribute("download", fileName);
  15. element.style.display = "none";
  16. document.body.appendChild(element);
  17. element.click();
  18. document.body.removeChild(element);
  19. },
  20. error: (err) => {
  21. console.log("err", err);
  22. },
  23. });
  24. }

All good so far. When I force the error, I get this as a response. This is what I need so I can display it properly.

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

However, this is what I see in the console logs from the downloadFile function.

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

Why am I not getting the response object and how do I fix it?

英文:

I have this API that throws an exception if the file size to be downloaded exceeds the max set.

  1. @GET
  2. @Path("files")
  3. public Response getFile(@QueryParam("filePath") String filePath) throws IOException {
  4. File file = new File(filePath);
  5. long fileSizeKb = Math.max(file.length() / 1024, 1);
  6. if (fileSizeKb > APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB) {
  7. String msg = MessageBundle.getMessage(MessageKeys.ERR_FILE_SIZE_EXCEEDED, String.valueOf(fileSizeKb),
  8. String.valueOf(APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB));
  9. System.out.println("msg: " + msg);
  10. throw new GeneralException(msg,
  11. new FileSizeLimitExceededException(msg, fileSizeKb, APIServerProperties.MAX_DOWNLOADABLE_FILE_SIZE_KB));
  12. }
  13. // Read file content into byte array
  14. byte[] fileBytes = readFileContent(file);
  15. return Response.ok(fileBytes).header("Content-Disposition", "attachment; filename=" + file.getName()).build();
  16. }

It is being called by this angular function:

  1. downloadFile(filePath: string) {
  2. this.migrationService.getFile(filePath).subscribe({
  3. next: (response) => {
  4. const fileContent: Uint8Array = new Uint8Array(response);
  5. const fileContentStr = new TextDecoder().decode(fileContent);
  6. const forwardSplit = filePath.split("/");
  7. const backwardSplit = forwardSplit[forwardSplit.length - 1].split("\\");
  8. let fileName = backwardSplit[backwardSplit.length - 1];
  9. const element = document.createElement("a");
  10. element.setAttribute(
  11. "href",
  12. "data:text/cvs;charset=utf-8," + encodeURIComponent(fileContentStr)
  13. );
  14. element.setAttribute("download", fileName);
  15. element.style.display = "none";
  16. document.body.appendChild(element);
  17. element.click();
  18. document.body.removeChild(element);
  19. },
  20. error: (err) => {
  21. console.log("err", err);
  22. },
  23. });
  24. }

All good so far. When I force the error, I get this as a response. This is what I need so I can display it properly.
为什么我的响应错误与错误拦截器捕获的错误不同?

However, this is what I see in the console logs from the downloadFile function.
为什么我的响应错误与错误拦截器捕获的错误不同?

Why am I not getting the response object and how do I fix it?

答案1

得分: 0

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

  1. downloadFile(filePath: string) {
  2. this.migrationService.getFile(filePath).subscribe({
  3. next: (response) => {
  4. const fileContent: Uint8Array = new Uint8Array(response);
  5. const fileContentStr = new TextDecoder().decode(fileContent);
  6. const forwardSplit = filePath.split("/");
  7. const backwardSplit = forwardSplit[forwardSplit.length - 1].split("\\");
  8. let fileName = backwardSplit[backwardSplit.length - 1];
  9. const element = document.createElement("a");
  10. element.setAttribute(
  11. "href",
  12. "data:text/cvs;charset=utf-8," + encodeURIComponent(fileContentStr)
  13. );
  14. element.setAttribute("download", fileName);
  15. element.style.display = "none";
  16. document.body.appendChild(element);
  17. element.click();
  18. document.body.removeChild(element);
  19. },
  20. error: (err) => {
  21. const buffer = new Uint8Array(err.error);
  22. const bufferStr = new TextDecoder().decode(buffer);
  23. const errorResponse = JSON.parse(bufferStr);
  24. const config = new MatDialogConfig();
  25. this.dialogRef = this.dialog.open(ErrorMessageComponent, config);
  26. this.dialogRef.componentInstance.msgObj = {
  27. errorMessage: errorResponse.statusText,
  28. };
  29. },
  30. });
  31. }
英文:

Saw this question which is quite similar to the problem I was having. It gave me an idea of what I had to do.

I updated my downloadFile function's error handling. I processed the arrayBuffer similar to when I am reading the file content. This time, passing in the error object.

  1. downloadFile(filePath: string) {
  2. this.migrationService.getFile(filePath).subscribe({
  3. next: (response) => {
  4. const fileContent: Uint8Array = new Uint8Array(response);
  5. const fileContentStr = new TextDecoder().decode(fileContent);
  6. const forwardSplit = filePath.split("/");
  7. const backwardSplit = forwardSplit[forwardSplit.length - 1].split("\\");
  8. let fileName = backwardSplit[backwardSplit.length - 1];
  9. const element = document.createElement("a");
  10. element.setAttribute(
  11. "href",
  12. "data:text/cvs;charset=utf-8," + encodeURIComponent(fileContentStr)
  13. );
  14. element.setAttribute("download", fileName);
  15. element.style.display = "none";
  16. document.body.appendChild(element);
  17. element.click();
  18. document.body.removeChild(element);
  19. },
  20. error: (err) => {
  21. const buffer = new Uint8Array(err.error);
  22. const bufferStr = new TextDecoder().decode(buffer);
  23. const errorResponse = JSON.parse(bufferStr);
  24. const config = new MatDialogConfig();
  25. this.dialogRef = this.dialog.open(ErrorMessageComponent, config);
  26. this.dialogRef.componentInstance.msgObj = {
  27. errorMessage: errorResponse.statusText,
  28. };
  29. },
  30. });
  31. }

huangapple
  • 本文由 发表于 2023年6月15日 13:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定