forked from GFlisch/Arc4u
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
355 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Arc4u.AspNetCore.Results/Arc4u.AspNetCore.Results.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks> | ||
<Version>0.0.0.0</Version> | ||
<Authors>Gilles Flisch</Authors> | ||
<Description>Package used on Interface and Facade projects to return ProblemDetails based on Results.</Description> | ||
<Copyright>Gilles Flisch</Copyright> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageIcon>Arc4u.png</PackageIcon> | ||
<PackageProjectUrl>https://github.com/GFlisch/Arc4u</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/GFlisch/Arc4u</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageTags>Arc4u</PackageTags> | ||
<AssemblyVersion>1.0.0.0</AssemblyVersion> | ||
<FileVersion>1.0.0.0</FileVersion> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<RootNamespace>Arc4u.OAuth2</RootNamespace> | ||
<LangVersion>latest</LangVersion> | ||
<PackageId>Arc4u.AspNetCore.Results</PackageId> | ||
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_").Replace(".Standard", ""))</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath> | ||
</PackagePath> | ||
</None> | ||
<None Include="..\..\Arc4u.png"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Arc4u.Standard.Results\Arc4u.Standard.Results.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Arc4u.AspNetCore.Results; | ||
using Arc4u.Results.Validation; | ||
using Arc4u.ServiceModel; | ||
using FluentResults; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Arc4u.Results; | ||
|
||
public static class ProblemDetailExtension | ||
{ | ||
public static Func<IError, ProblemDetails> FromError => error => _fromError(error); | ||
|
||
public static void SetFromErrorFactory(Func<IError, ProblemDetails> fromError) | ||
{ | ||
_fromError = fromError; | ||
} | ||
private static Func<IError, ProblemDetails> _fromError = _from; | ||
|
||
private static ProblemDetails _from(IError error) | ||
{ | ||
if (error is ValidationError validationError) | ||
{ | ||
return new ProblemDetails() | ||
.WithTitle("Error from validation.") | ||
.WithDetail(validationError.Message) | ||
.WithStatusCode(StatusCodes.Status422UnprocessableEntity) | ||
.WithSeverity(validationError.Severity.ToString()); | ||
} | ||
|
||
return new ProblemDetails() | ||
.WithTitle("Error.") | ||
.WithDetail(error.Message) | ||
.WithStatusCode(StatusCodes.Status400BadRequest) | ||
.WithSeverity(Severity.Error.ToString()); | ||
} | ||
|
||
public static ProblemDetails ToGenericMessage<TResult>(this Result<TResult> result) | ||
{ | ||
return ToGenericMessage(result, Activity.Current?.Id); | ||
} | ||
|
||
public static ProblemDetails ToGenericMessage<TResult>(this Result<TResult> result, string? activityId) | ||
{ | ||
return result.ToResult().ToGenericMessage(activityId); | ||
} | ||
|
||
public static ProblemDetails ToGenericMessage(this Result result) | ||
{ | ||
return result.ToGenericMessage(Activity.Current?.Id); | ||
} | ||
|
||
public static ProblemDetails ToGenericMessage(this Result result, string? activityId) | ||
{ | ||
if (result.IsFailed) | ||
{ | ||
result.Log(); | ||
|
||
if (activityId is not null) | ||
{ | ||
return new ProblemDetails() | ||
.WithTitle("A technical error occured!") | ||
.WithDetail($"Contact the application owner. A message has been logged with id: {activityId}.") | ||
.WithStatusCode(StatusCodes.Status400BadRequest); | ||
} | ||
|
||
return new ProblemDetails() | ||
.WithTitle("A technical error occured!") | ||
.WithDetail("Contact the application owner. A message has been logged.") | ||
.WithStatusCode(StatusCodes.Status400BadRequest); | ||
} | ||
|
||
return new ProblemDetails() | ||
.WithTitle("A technical error occured!") | ||
.WithDetail("Contact the application owner.") | ||
.WithStatusCode(StatusCodes.Status400BadRequest); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// If Success, return the reasons! | ||
/// If Failure and no exceptions, return the Errors: Message, Code, Severity. | ||
/// If Failure and exceptions, Log and return the generic messages. | ||
/// </summary> | ||
/// <typeparam name="TResult"></typeparam> | ||
/// <param name="result"></param> | ||
/// <returns></returns> | ||
public static List<ProblemDetails> ToProblemDetails<TResult>(this Result<TResult> result) | ||
{ | ||
if (result.IsSuccess) | ||
{ | ||
return new List<ProblemDetails>(); | ||
} | ||
|
||
if (result.IsFailed && result.Errors.OfType<IExceptionalError>().Any()) | ||
{ | ||
result.Log(); | ||
return new List<ProblemDetails>(new[] { result.ToGenericMessage() }); | ||
} | ||
|
||
return result.Errors.Select(e => ProblemDetailExtension.FromError(e)).ToList(); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// If Success, return the reasons! | ||
/// If Failure and no exceptions, return the Errors: Message, Code, Severity. | ||
/// If Failure and exceptions, Log and return the generic messages. | ||
/// </summary> | ||
/// <typeparam name="TResult"></typeparam> | ||
/// <param name="result"></param> | ||
/// <returns></returns> | ||
public static List<ProblemDetails> ToProblemDetails(this Result result) | ||
{ | ||
if (result.IsSuccess) | ||
{ | ||
return result.Reasons.Select(reason => new ProblemDetails().WithDetail(reason.Message).WithSeverity(Severity.Info.ToString())).ToList(); | ||
} | ||
|
||
if (result.IsFailed && result.Errors.OfType<IExceptionalError>().Any()) | ||
{ | ||
result.Log(); | ||
return new List<ProblemDetails>([result.ToGenericMessage()]); | ||
} | ||
|
||
return result.Errors.Select(error => ProblemDetailExtension.FromError(error)).ToList(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using Arc4u.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Arc4u.AspNetCore.Results; | ||
public static class ProblemDetailsExtensions | ||
{ | ||
public static ProblemDetails WithStatusCode(this ProblemDetails problemDetails, int statusCode) | ||
{ | ||
problemDetails.Status = statusCode; | ||
return problemDetails; | ||
} | ||
|
||
public static ProblemDetails WithTitle(this ProblemDetails problemDetails, string title) | ||
{ | ||
problemDetails.Title = title; | ||
return problemDetails; | ||
} | ||
|
||
public static ProblemDetails WithType(this ProblemDetails problemDetails, Uri type) | ||
{ | ||
problemDetails.Type = type.ToString(); | ||
return problemDetails; | ||
} | ||
|
||
public static ProblemDetails WithDetail(this ProblemDetails problemDetails, string detail) | ||
{ | ||
problemDetails.Detail = detail; | ||
return problemDetails; | ||
} | ||
|
||
public static ProblemDetails WithCode(this ProblemDetails problemDetails, string code) | ||
{ | ||
problemDetails.Extensions.AddOrReplace("Code", code); | ||
return problemDetails; | ||
} | ||
|
||
public static ProblemDetails WithSeverity(this ProblemDetails problemDetails, string severity) | ||
{ | ||
problemDetails.Extensions.AddOrReplace("Severity", severity); | ||
return problemDetails; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.