Skip to content

Reorganize functional asp.net core extensions #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using Codehard.Functional.AspNetCore.Errors;
using LanguageExt.Common;
using static LanguageExt.Prelude;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Net;
using Codehard.Functional.AspNetCore.ActionResults;
using Codehard.Functional.AspNetCore.Errors;
using Codehard.Functional.AspNetCore.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using Codehard.Functional.AspNetCore.Errors;
using LanguageExt.Common;
using static LanguageExt.Prelude;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Dynamic;
using Codehard.Functional.AspNetCore.Errors;

namespace Codehard.Functional.AspNetCore;
namespace Codehard.Functional.AspNetCore.ActionResults;

/// <summary>
/// An <see cref="IActionResult"/> that wraps an <see cref="HttpResultError"/> object and
Expand Down Expand Up @@ -65,6 +66,8 @@ await this.Error.Data

return Task.CompletedTask;
});

return;

ObjectResult AddErrorInfo(ObjectResult objectResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
</AssemblyAttribute>
</ItemGroup>




</Project>
101 changes: 12 additions & 89 deletions src/Codehard.Functional/Codehard.Functional.AspNetCore/Commons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,17 @@

public static class Commons
{
public static Error Flatten(this Seq<Error> errors, int errorCode, char sep = '\n')
=> Error.New(errorCode, string.Concat(sep, errors.Map(e => e.Message)));

internal static Aff<A> MapFailToHttpResultError<A>(
this Aff<A> ma,
HttpStatusCode code,
Option<string> errorCode = default,
Option<string> messageOpt = default,
Option<object> data = default,
bool @override = true)
=> ma.MapFail(err =>
{
var message = messageOpt.IfNone(string.Empty);

return
err switch
{
HttpResultError hre when !@override => hre,
_ =>
HttpResultError.New(
code, message, errorCode, data, err),
};
});

internal static Aff<A> MapFailToHttpResultError<A>(
this Aff<A> ma,
HttpStatusCode code,
Func<Error, string> messageFunc,
Option<string> errorCode = default,
Option<object> data = default,
bool @override = true)
=> ma.MapFail(err =>
{
return
err switch
{
HttpResultError hre when !@override => hre,
_ =>
HttpResultError.New(
code,
messageFunc(err),
errorCode,
data,
err),
};
});

internal static Eff<A> MapFailToHttpResultError<A>(
this Eff<A> ma,
HttpStatusCode code,
Option<string> errorCode = default,
Option<string> messageOpt = default,
Option<object> data = default,
bool @override = true)
=> ma.MapFail(err =>
{
var message = messageOpt.IfNone(string.Empty);

return
err switch
{
HttpResultError hre when !@override => hre,
_ =>
HttpResultError.New(
code, message, errorCode, data, err),
};
});

internal static Eff<A> MapFailToHttpResultError<A>(
this Eff<A> ma,
HttpStatusCode code,
Func<Error, string> messageFunc,
Option<string> errorCode = default,
Option<object> data = default,
bool @override = true)
=> ma.MapFail(err =>
{
return
err switch
internal static IActionResult MapToActionResult<T>(HttpStatusCode statusCode, T result)
{
return
result switch
{
IActionResult ar => ar,
Unit => new StatusCodeResult((int)statusCode),
_ => new ObjectResult(result)
{
HttpResultError hre when !@override => hre,
_ =>
HttpResultError.New(
code,
messageFunc(err),
errorCode,
data,
err),
};
});
StatusCode = (int)statusCode,
},
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Codehard.Functional.AspNetCore.Errors;

/// <summary>
/// An HTTP error object.
/// </summary>
public record ActionResultError : Error
{
private readonly IActionResult actionResult;

private ActionResultError(IActionResult actionResult)
{
this.actionResult = actionResult;
Message = string.Empty;
}

public override bool Is<E>() => false;

public override ErrorException ToErrorException()
{
throw new NotSupportedException();
}

public override string Message { get; }

public override bool IsExceptional => false;

public override bool IsExpected => true;

public IActionResult ActionResult => actionResult;

public static ActionResultError New(
IActionResult actionResult)
{
return new ActionResultError(actionResult);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.Serialization;

namespace Codehard.Functional.AspNetCore;
namespace Codehard.Functional.AspNetCore.Errors;

/// <summary>
/// An HTTP error object.
Expand Down
Loading