Just a little side note:
Filip is right, you can't use a Tapestry page for these, but that is probably 
the correct behavior as this is a different type of error (an exception to the 
rule if you like) which the server needs to handle by explicitly interrupting a 
request/response and generating a new one on the server in accordance with the 
error condition... For example a page not found has little to do with your 
Tapestry application, but the server is still being addressed and needs to 
respond. So you wouldn't want a Tapestry to handle these IMHO, but that doesn't 
stop tapestry apps from making use of them when required! (using 
response.sendError) 

To configure:
You would simply use a standard JSP / HTML error page, and wire it in your 
web.xml. I am using Tomcat and it works great... works on Jetty too. The only 
error page/s I can't get to override perfectly are 401 and 503, but I have 
found some workarounds for these. 

503 is a service unavailable/ under maintenance error and will occur when the 
server is running but the application is not, so wouldn't make sense using it 
in your app anyhow. I found Tomcat is a little deficient here, see: 
http://www.nabble.com/Error-Page-Question-td16153138.html (if anyone has solved 
this please share).For the 401 authentication error, I simply use a 403 
forbidden instead, but I can get away with this as I am using forms 
authentication. 

I had thought about putting some of this stuff on the wiki...  as yet I haven't 
because error handling can be very scenario specific. Anyway here is a short 
example of how to configure your server error page:

Add to web.xml:

<error-page> 
    <error-code>404</error-code>
   <location>/error404.jsp</location>  
</error-page>
        

error404.jsp: (Important - create this file in /webapp) and NOT in 
/webapp/WEB-INF
<%@ page isErrorPage="true" %>

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
<head>
        <title>Error 404 Page Not Found</title>
</head>
<body>
<h1>Page Not Found</h1><br/><br/>
<div>
 We apologise for any inconvenience, but the resource you have
        specified cannot be found, please check the URL is typed correctly. 
<br/><br/>
        If you require assistance please contact client support. To continue <a 
href="JavaScript:history.go(-1)">return to the previous page</a>.<br/><br/>
</div>
</body>
</html>

As simple as that...
Cheers,
Peter.

----- Original Message -----
From: "Filip S. Adamsen" <[EMAIL PROTECTED]>
To: "Tapestry users" <users@tapestry.apache.org>
Sent: Sunday, 18 May, 2008 1:39:31 AM GMT +02:00 Athens, Beirut, Bucharest, 
Istanbul
Subject: Re: T5: Exception Handling

Hi Leon,

How to override server error pages depends on what server you're using. 
As far as I'm aware it's unfortunately not possible to use Tapestry 
pages as server error pages - if I'm wrong here, I'd really like to know 
how to do it. :)

What I mean by turning the RequestExceptionHandler into a pipeline is 
that I've overridden the original service with my own that uses a pipeline.

http://tapestry.apache.org/tapestry5/tapestry-ioc/pipeline.html

So I define my filter:

   public interface RequestExceptionFilter {

     void handleRequestException(Throwable exception, 
RequestExceptionHandler handler)
         throws IOException;
   }

Then I create a new RequestExceptionHandler as a pipeline:

   public static RequestExceptionHandler 
buildImprovedRequestExceptionHandler(
       List<RequestExceptionFilter> configuration,
       @InjectService("RequestExceptionHandler") RequestExceptionHandler 
requestExceptionHandler,
       PipelineBuilder builder,
       Logger logger
   ) {
     return builder.build(
         logger,
         RequestExceptionHandler.class,
         RequestExceptionFilter.class,
         configuration,
         requestExceptionHandler
     );
   }

I then contribute this to AliasOverrides to replace the default 
RequestExceptionHandler:

   public static void contributeAliasOverrides(
       Configuration<AliasContribution> configuration,
       @InjectService("ImprovedRequestExceptionHandler") 
RequestExceptionHandler requestExceptionHandler
   ) {
 
configuration.add(AliasContribution.create(RequestExceptionHandler.class, 
requestExceptionHandler));
   }

I can then contribute filters to the pipeline like this:

   public static void contributeImprovedRequestExceptionHandler(
       OrderedConfiguration<RequestExceptionFilter> configuration,
       RequestExceptionErrorFilter requestExceptionErrorFilter
   ) {
     configuration.add("Error", requestExceptionErrorFilter);
   }

Where RequestExceptionErrorFilter looks like this:

   public class RequestExceptionErrorFilter
       implements RequestExceptionFilter {

     private final RedirectService redirectService;
     private final SecurityService securityService;
     private final Logger logger;

     public RequestExceptionErrorFilter(RedirectService redirectService, 
SecurityService securityService, Logger logger) {
       this.redirectService = redirectService;
       this.securityService = securityService;
       this.logger = logger;
     }

     public void handleRequestException(Throwable exception, 
RequestExceptionHandler handler)
         throws IOException {
       if (securityService.isProductionModeEnabled()) {
         logger.error("Exception during request", exception);
         redirectService.sendRedirect(ErrorIndex.class, false);
       }
       else {
         handler.handleRequestException(exception);
       }
     }
   }

RedirectService and SecurityService are my own classes. They just make 
some things easier for me. As you can see I show an error page when I'm 
in production but invoke the next filter in the pipeline if I'm not. You 
could contribute more filters if needed.

I've been wanting to contribute a strategy filter before this one 
allowing to do different things depending on the type of exception 
thrown, but haven't had the time nor need to do so yet.

http://tapestry.apache.org/tapestry5/tapestry-ioc/strategy.html

Anyhow, hope this helps - if not, you know where to ask. :)

-Filip

On 2008-05-16 17:37, Leon Derks wrote:
> Thanks Peter,
> 
> Your original question in the post is also what I would like to know.
> 
> Do you now know how to override server error pages(404, 505 etc)?
> And I don't understand what Flip means with :I've turned the 
> RequestExceptionHandler service into a pipeline.
> 
> Can you show me some code of how to do this?
> Leon
> 
> 
> Peter Stavrinides wrote:
>> Hi Leon
>>
>> I posted a number of questions to the list about this topic, but got 
>> only a few perls. One of them was this post, particularly Filip's answer:
>> http://www.mail-archive.com/users@tapestry.apache.org/msg21914.html
>>
>> Also see this page, for how to override Tapestry's error page with 
>> your own friendly error page:
>> http://wiki.apache.org/tapestry/Tapestry5ExceptionPage
>>
>> I am not sure what you are trying to do, but would suggest you 
>> familiarize yourself a bit more with Tapestry's error reporting 
>> mechanisms, which imho are incredibly powerful and easy to use.
>>
>> Hope this helps,
>> Peter
>>
>>
>> ----- Original Message -----
>> From: "Leon Derks" <[EMAIL PROTECTED]>
>> To: "Tapestry users" <users@tapestry.apache.org>
>> Sent: Friday, 16 May, 2008 1:35:19 PM GMT +02:00 Athens, Beirut, 
>> Bucharest, Istanbul
>> Subject: T5: Exception Handling
>>
>> Hello
>>
>> What is the best way to handle exceptions in Tapestry 5?
>>
>> I want to show some kind of general error page, when an exception occurs.
>>
>> At the moment I have a onException method in my BasePage class. But 
>> for some reason the Error page is not returned.
>> What is the way in T5 to handle exceptions?
>>
>> Object onException(Throwable cause) {
>>         return new Error();
>>     }
>>
>> Leon
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>>
>>   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to