Giter Site home page Giter Site logo

simpleexceptionhandling's Introduction

Hi there 👋

simpleexceptionhandling's People

Contributors

gravity00 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

erisonliang

simpleexceptionhandling's Issues

Catch with an options class as an argument

The catch method could also receive an options class as an argument. Example:

var result = 
    Handling.Prepare()
        .Catch(context.Exception, null, new CatchOptions
        {
            ThrowIfNotHandled = false
        });

Finalization code

Since the framework replicates the try-catch block, adding a finalization function could be useful, allowing for a try-catch-finally block.

Example to replicate:

try
{
    throw new ArgumentNullException();
}
catch (ArgumentNullException)
{
    Console.WriteLine("ArgumentNullException thrown");
}
catch (Exception)
{
    Console.WriteLine("Exception thrown");
}
finally
{
    Console.WriteLine("Finally code executed");
}

IHandlingConfiguration - Single responsibility principle

At the present moment, the interface IHandlingConfiguration is responsible for storing handling configurations and running the handling logic itself. This could be considered a violation of the single responsibility principle.

To resolve this question, it could be split, at least, in two interfaces:

  • IHandlingConfiguration - stores the collection of handlers and other handling properties;
  • IHandlingService - contains the logic for handling exceptions, and uses an IHandlingConfiguration to decide his behavior;

This implementation should allow a more decoupled handling logic.

Handling.Prepare() not returning a IHandlingConfiguration

The method Handling.Prepare() is returning an IHandlingConfiguration<object, object> instead of an IHandlingConfiguration, as indicated by the documentation.

Example:

IHandlingConfiguration conf = Handling.Prepare(); // error, can´t implicit convert the result
IHandlingConfiguration<object, object> conf = Handling.Prepare(); // success

Stack trace being rewritten when thrown in Catch method

Example code:

      var result = Handling.Prepare()
                .On<ValidationException>(ex =>
                {
                    var messages = new List<string>();
                    foreach (var error in ex.ValidationErrors)
                    {
                        messages.AddRange(error.Messages);
                    }

                    context.CreateBadRequestResult(
                        IsLocalhost(context),
                        new[]
                        {
                            new KeyValuePair<string, string[]>(string.Empty, messages.ToArray())
                        });
                })
                .On<BusinessException>(ex =>
                {
                    context.CreateConflictResult(ex.Message, IsLocalhost(context));
                })
                .On<GenericException>((ex) =>
                {
                    context.TransformResultIntoInternalServerError(ex.Message, IsLocalhost(context));
                })
                .On<TimeoutException>((ex) =>
                {
                    context.CreateGatewayTimeoutResult(IsLocalhost(context));
                })
                .On<NotImplementedException>(ex =>
                {
                    context.CreateNotImplementedResult(IsLocalhost(context));
                })
                .Catch(context.Exception, null, true);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.