2

I have a GWT 2.4 web-application. I want to handle the HTTP error-codes in the following way: depending on the code send user to different GWT pages. Yes, I understand that GWT has only one dynamical page, so I want to use Places for this. I added the following lines to web.xml:

   <error-page>
        <error-code>404</error-code>
        <location>/index.html#error:404</location>
    </error-page>

I thought this should redirect user to my GWT app and show a view that corresponds to that place. But this doesn't work at all. A blank page is shown when I try to access resource which doesn't exist.

A place itself exists and can be reached via the ../index.html#error:404 URL without problems. So the problem is on the server side. May be the server cannot redirect me to the page which doesn't exist in fact?

So, how can I handle 404 and other errors with GWT?

KutaBeach
  • 1,445
  • 21
  • 43
  • You should elaborate on what sort of mechanism are you using for communication and when does the server error take place. – Shuky Capon Jul 30 '12 at 13:13
  • I want to handle two cases: 404 server error if user enters the wrong address MANUALLY, and a global error processing for all uncaught exceptions in GWT client side. – KutaBeach Jul 31 '12 at 08:40

2 Answers2

0

The error handling and redirect need to happen on the client side when it gets the data back from the server.

Are you using RequestBuilder or RequestFactory to handle that communication? If so you could detect a 404 on the RequestBuilder response status and use the placeController.goTo(new ErrorPlace(404)); (or however you have your places setup)

checketts
  • 14,167
  • 10
  • 53
  • 82
  • Thanks for reply, checketts! Thats exactly the way I do it. Bu there are two more cases I want to handle: 404 server error if user enters the wrong address MANUALLY, and a global error processing for all uncaught exceptions. – KutaBeach Jul 31 '12 at 08:38
  • You'll need a separate handler on the server side. – checketts Aug 01 '12 at 04:21
  • Also for Global exception handling you can do that on the server side. I'm using Spring WebMVC and the exception handling is working great since it sets the Http Status codes so if a user manually browses to the rest call they get the same behavior, and my subclass of RequestBuilder does the redirects on the client side. – checketts Aug 01 '12 at 04:24
  • >You'll need a separate handler on the server side - as far as I understand, 404 error is usually handled by sending user to error page using error-page tag in web.xml. I cannot get why it is not working in GWT. – KutaBeach Aug 03 '12 at 05:55
0

Are you using Google App Engine? If you are, you can not define your custom 404 handler. Take a look at the docs here:

https://developers.google.com/appengine/docs/java/config/webxml#Error_Handlers

Anyway, regardless of that, you need to define your /* (catch all) servlet mapping that gets executed after all other mappings above it fail to match. In that servlet you should redirect to your custom page.

<!-- ERROR 404 replacement -->
<servlet>
    <servlet-name>missingPageServlet</servlet-name>
    <servlet-class>com.myproject.MissingPageServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>missingPageServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Hope that helped.

Viktor Brešan
  • 5,293
  • 5
  • 33
  • 36