Skip to content

Commit

Permalink
Adding error handling. related #1
Browse files Browse the repository at this point in the history
  • Loading branch information
andreazevedo committed Aug 11, 2011
1 parent 2d26e16 commit 9caccae
Show file tree
Hide file tree
Showing 16 changed files with 990 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/PetStore.Core/DomainObjects/DomainObjectsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PetStore.Core.DomainObjects.Log;
using PetStore.Core.Helper;

namespace PetStore.Core.DomainObjects
{
Expand All @@ -10,5 +12,42 @@ namespace PetStore.Core.DomainObjects
/// </summary>
public static class DomainObjectsFactory
{
#region Log

public static class Log
{
/// <summary>
/// Creates an system error from an exception
/// </summary>
/// <param name="exception">Exception</param>
/// <param name="additionalInformation">Additional information about this error (if any).</param>
/// <param name="handled">Informs whether the exception was properly handled.</param>
/// <returns>Error object.</returns>
public static Error CreateError(Exception exception, bool handled = true, string additionalInformation = null)
{
Check.Argument.IsNotNull(exception, "exception");

Error error;
if (exception is DomainException)
{
error = new DomainError();
}
else
{
error = new GenericError();
}

error.GeneratedAt = DateTime.Now;
error.Type = exception.GetType().FullName;
error.Message = exception.Message;
error.Details = ExceptionManager.GetExceptionAsString(exception);
error.AdditionalInformation = additionalInformation;
error.Handled = handled;

return error;
}
}

#endregion
}
}
14 changes: 14 additions & 0 deletions src/PetStore.Core/DomainObjects/Log/DomainError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PetStore.Core.DomainObjects.Log
{
/// <summary>
/// Error of application domain
/// </summary>
public class DomainError : Error
{
}
}
62 changes: 62 additions & 0 deletions src/PetStore.Core/DomainObjects/Log/Error.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PetStore.Core.DomainObjects.Log
{
/// <summary>
/// Represents an error
/// </summary>
public abstract class Error : IValidatable<Error>
{
#region Properties

/// <summary>
/// Error ID
/// </summary>
public virtual int Id { get; set; }

/// <summary>
/// When the error occurred
/// </summary>
public virtual DateTime GeneratedAt { get; set; }

/// <summary>
/// Error Type (represents the exception Type)
/// </summary>
public virtual string Type { get; set; }

/// <summary>
/// Error message
/// </summary>
public virtual string Message { get; set; }

/// <summary>
/// Error details
/// </summary>
public virtual string Details { get; set; }

/// <summary>
/// Error additional information
/// </summary>
public virtual string AdditionalInformation { get; set; }

/// <summary>
/// Informs whether the error was handled (true) or not (false).
/// </summary>
public virtual bool Handled { get; set; }

#endregion

#region IValidatable Members

bool IValidatable<Error>.Validate(IValidator<Error> validator, out IList<string> brokenRules)
{
brokenRules = validator.BrokenRules(this);
return brokenRules.Count() == 0;
}

#endregion
}
}
14 changes: 14 additions & 0 deletions src/PetStore.Core/DomainObjects/Log/GenericError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PetStore.Core.DomainObjects.Log
{
/// <summary>
/// Generic Error - not an error of application domain.
/// </summary>
public class GenericError : Error
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PetStore.Resources.DomainObjects.Log;

namespace PetStore.Core.DomainObjects.Log.Validation
{
public class ErrorValidationException : ValidationException
{
#region Constructors

public ErrorValidationException(IList<string> brokenRules)
: base(GetMainMessage(), brokenRules)
{
}

#endregion

#region Static Methods

private static string GetMainMessage()
{
return ErrorResources.ErrorValidationExceptionMainMessage;
}

#endregion
}
}
40 changes: 40 additions & 0 deletions src/PetStore.Core/DomainObjects/Log/Validation/ErrorValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PetStore.Core.Extension;
using PetStore.Resources.DomainObjects.Log;

namespace PetStore.Core.DomainObjects.Log.Validation
{
/// <summary>
/// Validates an error object
/// </summary>
public class ErrorValidator : IValidator<Error>
{
#region Constants

public const int TypeMaxSize = 500;

#endregion

#region Methods

public IList<string> BrokenRules(Error obj)
{
IList<string> brokenRules = new List<string>();

if (String.IsNullOrWhiteSpace(obj.Type))
brokenRules.Add(ErrorResources.ErrorTypeIsRequired);
else if (obj.Type.Length > TypeMaxSize)
brokenRules.Add(ErrorResources.ErrorTypeTooBig.FormatWith(TypeMaxSize));

if (String.IsNullOrWhiteSpace(obj.Message))
brokenRules.Add(ErrorResources.ErrorMessageIsRequired);

return brokenRules;
}

#endregion
}
}
133 changes: 133 additions & 0 deletions src/PetStore.Core/Helper/ExceptionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using PetStore.Core.Infrastructure.InversionOfControl;
using PetStore.Core.Services.Log;

namespace PetStore.Core.Helper
{
/// <summary>
/// Responsible for registering exception information
/// </summary>
public static class ExceptionManager
{
#region Public Methods

/// <summary>
/// Logs an exception
/// </summary>
/// <param name="exception">Exception</param>
public static void LogException(Exception exception)
{
Check.Argument.IsNotNull(exception, "exception");

LogException(exception, true, string.Empty);
}

/// <summary>
/// Logs an Exception
/// </summary>
/// <param name="exception">Excetion</param>
/// <param name="handled">Informs whether the exception was handled.</param>
/// <param name="additionalInformation">Additional information about the error.</param>
public static void LogException(Exception exception, bool handled, string additionalInformation)
{
Check.Argument.IsNotNull(exception, "exception");

GetErrorService().RegisterException(exception, handled, additionalInformation);
}

/// <summary>
/// Gets an excetpion as a string. Includes all inner exception details in the string
/// </summary>
/// <param name="ex">Exception.</param>
/// <returns>String with all information about the exception.</returns>
public static string GetExceptionAsString(Exception ex)
{
Check.Argument.IsNotNull(ex, "exception");

try
{
StringBuilder sbMessage = new StringBuilder();
sbMessage.Append("EXCEPTION\n");

sbMessage.AppendLine("Type:");
sbMessage.AppendLine(ex.GetType().FullName);
sbMessage.AppendLine();

if (!String.IsNullOrEmpty(ex.Message))
{
sbMessage.AppendLine("Message:");
sbMessage.AppendLine(ex.Message);
sbMessage.AppendLine();
}

if (!String.IsNullOrEmpty(ex.StackTrace))
{
sbMessage.AppendLine("Stack Trace:");
sbMessage.AppendLine(ex.StackTrace);
sbMessage.AppendLine();
}

if (ex.InnerException != null)
{
sbMessage.AppendLine();
sbMessage.AppendLine();
sbMessage.Append(GetMessageFromInnerException(ex.InnerException, 1));
}

return sbMessage.ToString();
}
catch (Exception)
{
return string.Empty;
}
}

#endregion

#region Private Methods

private static string GetMessageFromInnerException(Exception ex, int number)
{
StringBuilder sbMessage = new StringBuilder();
sbMessage.Append(String.Format("INNER EXCEPTION {0}\n", number));

sbMessage.AppendLine("Type:");
sbMessage.AppendLine(ex.GetType().FullName);
sbMessage.AppendLine();

if (!String.IsNullOrEmpty(ex.Message))
{
sbMessage.AppendLine("Message:");
sbMessage.AppendLine(ex.Message);
sbMessage.AppendLine();
}

if (!String.IsNullOrEmpty(ex.StackTrace))
{
sbMessage.AppendLine("Stack Trace:");
sbMessage.AppendLine(ex.StackTrace);
sbMessage.AppendLine();
}

if (ex.InnerException != null)
{
sbMessage.AppendLine();
sbMessage.Append(GetMessageFromInnerException(ex.InnerException, number + 1));
}

return sbMessage.ToString();
}

[DebuggerStepThrough]
private static ErrorService GetErrorService()
{
return IoC.Resolve<ErrorService>();
}

#endregion
}
}
Loading

0 comments on commit 9caccae

Please sign in to comment.