Skip to content

A Straightforward Package for avoiding exception handling in ASP.NET Core.

Notifications You must be signed in to change notification settings

SamCoSystems/SafeResult

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SafeResult

Simple Wrapper for avoiding exception handling and validation/guard logic in your ASP.NET Core controllers.

using SamCo.AspNetCore.SafeResult;

public class PostsController : Controller
{
    private Result<Post> GetPost(int id)
    {
        try
        {
            Post post = Some.Legacy.Api.GetPost(id);
            if (post == null)
                return new NotFoundResult(); // Implicit cast
            return post; // also implicit cast
        }
        catch
        {
            // Log or whatever you want
            return StatusCodeResult(500);
        }
    }

    [HttpGet("/post/{id}")]
    public IActionResult GetPost(int id)
    {
        Result<Post> getPost = GetPost(id);
        if (getPost.Errored)
            return getPost.ErrorResult;

        return Ok(getPost.Value);
    }

    [HttpDelete("/post/{id}")]
    public IActionResult DeletePost(int id)
    {
        Result<User> getUser = _userService.GetCurrentUser();
        if (getUser.Errored) // Unauthorized or challenge
            return getUSer.ErrorResult;

        Result<Post> getPost = GetPost(id);
        if (getPost.Errored) // NotFound or InternalServerError
            return getPost.ErrorResult;

        // "void" methods can use non-generic Result
        Result deletePost = _newPostService.Delete(getPost.Value, getUser.Value);

        if (deletePost.Errored) // Forbid (user trying to delete post they don't own)
            return deletePost.ErrorResult;

        return NoContent(); // Finish
    }
}

About

A Straightforward Package for avoiding exception handling in ASP.NET Core.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages