5

How to handle error with primefaces filedownload

<p:fileDownload value="#{testBean.file}" /> 

TestBean.java

    public StreamedContent getFile() {  
    if(selectedReport ==null){
        FacesContext.getCurrentInstance().addMessage(.."Please select a file");
        return null;//What to do here
    }
    InputStream inps =  ReportGenerator.getPDF(selectedReport);
    return new DefaultStreamedContent(inps, "application/pdf", "report.pdf"); 
    }
yodhevauhe
  • 765
  • 3
  • 11
  • 33
  • In this SO question it is described how to handle the problem of error handling and downloads with OmniFaces and PrimeFaces: [Conditionally provide either file download or show export validation error message](https://stackoverflow.com/q/32591795). However the solution does not make use of ``. Here is another solution not using any third party library: [JSF file download exception handling](https://stackoverflow.com/a/37520406) – Filou Feb 19 '19 at 16:32

3 Answers3

3

This helped http://forum.primefaces.org/viewtopic.php?f=3&t=8263

<p:commandButton    ajax="false"
                    value="Download Detailed Report"
                    actionListener="#{reportBean.generateDetailedReport}">

    <p:fileDownload value="#{reportBean.detailedReport}"/>

</p:commandButton>
public void generateDetailedReport(ActionEvent ae) {

    ......

    reportStream = ExcelReport.generate(reportList);

    if (reportStream == null) {

        FacesUtil.addError("Error generating report");

        throw new AbortProcessingException();
    }
}

public StreamedContent getDetailedReport() {

    return new DefaultStreamedContent(reportStream, "application/xls", "report.xls"); 
}
Laurent.B
  • 213
  • 2
  • 14
yodhevauhe
  • 765
  • 3
  • 11
  • 33
  • The link is dead. I believe this is the new address: http://forum.primefaces.org/viewtopic.php?f=3&t=8263 – douglaslps Feb 24 '15 at 10:56
  • It is important to exactly throw the `javax.faces.event.AbortProcessingException`. Any other exception will lead to different results. – Filou Apr 14 '22 at 08:04
0

If you mean handle error by show an html message to the user, then you should put the code that can throw an exception inside a try...catch block, and the catch block changes the content type back to text/html and display the error in the way you want.

But since the content type is added in the DefaultStreamedContent, maybe only a redirection can solve this.

bluefoot
  • 10,220
  • 11
  • 43
  • 56
-2

Better to use obtain all exception in one like ;

} catch (Exception e) {
        handleError(e);
    }

handleError method ;

private void handleError (Throwable t) {

    try {
        throw t;
    } catch (AccessException ae) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (SQLException sqle) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (ConnectException ce) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (RemoteException re) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (NotBoundException nbe) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (IOException ioe) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (IllegalArgumentException iae) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (Throwable tt) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    }

    FacesUtil.completeResponse();
}
newuserua
  • 299
  • 2
  • 6