Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Extensions / ExceptionChaining / EmailHandler.cs
blob63195e73c2338bac680ef64e584857245b39cb5a
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Extensions.ExceptionChaining
17 using System;
18 using System.Configuration;
19 using System.Xml;
21 using Castle.Components.Common.EmailSender;
23 /// <summary>
24 /// A handle that sends the exception by email
25 /// </summary>
26 public class EmailHandler : AbstractExceptionHandler, IConfigurableHandler
28 private String mailTo;
29 private String mailFrom = "donotreply@castleproject.org";
31 /// <summary>
32 /// Implementors should check for known attributes and child nodes
33 /// within the <c>exceptionHandlerNode</c>
34 /// </summary>
35 /// <param name="exceptionHandlerNode">The Xml node
36 /// that represents this handler on the configuration file</param>
37 public void Configure(XmlNode exceptionHandlerNode)
39 XmlAttribute mailToAtt = exceptionHandlerNode.Attributes["mailTo"];
41 if (mailToAtt == null || mailToAtt.Value == String.Empty)
43 String message="'mailTo' is a required attribute " +
44 "for EmailHandler (part of ExceptionChaining extension)";
45 throw new ConfigurationErrorsException(message);
48 mailTo = mailToAtt.Value;
50 XmlAttribute mailFromAtt = exceptionHandlerNode.Attributes["mailFrom"];
52 if (mailFromAtt != null && mailFromAtt.Value != String.Empty)
54 mailFrom = mailFromAtt.Value;
58 /// <summary>
59 /// Implementors should perform the action
60 /// on the exception. Note that the exception
61 /// is available in <see cref="IRailsEngineContext.LastException"/>
62 /// </summary>
63 /// <param name="context"></param>
64 public override void Process(IRailsEngineContext context)
66 IEmailSender emailSender = (IEmailSender) context.GetService( typeof(IEmailSender) );
68 String message = BuildStandardMessage(context);
70 try
72 emailSender.Send( mailFrom, mailTo,
73 "MonoRail Exception: " + context.LastException.GetType().Name, message );
75 catch(Exception)
77 // We ignore errors during send
80 InvokeNext(context);