Search in sources :

Example 1 with ErrorPage

use of lucee.runtime.err.ErrorPage in project Lucee by lucee.

the class PageContextImpl method handlePageException.

public void handlePageException(PageException pe, boolean setHeader) {
    if (!Abort.isSilentAbort(pe)) {
        // if(requestTimeoutException!=null)
        // pe=Caster.toPageException(requestTimeoutException);
        int statusCode = getStatusCode(pe);
        // prepare response
        if (rsp != null) {
            // content-type
            Charset cs = ReqRspUtil.getCharacterEncoding(this, rsp);
            if (cs == null)
                ReqRspUtil.setContentType(rsp, "text/html");
            else
                ReqRspUtil.setContentType(rsp, "text/html; charset=" + cs.name());
            // expose error message in header
            if (rsp != null && pe.getExposeMessage())
                rsp.setHeader("exception-message", StringUtil.emptyIfNull(pe.getMessage()).replace('\n', ' '));
            // status code
            if (getConfig().getErrorStatusCode())
                rsp.setStatus(statusCode);
        }
        ErrorPage ep = errorPagePool.getErrorPage(pe, ErrorPageImpl.TYPE_EXCEPTION);
        // ExceptionHandler.printStackTrace(this,pe);
        ExceptionHandler.log(getConfig(), pe);
        // error page exception
        if (ep != null) {
            try {
                Struct sct = pe.getErrorBlock(this, ep);
                variablesScope().setEL(KeyConstants._error, sct);
                variablesScope().setEL(KeyConstants._cferror, sct);
                doInclude(new PageSource[] { ep.getTemplate() }, false);
                return;
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
                if (Abort.isSilentAbort(t))
                    return;
                pe = Caster.toPageException(t);
            }
        }
        // error page request
        ep = errorPagePool.getErrorPage(pe, ErrorPageImpl.TYPE_REQUEST);
        if (ep != null) {
            PageSource ps = ep.getTemplate();
            if (ps.physcalExists()) {
                Resource res = ps.getResource();
                try {
                    String content = IOUtil.toString(res, getConfig().getTemplateCharset());
                    Struct sct = pe.getErrorBlock(this, ep);
                    java.util.Iterator<Entry<Key, Object>> it = sct.entryIterator();
                    Entry<Key, Object> e;
                    String v;
                    while (it.hasNext()) {
                        e = it.next();
                        v = Caster.toString(e.getValue(), null);
                        if (v != null)
                            content = repl(content, e.getKey().getString(), v);
                    }
                    write(content);
                    return;
                } catch (Throwable t) {
                    ExceptionUtil.rethrowIfNecessary(t);
                    pe = Caster.toPageException(t);
                }
            } else
                pe = new ApplicationException("The error page template for type request only works if the actual source file also exists. If the exception file is in an " + Constants.NAME + " archive (.lar), you need to use type exception instead.");
        }
        try {
            String template = getConfig().getErrorTemplate(statusCode);
            if (!StringUtil.isEmpty(template)) {
                try {
                    Struct catchBlock = pe.getCatchBlock(getConfig());
                    variablesScope().setEL(KeyConstants._cfcatch, catchBlock);
                    variablesScope().setEL(KeyConstants._catch, catchBlock);
                    doInclude(template, false);
                    return;
                } catch (PageException e) {
                    pe = e;
                }
            }
            if (!Abort.isSilentAbort(pe))
                forceWrite(getConfig().getDefaultDumpWriter(DumpWriter.DEFAULT_RICH).toString(this, pe.toDumpData(this, 9999, DumpUtil.toDumpProperties()), true));
        } catch (Exception e) {
        }
    }
}
Also used : PageException(lucee.runtime.exp.PageException) ErrorPage(lucee.runtime.err.ErrorPage) Resource(lucee.commons.io.res.Resource) Charset(java.nio.charset.Charset) PageException(lucee.runtime.exp.PageException) CasterException(lucee.runtime.exp.CasterException) MissingIncludeException(lucee.runtime.exp.MissingIncludeException) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) CacheException(lucee.commons.io.cache.exp.CacheException) DatabaseException(lucee.runtime.exp.DatabaseException) ServletException(javax.servlet.ServletException) ModernAppListenerException(lucee.runtime.listener.ModernAppListenerException) RequestTimeoutException(lucee.runtime.exp.RequestTimeoutException) ExpressionException(lucee.runtime.exp.ExpressionException) ApplicationException(lucee.runtime.exp.ApplicationException) Struct(lucee.runtime.type.Struct) Entry(java.util.Map.Entry) ApplicationException(lucee.runtime.exp.ApplicationException) Key(lucee.runtime.type.Collection.Key)

Aggregations

IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 Entry (java.util.Map.Entry)1 ServletException (javax.servlet.ServletException)1 JspException (javax.servlet.jsp.JspException)1 CacheException (lucee.commons.io.cache.exp.CacheException)1 Resource (lucee.commons.io.res.Resource)1 ErrorPage (lucee.runtime.err.ErrorPage)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 CasterException (lucee.runtime.exp.CasterException)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 ExpressionException (lucee.runtime.exp.ExpressionException)1 MissingIncludeException (lucee.runtime.exp.MissingIncludeException)1 PageException (lucee.runtime.exp.PageException)1 RequestTimeoutException (lucee.runtime.exp.RequestTimeoutException)1 ModernAppListenerException (lucee.runtime.listener.ModernAppListenerException)1 Key (lucee.runtime.type.Collection.Key)1 Struct (lucee.runtime.type.Struct)1