Home

Castle Stronghold

Exception Chaining Extension

The Exception Chaining extension allow you to execute one or more steps in response to an exception threw by an action. The steps are called Exception Handlers and must implement IExceptionHandler (or IConfigurableHandler if they need external configuration).

Using the extension

The extension does not do much more than delegating the execution to the installed handlers. You can create handlers to provide actions and even introduce new semantics. As the handlers are chained together, you can even implement a handler that decides if the execution chain should continue or stop right there. For example, suppose you want that only exceptions that extends SqlException be e-mailed to you. In this case you could write this simple handler:


public class MyFilterHandler : AbstractExceptionHandler
{
    public override void Process(IRailsEngineContext context, IServiceProvider serviceProvider)
    {
        if (context.LastException is SqlException)
        {
            InvokeNext(context, serviceProvider);
        }
    }
}

And of course, register this handler before others.

The EmailHandler

MonoRail comes with just one exception handler: EmailHandler. This handler e-mails the exception details and the environment details to a specified e-mail address.

This handler requires the attribute mailto and you can optionaly inform the mailfrom as well. Also, you must provide the smtpHost in the configuration -- see MonoRail Configuration Reference for more details on this one.


<monorail smtpHost="my.smtp.server">
    <extensions>
        <extension 
        type="Castle.MonoRail.Framework.Extensions.ExceptionChaining.ExceptionChainingExtension, Castle.MonoRail.Framework" />
    </extensions>
    <exception>
        <exceptionHandler 
            mailTo="lazydeveloper@mycompany.com" mailFrom="angry.client@client.com" 
            type="Castle.MonoRail.Framework.Extensions.ExceptionChaining.EmailHandler, Castle.MonoRail.Framework" />
    </exception>
</monorail>

IExceptionHandler

The IExceptionHandler interface is very straighforward, it simply dictates the contract for processing the exception information. As they are chained you must be good and check if there's a next handler available and if so, delegate the invocation to it. It would be also a good behavior if your handler implementation doesn't throw exceptions at all.

It is also important to note that you can use the AbstractExceptionHandler to save you some few types.

IConfigurableHandler

The IConfigurableHandler is just an increment the the previous interface for those handlers that require configuration information. The Configure method is invoked as soon as the handler is instantiated and its node on the configuration is passed.

Changing MonoRail configuration

You need to install the extension using the extensions node, as usual, and also provide the node exception to list the handlers you want to install. Please note that they will be installed and chained in the same order they were declared.


	
		
	
	
		
	

]]>
Google
Search WWW Search castleproject.org