Random bits of information by a developer

08 December 2010

Seam Catch Alpha2

Over the weekend Seam Catch Alpha2 was released! I'm trying to do a two week timebox release cycle for Seam Catch. Alpha 2 contains some minor API changes (some additions, and a couple of renames), a new qualifier and a couple of new objects for better framework integration. The largest of these enhancements is the ability to support ServiceHandlers.

Qualifier changes

The @HandlesExceptions is no longer a qualifier, this was needed to support ServiceHandlers, and is simply a marker anyway. This shouldn't affect users.

We've added a new qualifier @CatchResource to help distinguish resources injected into handlers that are only for Catch use. Here's an example:

public void logToSpecialCatchLog(@Handles CaughtException<Throwable> e, @CatchResource Logger log)
{
    log.warn("Unexpected exception caught: " + e.getException().getMessage());
}
      
It doesn't do much, but this shows that the Logger instance being injected is a special log configured only for Catch. This qualifier can be applied to anything and helps the developer visually see what resources are really being used.

ServiceHandlers

This feature is a great feature that really makes exception handling easy. So far we've seen examples of creating handlers with full method implementations, if you're doing something specialized, but repetitive, such as a JAX-RS error response you end up writing the boiler plate code over and over again just to change a few things (the message and the status code). Now you can do this sort of thing with only writing the boiler plate part once, the rest is all declarative, take a look!
@HandlesExceptions
@ExceptionResponseService
public interface DeclarativeRestExceptionHandlers
{           
   @SendHttpResponse(status = 404, message = "Requested resource does not exist")
   void onNoResult(@Handles @RestRequest CaughtException<NoResultException> e);

   @SendHttpResponse(status = 403, message = "Access to resource denied (Annotation-configured response)")
   void onNoAccess(@Handles @RestRequest CaughtException<AccessControlException> e);

   @SendHttpResponse(status = 400, message = "Invalid identifier (Annotation-configured response)")
   void onInvalidIdentifier(@Handles @RestRequest CaughtException<IllegalArgumentException> e);
}
      
There are three handlers right there, nothing else (save the [ServiceHandler][] implemenation) is needed! These handlers all send an error response for a JAX-RS request. They all change the status code returned and the message. What does it take to implement this? Aside from the new annotation (@SendHttpResponse), there's two other classes that need to be written:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ServiceHandler(ExceptionResponseServiceHandler.class)
public @interface ExceptionResponseService
{               
} 

public class ExceptionResponseServiceHandler
{
   @Inject @CatchResource
   private Instance<ResponseBuilder> builderProvider;

   @AroundInvoke
   public Object processException(InvocationContext ctx)
   {
      Method m = ctx.getMethod();
      if (ctx.getParameters().length > 0 && ctx.getParameters()[0] instanceof CaughtException)
      {
         ResponseBuilder builder = builderProvider.get();
         CaughtException<?> c = (CaughtException<?>) ctx.getParameters()[0];
         if (m.isAnnotationPresent(SendHttpResponse.class))
         {
            SendHttpResponse r = m.getAnnotation(SendHttpResponse.class);
            String message = r.message();
            if (r.message().length() == 0 && r.passthru())
            {
               message = c.getException().getMessage();
            }

            builder.status(r.status());
            if (message != null && message.length() > 0)
            {
               builder.entity(new ErrorMessageWrapper(message));
            }
         }
         else
         {
            builder.entity(new ErrorMessageWrapper("Unknown error"));
         }
      }
      return Void.TYPE;
   }
}
Most of this is building up the error from the annotation, but it's only 26 LoC, and you quite a bit of reuse from them. That's all there is to it! Framework integrations should implement some of these for basic things, but you can further create them for yourself if you have some repetitive action that you can extract in your exception handling practices. Look for another release (Alpha3) in a couple of weeks and a Beta out by the end of the year!

24 November 2010

Seam Catch Release

Seam Catch Alpha1 was released yesterday! You can read about ithere, or the docs. Bits can be found in the JBossrepository using maven:
<dependency>
    <groupId>org.jboss.seam.catch</groupId>
    <artifactId>seam-catch-api</groupId>
    <version>3.0.0.Alpha1</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.jboss.seam.catch</groupId>
    <artifactId>seam-catch-impl</groupId>
    <version>3.0.0.Alpha1</version>
    <scope>runtime</scope>
</dependency>
Or at SourceForge. I'm very interested in any feedback that you may have.

What is Catch?

Catch is the exception handling infrastructure in Seam3. I like to refer to it as Next Generation Exception Handling. It really goes beyond what was available in Seam2 and offers a complete solution for Exception Handling. It's built on top of CDI events so the entry point is very minimal and usage is quite easy.

Usage

For those who are familiar with CDI events, creating your own handlers will be very familiar. Handlers are quite similar to CDI Observers, though there are a few differences:
  1. handlers must be contained in a bean marked by @HandlesExceptions
  2. the first parameter in the method must be annotated with @Handles and be an instance of CaughtException&lt;T extends Throwable>
  3. handlers are ordered before they're invoked
Keep those things in mind and the rest is very easy.

Creating a Handler

The docs really cover this very well, but here's a simple handler to get you started:
@HandlesExceptions
public class JsfHandlers {
    public void redirectingHandler(@Handles(precedence = -100) CaughtException<Throwable> event, NavigationHandler nav) {
        nav.handleNavigation(FacesContext.getCurrentInstance(), null, "/error.xhtml");
    }
}
NOTE: The best way to tie this handler into JSF is by adding a JSF to Catch bridge by creating an ExceptionHandler (this will be added to Catch in the next release, as a separate jar) to fire the ExceptionToCatchEvent.

There's not much there as you can see. We're saying this is a general handler (the CaughtException type is Throwable) and we want it run towards the end of the cause container traversal (precedence = -100). If you're not familiar with JSF, this is will navigate the user to the error page at the end of the exception handling. Of course this would require some extra JSF integration pieces to produce the NavigationHandler such as Seam Faces or Apache CODI, or your own producer. The NavigationHandler is injected via CDI, in fact, any additional parameters past the CaughtException param will be injected for you. Anything that you need for your exception handling, as long as CDI can create it, can be injected for you.

Entering Catch

Above I mentioned the ExceptionToCatchEvent. This event is all that's needed to start the handling process. Something as simple as
@Inject Event<ExceptionToCatchEvent> catchEvent;
...
try {
    your code
} catch (Exception e) {
    catchEvent.fire(new ExceptionToCatchEvent(e));
}
will get the ball rolling.

That's really all there is to it! I'm looking forward to any feedback you may have, and bugs if you find them. Bugs should be filled in JIRA. In the next release look for bridges for JSF, JAX-RS and others, and the ability to filter stack traces!

28 February 2010

Seam In Depth: Exception Handling Part II

I would like to thank those who provided feedback for Part I of this exception handling series. A special thanks goes out to Dan Allen and Dan Hinojosa for the wonderful constructive feedback. I'm sure over time these posts will improve and become better.

This entry was originally going to be talking about custom handlers and provide some code; however, due to family and work factors I have not been able to finish what I wanted for this. Instead we'll be diving even further into the built-in handlers and providing examples. At the end I'll also give a sneak peek of the exception handling framework that will make up Part III of this series, hopefully complete in March. To cut down on the length of this post I've linked to Seam's Fisheye install of the 2.2.0.GA release. With that, let the journey into org.jboss.seam.exception begin!

Exceptions

This class (org.jboss.seam.exception.Exceptions) is a built in Seam class, all you need to do is obtain a reference to it (either via injection @In("exceptions") Exceptions or Exceptions.getInstance()) and call the getHandlers() method to add your own exception handlers. Typical interaction is done by adding exception handlers via the annotations or pages.xml. Let's begin the exploration with the initialize() method where all the initial setup and creation of the built-in handlers happen. Handlers are added in the following order:
  1. AnnotationRedirectHandler
  2. AnnotationErrorHandler
  3. Handlers declared in pages.xml
  4. User defined custom handlers (more on this later)
At first glance, it appears that an old deprecated way of adding exceptions is first added, then the exception handlers added via page(s).xml; however, this is not the case as each of those ExceptionHandlers is added to a list, then added at the end. Just before any handlers that the user specifies with pages.xml or custom handlers the Seam Debug Page is added.
It's important to know that while in debug (typically only development mode) your annotated exceptions will take precedence over the Seam Debug Page, and nothing after the debug page will be triggered (the debug page is a global catch all handler).

The parsing of the XML files happens in the parse() method which does some basic XML parsing looking for the exception elements. The class and logging level (if configured) are pulled from the exception element, and a new ConfigRedirectHandler or ConfigErrorHandler is created (more on these handlers later). If there is no class attribute specified then a catch-all handler, that handles Exception.class is created.

The private createHandler() method does the work of pulling additional information about the configured handler such as

  • ending conversations
  • redirecting
  • adding FacesMessages
  • finding the http error code, etc.
The correct handler is then created and returned to be added into the list.

The only other part of this class that has any importance for the developer/user is the handle() method, which is really just an iterator over the handlers looking for a handler for the exception which was thrown. Though there is a catch: Nested exceptions are unwrapped and handled from the bottom up (though in the Seam Debug Page the exception is recreated so you may need to scroll to the bottom to actually find the real exception). If a long running conversation is active then the "org.jboss.seam.handledException" object, which is the exception object, is added to the conversation context. If logging is configured for the exception handler a log message is added then two events are raised (regardless of logging): "org.jboss.seam.exceptionHandled." + cause.getClass().getName() and org.jboss.seam.exceptionHandled allowing a further user extension point if so desired. If no handler is found then an event is raised ("org.jboss.seam.exceptionNotHandled") and the exception is rethrown. Even if this event is observed, (the exception is passed as a parameter to the observer's method) it will not be able to influence the exception being rethrown.

Any handlers the developer creates and adds to the Exceptions list will be added at the end of the list (unless the list is manually modified and rearranged, which is allowed). Depending on the specific needs of the application this is a great place to add a general catch all handler for the application (unless a general handler in pages.xml is sufficient). When adding custom handlers or any handler really, it is important not to create multiple handlers for the same exception as the first in the list will be called and the list of handlers will terminate with that handler.

AnnotationErrorHandler and ConfigErrorHandler

ErrorHandlers (AnnotationErrorHandler and ConfigErrorHandler) are a specific type of ExceptionHandler, they're used to send an HTTP Error code to the browser, probably more useful when combined with Seam's Web Service or REST integration. There is not a lot of code to these handlers, in fact, they are only data containers used by the parent class ErrorHandler which handles sending the error to the browser. However, they are great examples of reuse for exception handling, and make for good templates to follow in abstracting logic in custom handlers. The two sub classes hold
  • the message (if configured)
  • the error code number
  • and the flag to end the currently conversation.
All of the logic to make it happen is in the handle() method.

AnnotationRedirectHandler and ConfigRedirectHandler

The redirect handlers (AnnotationRedirectHandler and ConfigRedirectHandler) are in the same category as the error handlers: there's not much to them, and most of the interesting code is in the parent class RedirectHandler. The first bit of code in the handle() method is finding the viewId if none was specified in the handler instance, but it really isn't related to the actual exception handling. Next is the addition of a defined FacesMessage, ending the conversation (again, if configured), and finally the redirect.

DebugPageHandler

This is a great handler (source) along with the associated code in the jboss-seam-debug.jar to fully understand the potential of exception handlers in Seam. The handler is really the entry point into the debug system in Seam. Java code wise, there's not much to it, it's a redirect handler that sets some additional parameters for the redirect. The whole process of Seam Debug really could be it's own blog entry or series of entries. It's pretty involved: it includes a PhaseListener, a SerializationProvider, and some custom Introspection. Because it's such a large, involved piece of code, I'll cover it more fully in a future post, or perhaps a whole series.

Examples

Annotations

Consistent with the rest of the framework, Seam provides a few Annotations to help with exception handling, these were briefly explored in Part I. Here are some examples on how they're applied to application code.
@ApplicationException(rollback=true)
@Redirect(message="This item is out of stock")
public class OutOfStockException extends Exception {
...
}
The @ApplicationException annotation is a mirror of javax.ejb.ApplicationException with additional support for ending a conversation. It is also the entry point Seam uses to configure annotation based exception handlers and must be used if the @HttpError or @Redirect annotations are to be used. In this particular example some of the defaults are used such as not ending the conversation (with the end attribute) and there is no redirect happening, but the message (which can also take an EL expression for i18n, or substitution purposes) will be displayed on the page where the exception occurred.

This next example might be useful with Seam's WebService support, it will return an HTTP error code 500 (probably not the most useful for the clients, but it's an example, right?).

@ApplicationException(rollback=true)
@HttpError(code=501)
public class OutOfStockWsException extends Exception {
...
}
As noted earlier, these two exception handlers (AnnotationRedirectHandler and AnnotationErrorHandler) are the first two handlers in the list and will be the first called to handle the exception, if they are capable, as defined by boolean isHandler(Exception). The classes use the meta data from the annotations to determine if the exception can be handled by one of these two classes. Next up is a closer look at defining the same exception handlers in pages.xml.
Exception handlers can only be defined in the pages.xml, not in the individual page.xml files, which in my opinion is a bit of a short coming. There could be times when the same exception should be handled differently for different pages.

Pages.xml

Because the handlers for pages.xml are roughly the same handlers, different sub classes but they do the same as the annotation sub classes, the above handlers could be declared via pages.xml with the following XML:
<exception class="OutOfStockException">
<redirect>
    <message>This item is out of stock 
</redirect>
</exception>
<exception class="OutofStockWsException">
    <http-error code="501" />
</exception>
Pretty simple stuff. These exceptions will follow the annotations in the list (it's worth noting that each exception that is declared in the XML creates a new instance of the associated handler, unlike the annotation counterpart). If both were declared in the same application the annotation ones would be used. Both ways get the job done, and for your own exceptions it's really just a matter of preference.
The pages.xml declaration really lends itself better for exceptions that cannot be annotated, such as form Hibernate or JPA, please see Part I for examples.

Part III of this series will demonstrate an exception handler framework that could be plugged into Seam and will allow chaining for handling (for example send an email or IRC message and create a bug report for the exception). It should be generic enough to work with Seam, JSF2 (Ed Burns talked about JSF2 Exception handling on his blog), or even the base JRE (yep, 1.5 added Exception handling). It will be hosted on GitHub.

31 January 2010

Seam In Depth: Exception Handling Part I

This three part entry will focus on the mechanisms Seam provides for handling exceptions gracefully -- both out of the box and extensible. An in depth look at where this happens in the Seam code base and user programmable extension points will be covered in Part I. Part II will dive even further and provide examples, and Part III will cover a custom ExceptionHandler implementation. Exception handling in any large application is something that's typically debated at least once: What does it mean, is it simply logging, what do we show the user, can our framework help etc. etc. All software developers will run into this debate at least once in their career, even if it's as simple as "what should I put in that catch block?" It's pretty safe to say swallowing exceptions (as a general rule of thumb) is a bad idea. Of course System.out.println(...) or log.error(...) really isn't much better. Typically the user should be informed that something they have done has resulted in an error on the server, and at the very least ask them to try again. We've all seen pages like the following: An error has occurred, please try again. If the error continues, please contact the webmaster / admin / whatever. This is a cop out and typically a sign of poor exception handling. At least the user didn't see that nasty stack trace or the 500 page from the server. If it's being left up to the user to make contact about an error, chances are it won't happen and the development team probably won't hear about it. Enter the debate about handling those exceptions, and being more pro-active about knowing about errors. Covering each of those questions is beyond the scope of this series, but the question about framework help certainly applies!

Built-in Handling

Seam proveds a simple, yet robust solution for integrating your exception handling strategy. Two methods exist out of the box for rudimentary handling: <exception> in pages.xml and annotations directly on application specific exceptions. Dan Allen covers these two options nicely in his book Seam in Action section 3.6, (if you don't already own a copy, go get one. It's a life saver) so these will only be briefly reviewed. A third option exists (and the before mentioned options, as well as the powerful Seam Debug page are all built on top of this mechanism) to allow complete customization of display to the end user and actions on the back-end: ExceptionHandler classes.

pages.xml

In a basic seam-gen application one will find the following stanzas of XML relating to exception handling:
    <exception class="org.jboss.seam.framework.EntityNotFoundException">
        <redirect view-id="/error.xhtml">
            <message severity="warn">Record not found</message>
        </redirect>
    </exception>
    
    <exception class="javax.persistence.EntityNotFoundException">
        <redirect view-id="/error.xhtml">
            <message severity="warn">Record not found</message>
        </redirect>
    </exception>
    
    <exception class="javax.persistence.EntityExistsException">
        <redirect view-id="/error.xhtml">
            <message severity="warn">Duplicate record</message>
        </redirect>
    </exception>
    
    <exception class="javax.persistence.OptimisticLockException">
        <end-conversation/>
        <redirect view-id="/error.xhtml">
            <message severity="warn">Another user changed the same data, please try again</message>
        </redirect>
    </exception>
    
    <exception class="org.jboss.seam.security.AuthorizationException">
        <redirect view-id="/error.xhtml">
            <message severity="error">You don't have permission to access this resource</message>
        </redirect>
    </exception>
    
    <exception class="org.jboss.seam.security.NotLoggedInException">
        <redirect view-id="/login.xhtml">
            <message severity="warn">#{messages['org.jboss.seam.NotLoggedIn']}</message>
        </redirect>
    </exception>
    
    <exception class="javax.faces.application.ViewExpiredException">
        <redirect view-id="/error.xhtml">
            <message severity="warn">Your session has timed out, please try again</message>
        </redirect>
    </exception>
    
    <exception class="org.jboss.seam.ConcurrentRequestTimeoutException" log-level="trace">
      <http-error error-code="503" />
    </exception>
     
    <exception>
        <redirect view-id="/error.xhtml">
            <message severity="error">Unexpected error, please try again</message>
        </redirect>
    </exception>
The format is pretty straight forward, a fully qualified class is given in the optional class attribute (leaving this out creates a catch-all handler) and the body is as follows from the XSD:
     <xs:element name="exception">
         <xs:annotation>
             <xs:documentation>A Seam exception handler</xs:documentation>
         </xs:annotation>
         <xs:complexType>
             <xs:sequence>
                 <xs:element minOccurs="0" ref="pages:end-conversation"/>
                 <xs:choice>
                     <xs:element ref="pages:http-error"/>
                     <xs:element ref="pages:redirect"/>
                 </xs:choice>
             </xs:sequence>
             <xs:attributeGroup ref="pages:attlist.exception"/>
         </xs:complexType>
     </xs:element>
     <xs:attributeGroup name="attlist.exception">
         <xs:attribute name="class" type="xs:token"/>
         <xs:attribute name="log" type="pages:tf-boolean"/>
         <xs:attribute name="log-level" type="pages:loglevel-values"/> 
     </xs:attributeGroup>
Redirects are the typical body of these XML blocks. FacesMessages can be added to the redirect as well as ending conversations. Typically these are used for exceptions the developer does not create, and therefore cannot be annotated; however, there may be uses for this style of handling with application exceptions. Please note that order does matter. If the catch-all handler is first, none of the other handlers will be used. Each of these entries creates an ExceptionHandler instance which is then added to the chain of handlers, which is covered later in the entry.

Annotations

Seam has three annotations to help with exception handling: @Redirect, @HttpError, and @ApplicationException. Only one of @Redirect or @HttpError may be used on an exception. Both allow for an info level FacesMessage to be added via the message attribute. The @Redirect annotation allows input for a view-id to which the user will be redirected. @HttpError simply returns the specified HTTP Error code. The last annotation, @ApplicationException provides the ability to rollback the transaction and end the existing conversation. These annotations are added directly on the Exception class that is created. Both of these built-in methods are built on top of two classes in Seam: org.jboss.seam.exception.Exceptions and org.jboss.seam.exception.ExceptionHandler. These two classes and their usage make up Seam's infrastructure for exception handling in an application, and provide customization to the developer.

Extensible Handling

The above two methods work well for simple cases. What if a team wants to be more pro-active about addressing errors? Could the application send the team emails about exceptions, or create bug tickets, or perhaps create a contextual log entry? This is where the exception handling capabilities of Seam really shine, and the answer to the above questions are all yes!

ExceptionHandler

package org.jboss.seam.exception;

import org.jboss.seam.faces.Navigator;

/**
 * An element of the chain that knows how to handle a 
 * specific exception type.
 * 
 * @author Gavin King
 *
 */
public abstract class ExceptionHandler extends Navigator
{  
   public enum LogLevel { fatal, error, warn, info, debug, trace }
   
   private boolean logEnabled;
   private LogLevel logLevel;
   
   public abstract void handle(Exception e) throws Exception;
   public abstract boolean isHandler(Exception e);
   
   public boolean isLogEnabled()
   {  
      return logEnabled;
   }
   
   public void setLogEnabled(boolean logEnabled)
   {  
      this.logEnabled = logEnabled;
   }
   
   public LogLevel getLogLevel()
   {
      return logLevel;
   }
      
   public void setLogLevel(LogLevel logLevel)
   {
      this.logLevel = logLevel;
   }   
   
}
The contract for this class is pretty straight forward. The only methods that need to be implemented are isHandler and handleException. Of course the isHandler implementation is trivial, and based on the needs of the handler the handleException implementation could be fairly simple as well. Take note that ExceptionHandler is a subclass of Navigator. There are methods defined in that class which may be helpful in some exception handlers.

Exceptions

This class is used in the org.jboss.seam.jsf.SeamPhaseListener before and after each phase of the JSF lifecycle and also in the org.jboss.seam.web.ExceptionFilter, wrapping the filter chain, and used again after any redirects that may happen after the filter chain has completed. As Dan Allen described it in Seam in Action: "A try-catch around the [whole request]" (Seam in Action pg. 126). There's only one method that needs any kind of attention: getHandlers. It returns a list that is used to add or remove ExceptionHandlers as needed. This method should be called in the @Create method, or constructor of any ExceptionHandler. ExceptionHandler classes should also be marked with @BypassInterceptors as there's no need to do bi-jection. A simple POJO, though, will typically suffice. Exception handling in Seam can be very powerful if used to it's full potential and should be an excellant tool in crafting an exception handler strategy for your application. The next entry in this series will demonstrate a custom exception handler in action.