• Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Java and Wicket web development thoughts, tutorials, and tips

roman@coderdreams.com
Coder DreamsCoder Dreams
  • Fat Cats Boardgame
  • Wicket UI Library
  • About Me

How to catch uncaught exceptions in Wicket

August 1, 2019 Posted by Roman Sery debugging, wicket No Comments

The problem

In any production Wicket application, there are countless exceptions that are thrown which lead to error pages. Some are very difficult to debug, or even reproduce, due to lack of information. You normally wouldn’t be handling these kind of exceptions yourself, so wouldn’t it be nice to somehow report all these? Some common exceptions every Wicket developer is probably sick of are:

org.apache.wicket.MarkupContainer  - Unable to find component with id 'XXX' in [Page class = YYY, id = 0, render count = 1]

IllegalArgumentException - Cannot update component that does not have setOutputMarkupId property set to true. Component: blah

Tried of struggling to reproduce bugs reported by users? Wouldn’t it be nice to see the past N actions taken by the user before getting the error page? We can accomplish all of this by implementing Wicket request logging and using our own custom AbstractRequestLogger.

The solution

Creating a custom AbstractRequestLogger

The first step is to modify our Wicket’s WebApplication init() method to enable request logging, and to set our own custom request logger that will use an EvictingQueue to store the past N request URL’s in session.

@Override
    public void init() {
    	super.init();
    
        getRequestLoggerSettings().setRequestLoggerEnabled(true);
        getRequestLoggerSettings().setRequestsWindowSize(5); //set # of requests to store
    }

	@Override
	protected IRequestLogger newRequestLogger() {
		return new CustomRequestLogger();
	}

Here we have set the requestsWindowSize to 5. This means that our error log will also contain the previous 5 requests by the user.

You can see the full implementation of CustomRequestLogger here.

Now, to make sure that we capture all exceptions thrown by Wicket, we also need to set a custom IRequestCycleListener in our init() method:

getRequestCycleListeners().add(new IRequestCycleListener() {
			@Override
			public IRequestHandler onException(RequestCycle cycle, Exception ex) {
				//do something with ex                
				if(ex instanceof ListenerInvocationNotAllowedException || ex instanceof ComponentNotFoundException) {
					//if this is an ajax request, just return an empty response to avoid sending user to error page
					return EmptyAjaxRequestHandler.getInstance();
				}
				return null;
			}
        });

In the onException() method, you can now do whatever you need to do with the exception.

A good approach would be to save the error and stack trace to a database table, as well as send an email to developers so it can be quickly addressed. They will receive a nicely formatted email with environment information, the logged in user id, URL, stack trace, and the requests leading up to the exception.

They can then happily go about fixing it with a smile on their face, well maybe 馃檪

You can checkout a full working example of this here!

No Comments
Share
28

About Roman Sery

I've been a software developer for over 10 years and still loving Java!

You also might be interested in

How to use Wicket component events

How to use Wicket component events

Feb 23, 2020

One of the most powerful features of Wicket, component events, let's you build complex yet decoupled structures.

Dynamically add Wicket panels

Aug 8, 2019

How to dynamically choose the implementation of Wicket panels when adding them to your page using reflection and the factory pattern.

Basics of Wicket AJAX requests

Oct 17, 2019

Wicket is a very server-side focused framework, so working with ajax requests is quite different from what you might be used. This will show you how to get started quickly!

Categories

  • aws
  • customization
  • database
  • debugging
  • enum
  • java
  • models
  • performance
  • projects
  • react
  • software design
  • Spring
  • tool
  • Uncategorized
  • wicket

Recent Posts

  • Rent Day
  • Self-contained Wicket Fragments
  • Pros and cons of unit testing
  • Themeable React Monopoly board
  • Please dont use client-specific release branches

Recent Comments

  • TCI Express Thanks for sharing such insightful information. TCI Express truly stands out as the best air logistics company, offering fast, secure, and efficient air express and cold chain transportation services....

    Tracking down a bug in production Wicket application 路  March 25, 2025

  • Tom Error: A zip file cannot include itself Can you please correct the plugin part so it doesn't use the same folder as input?

    Deploying Spring Boot app to AWS Beanstalk with Nginx customization 路  September 3, 2021

  • Golfman: Reality always wins I've used both Wicket and front-end JS frameworks and, having worked extensively on both, I can tell you that "Speed of development" is definitely NOT with the JS frameworks. You basically end up...

    Five reasons you should use Apache Wicket 路  August 29, 2021

  • Kiriller Sorry can not agree with you, wicket might be a well built technical framework. But the advantages of using a front-end framework like react.js and vue.js can not be beaten by Wicket nowadays. - Speed...

    Five reasons you should use Apache Wicket 路  August 23, 2021

  • Bernd Lauert Sorry but i have to refute your claims with the following arguments: 1. the Wicket community may be small but it is also very responsive, you usually get a helpful answer from the core devs on the...

    Five reasons you should use Apache Wicket 路  July 1, 2021

Archives

  • May 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019

Contact Me

Send Message
Prev Next