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();
}
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.
Comments