What is the correct way to handle exceptions in a REST API that produces an Excel or PDF file as response?

vaisakh

How do we handle exceptions or errors in a REST API which produces a file for download? I have an API written using Jersey which produces an Excel file, and it has the appropriate annotation as:

@Produces("application/vnd.ms-excel")

When everything works as expected, I am building a response with the file, and with status as Status.OK.

However, what is the correct way to build a response when an exception occurs? What should be the response header, and would the @Produces annotation cause a problem(as it mentions an Excel file, but the error response would most likely be JSON)?

Code snippet for reference:

@GET
@Path("{report}")
@Produces("application/vnd.ms-excel")
public Response generateReport(@PathParam("report") String reportName /* other query params */) {
    boolean isValid = false;
    File file = null;
    try {
        /*
         Logic to generate the excel file and return info about the generated report
        */
         /* Includes code that throws IllegalArgumentException */


    } catch(IllegalArgumentException e) {
        isValid = false;
        status = Status.BAD_REQUEST;
    } catch(Exception e) {//Quick and dirty testing for the API
        isValid = false;
        status = Status.BAD_REQUEST;
    }


    ResponseBuilder response = null;

    if(isValid) {
        response = Response.ok((Object) file);  
        response.header("Content-Disposition","attachment; filename=\"test.xlsx\"");  
    } else {
        response = Response.status(status);  
        // is this enough, or do we add info in the header here as well?
    }
    return response.build();  
}
Thomas

As per request, my comment as an answer :)

Here's an article on exception handling in JaxRS: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/en/part1/chapter7/exception_handling.html

This indicates that you should be able to register a custom ExceptionMapper that handles the response for exceptions in the way you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is a good way to handle exceptions when trying to read a file in python?

What is correct way to handle form submission in beego?

Correct way to handle exceptions in Spring Boot

What is a good way to return a partial success response for a REST API?

The correct way to handle Exceptions in a Python2.7 context manager class

Correct way to place and handle .json file in Xcode

What is preferable way to handle exceptions?

Correct way to handle exceptions in Java

What is correct way to handle fetch response

What is the correct way to handle a null response from an asynchronous function?

What is the cleanest/correct way to handle Future.get() custom exceptions inside callable? (Advanced)

More efficient way to handle an extremely specific response from a Rest API in JavaScript

Correct way to use Rest API

Correct way to handle deprecated API

what is Correct Exception to raise in a rest API in response to request to delete a resource without enough permission?

What is the correct way to post and save stream response to file using Flurl

What is the correct way to handle HTTP response in React Native component

What is the best way to handle exceptions in this scenario

What is the proper way to load images in Flutter and handle exceptions on load

What is the cleaner way to handle exceptions

What is the correct way to handle stale NSURL bookmarks?

What is the correct way of returning a file in a Rest webservice request?

Recommended way to handle exceptions?

ADAL for Android - What is the correct way to handle errors?

PHP - What is the correct way to handle this strange array?

Correct REST API Response HTTP Status Code

What is the best way to handle a JSON API response which returns 200 regardless of error or not

Python: Correct way to handle chain exceptions

What is the correct way to implement '...rest' in a TSX component?