1

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

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

2 Answers2

1

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);  
                
            }
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
0

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().

Melloware
  • 10,435
  • 2
  • 32
  • 62
  • unfortunately the above doesn't work, it works only if you try to access the servlet/controller directly as get request from the browser : it redirects to error page, but when accessing the servlet/controller from url attribute in the component nothing happens except I get error message in the viewer : Unexpected server response – Mahmoud Saleh Oct 31 '22 at 07:30
  • please see my updated question, I added an image – Mahmoud Saleh Oct 31 '22 at 07:37