tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Tapestry Wiki] Update of "Tapestry5HowToSendErrorPageAsEmail" by WulfWechsung
Date Sat, 22 Oct 2011 20:21:01 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The "Tapestry5HowToSendErrorPageAsEmail" page has been changed by WulfWechsung:

New page:

Like the extensive and verbose error output of tapestry development mode? Don't want to scare
your users away with the page? Want to be notified when your users run into trouble?

=== App Module ==
add this to  to your app module:
     public static void contributeApplicationDefaults(
            MappedConfiguration<String, String> configuration)

        configuration.add(USER_FRIENDLY_EXCEPTION_PAGE, "exception/Errors");
        configuration.add(FULL_EXCEPTION_REPORT_PAGE, "exception/ExceptionReport");

 public RequestExceptionHandler buildAppRequestExceptionHandler(
            @Inject final ResponseRenderer responseRender,
            @Inject final PageDocumentGenerator generator,
            @Inject final ComponentSource componentSource,
            final @Symbol(AppModule.EMAILSERVER) String emailserver,
            final @Symbol(SymbolConstants.PRODUCTION_MODE) boolean productionMode,
            final @Symbol(AppModule.FULL_EXCEPTION_REPORT_PAGE) String exceptionPage,
            final @Symbol(AppModule.USER_FRIENDLY_EXCEPTION_PAGE) String userfriendly,
            final @Inject Response response)

        final String sender = // your sender, symbol etc
        final String rcpt = // recepient of the email 

        return new RequestExceptionHandler()
            public void handleRequestException(Throwable exception) throws IOException
                // always log to console
                logger.error("Unexpected runtime exception: " + exception.getMessage(), exception);
                // let clients know something is up

                // get the page that will render the full non-production mode output
                Component exceptionReport = componentSource.getPage(exceptionPage);
                ((ExceptionReporter) exceptionReport).reportException(exception);

                if (productionMode) {
                    try {

                        String errorTrackId = System.currentTimeMillis() + "";

                        Document v = generator.render(exceptionPage);
                        /** send email here **/
                        // this is a custom email builder so you'll have to substitute your
own email sending code
                        Email.from(sender).to(rcpt).withSubject("Error occured [tracking id=%s]",
                                .withContent("text/html", v.toString()).finish().sendVia(emailserver);
                        /** Show your custom, user friendly page **/
                        /** In the original scenario I would show the tracker-id on the userfriendly
error page
                        ((ErrorTracker) userfiendly).setErrorID(errorTrackId);
                        Component userfiendly = componentSource.getPage(userfriendly);

                    } catch (Throwable t) {
                        logger.error("Error in error handling. Ironic? Yes!", t);
                } else {
                    /** if in non-production we render the full exceptionPage right away **/

    public void contributeServiceOverride(MappedConfiguration<Class, Object>
            @Local RequestExceptionHandler handler) {

        configuration.add(RequestExceptionHandler.class, handler);


=== Exception Report ===

Unfortunately ExceptionReport ( the class that renders the exception pages per default) reads
the production-mode symbol itself so we have to copy it. 
It's in org.apache.tapestry5.corelib.pages.ExceptionReport

Copy it to your project and modify to always show the extensive report. I did that by stripping
out the productionMode property and the <t:if><p:else></t:if> referencing
it in the template.

The contributeApplicationDefaults above assumes you have copied it to exception/ExceptionReport
in your pages folder.

== Error Page ==

The contributeApplicationDefaults above assumes you have created a page that display user-friendly
errors ad exception/Errors in your pages folder.

To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org

View raw message