Primefaces Document Viewer Exception Handling

254 Views Asked by At

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 ?

enter image description here

2

There are 2 best solutions below

2
Melloware On

Why not put a custom Error in your web.xml???

web.xml

    <!-- Handle Invalid PDF errors gracefully -->
    <error-page>
        <exception-type>com.your.InvalidPdfException</exception-type>
        <location>/pages/error.xhtml</location>
    </error-page>

Then in your code throw new InvalidPdfException().

0
Mahmoud Saleh On

I was able to handle the error using javascript interval on load as follows :

window.onload = function(){
                        
                PF('statusDialog').show();
                 var checkExist = setInterval(function() {
                        var iframe=document.getElementsByTagName('iframe')[0];
                        var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
                        var viewer = innerDoc.getElementById('viewer');
                        var errorMessage = innerDoc.getElementById('errorMessage');
                        var innerHTML = viewer.innerHTML;
                        
                        if(innerHTML != null &amp;&amp; innerHTML!='' &amp;&amp; innerHTML!='undefined'){                                       
                            clearInterval(checkExist);
                            PF('statusDialog').hide();
                        }
                        
                        if(errorMessage != null &amp;&amp; errorMessage.innerText != ''){                                       
                            clearInterval(checkExist);
                            PF('statusDialog').hide();
                            // handle error here
                            window.location.href = getContextPath()+'/error';
                        }
                  }, 1000);  
                
            }