ExceptionDetailConverter Example

on Monday, April 20, 2020

Thanks to Alan’s sharp eyes, he saw that I had forgotten to put in examples for the IYourBussApiExceptionDetailConverter and YourBussApiExceptionDetailConverter into the post called ExceptionHandler Needed. Which was a follow-up post to Create a Custom ProblemDetailsFactory.

So, here are some examples, which should definitely be customized to fit your environments needs:

namespace Your.Namespace
{
/// <summary>
/// Serializable information from an exception. This will contain the Exceptions Message, Type, StackTrace and InnerException (if any).
/// </summary>
public class YourBussApiExceptionDetail
{
/// <summary>
/// The message of the original exception.
/// </summary>
public string Message { get; set; }
/// <summary>
/// The type of the exception.
/// </summary>
public string Type { get; set; }
/// <summary>
/// The stacktrace of the exception.
/// </summary>
public string StackTrace { get; set; }
/// <summary>
/// The inner exception, if any. If there is no inner exception, then this is null.
/// </summary>
public SaApiExceptionDetail InnerException { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Logging;
namespace Your.Namespace
{
/// <summary>
/// Convertor for <see cref="Exception" />s into <see cref="SaApiExceptionDetail" /> object.
/// </summary>
public interface IYourApiExceptionDetailConverter
{
/// <summary>
/// Will convert an <see cref="Exception" /> into an <see cref="SaApiExceptionDetail" /> object.
/// </summary>
YourBussApiExceptionDetail Convert(Exception e);
}
/// <summary>
/// Convertor for <see cref="Exception" />s into <see cref="SaApiExceptionDetail" /> object.
/// </summary>
public class YourBussApiExceptionDetailConverter : IYourBussApiExceptionDetailConverter
{
private ILogger<YourBussApiExceptionDetailConverter> _logger;
public YourBussApiExceptionDetailConverter(ILogger<YourBussApiExceptionDetailConverter> logger)
{
_logger = logger;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void LogConverting(Exception e)
{
_logger.LogDebug(
$"Converting '{e.GetType().Name}' ({e.Message})",
new { Type = e.GetType().Name, Message = e.Message }
);
}
/// <inheritdoc />
public YourBussApiExceptionDetail Convert(Exception e)
{
var result = new YourBussApiExceptionDetail();
var currentException = e;
var currentDetail = result;
do
{
LogConverting(currentException);
currentDetail.Message = currentException.Message;
currentDetail.Type = currentException.GetType().AssemblyQualifiedName;
currentDetail.StackTrace = currentException.StackTrace;
currentDetail.InnerException = null;
if (currentException.InnerException != null)
{
currentDetail.InnerException = new YourBussApiExceptionDetail();
currentDetail = currentDetail.InnerException;
}
currentException = currentException.InnerException;
} while (currentException != null);
return result;
}
}
}

Again, Thanks for catching that!

0 comments:

Post a Comment


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.