I am using Primefaces documentViewer from primefaces-extensions 12 as follows :
<pe:documentViewer zoom="page-fit" pagemode="thumbs"
id="iframeViewPdf" width="80%" height="800"
url="/pdfServlet?id=#{param.id}"/>
My controller writes the bytes to the response as follows :
@RequestMapping(value = "/pdfServlet")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
// code here
try{
documentId = request.getParameter("id");
inputStream = myRepository.getFileAsInputStream(documentId);
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
}catch(Exception e){
// all below didn't work
// request.getRequestDispatcher(request.getContextPath() + "/error").forward(request, response);
// response.sendRedirect(request.getContextPath() + "/error");
// throw e;
// what should be done here to redirect to error page / show error message in the page
}finally{
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
Above code works fine, but when any exception occurs in the backend I want to handle it in the client side, I tried to throw the exception in the catch part and got no effect.
Note: my application has error page configuration for any exception to forward to error page.
I also tried to redirect/forward to error page in the catch part, but I always gets an exception that cannot forward/redirect after response is committed.
How can I better handle exception in this case?
UPDATE : below is the response I get in the viewer when loading exception occurs in the backend, is there's any way to call a javascript function instead ?