Skip to content

Commit

Permalink
MapToProblemDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 13, 2024
1 parent ffcb034 commit f74377c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/SwiftLink.Application/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ internal static class Constants
{
public static class Database
{
public static Error InsertFailed = new("Database.Failed", "Insert into db is failed :(");
public static Error InsertFailed = Error.Failure("Database.Failed", "Insert into db is failed :(");
}

public static class Link
{
public static Error UrlMustBeSent = new("InValidFormat", "Url must be sent!");
public static Error UrlMustBeSent = Error.Validation("InValidFormat", "Url must be sent!");

public static Error InvalidUrlFormat = new("InValidFormat", "Invalid Url Format!");
public static Error InvalidUrlFormat = Error.Validation("InValidFormat", "Invalid Url Format!");
}

public static class Subscriber
{
public static Error TokenMustBeSent = new("UnAuthorized", "Token must be sent!");
public static Error TokenMustBeSent = Error.Validation("UnAuthorized", "Token must be sent!");

public static Error InvalidToken = new("UnAuthorized", "Invalid User!");
public static Error InvalidToken = Error.NotFound("UnAuthorized", "Invalid User!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public async Task<Result<object>> Handle(GenerateShortCodeCommand request, Cance

var dbResult = await _dbContext.SaveChangesAsync(cancellationToken);
if (dbResult.IsFailure)
return Result<object>.Failure(dbResult.Message);
return Result.Failure<object>(dbResult.Message);

await _cache.Set(request.Url, JsonSerializer.Serialize(link), link.ExpirationDate);
return Result<object>.Success(link);
return Result.Success<object>(link);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class GenerateShortCodeValidator : AbstractValidator<GenerateShortCodeCom
public GenerateShortCodeValidator()
{
RuleFor(x => x.Url)
.NotNull().WithMessage(Constants.Link.UrlMustBeSent)
.Must(BeAValidUrl).WithMessage(Constants.Link.InvalidUrlFormat);
.NotNull().WithMessage(Constants.Link.UrlMustBeSent.Message)
.Must(BeAValidUrl).WithMessage(Constants.Link.InvalidUrlFormat.Message);

//RuleFor(x => x.Token)
// .NotNull().WithMessage(Constants.Link.UrlMustBeSent)
Expand Down
36 changes: 36 additions & 0 deletions src/SwiftLink.Presentation/Extensions/MapToProblemDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using SwiftLink.Shared;

namespace SwiftLink.Presentation.Extensions;

public static class MapToProblemDetailsExtension
{
public static Result MapToProblemDetails(this Result result)
{
static int GetStatusCode(ErrorType type)
=> type switch
{
ErrorType.Validation => StatusCodes.Status400BadRequest,
ErrorType.Failure => StatusCodes.Status500InternalServerError,
ErrorType.NotFound => StatusCodes.Status404NotFound,
ErrorType.None => StatusCodes.Status500InternalServerError,
_ => StatusCodes.Status500InternalServerError
};

static string GetTitle(ErrorType type)
=> type switch
{
ErrorType.Validation => "Bad Request",
ErrorType.Failure => "Internal Server Error",
ErrorType.NotFound => "Not Found",
ErrorType.None => "Internal Server Error",
_ => "Internal Server Error"
};

return Result.Problem(
status: GetStatusCode(result.Error.Type),
type: result.Error.Type.ToString(),
title: GetTitle(result.Error.Type),
detail: result.Error.Message
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ public async Task InvokeAsync(HttpContext context)
Detail = "One or more validation errors has occurred"
};

if (exception.Errors is not null)
problemDetails.Extensions["errors"] = exception.Errors;
//if (exception.Errors is not null)
// problemDetails.Extensions["errors"] = exception.Errors;

Result.Failure
//Result.Failure<ProblemDetails>(problemDetails);

context.Response.StatusCode = StatusCodes.Status400BadRequest;
//context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsJsonAsync(problemDetails);
}



}


}
39 changes: 36 additions & 3 deletions src/SwiftLink.Shared/Error.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
namespace SwiftLink.Shared;
public sealed record Error(string Code, string? Message = null)

using System.Net.Http.Headers;

namespace SwiftLink.Shared;
public sealed record Error
{

public static readonly Error None = new(string.Empty, string.Empty);
public static readonly Error None = new(string.Empty, string.Empty, ErrorType.Failure);
public static implicit operator Result(Error error) => Result.Failure(error);

private Error(string code, string message, ErrorType type)
{
Code = code;
Message = message;
Type = type;
}

public string Code { get; set; }
public string Message { get; set; }
public ErrorType Type { get; set; }

public static Error NotFound(string code, string message)
=> new(code, message, ErrorType.NotFound);

public static Error Failure(string code, string message)
=> new(code, message, ErrorType.Failure);

public static Error Validation(string code, string message)
=> new(code, message, ErrorType.Validation);


}

public enum ErrorType : byte
{
Validation = 0,
Failure = 1,
NotFound = 2,
None = 4
}
5 changes: 5 additions & 0 deletions src/SwiftLink.Shared/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static Result<T> Failure<T>(Error error)

public static Result<T> Success<T>(T data)
=> Result<T>.Success(data);

public static ProblemDetail Problem(int status, string type, string title, string detail)
{
throw new NotImplementedException();
}
}

public class Result<T> : Result
Expand Down

0 comments on commit f74377c

Please sign in to comment.