From 8aeaf5b33feddc23826853b628f0c53c34ef3ef0 Mon Sep 17 00:00:00 2001 From: VladKovtun99 Date: Wed, 13 Aug 2025 14:59:09 +0200 Subject: [PATCH 1/2] Add FailBadRequest methods to handle bad request scenarios --- .../CollectionResultT.Fail.cs | 38 +++++++++++---- .../Constants/ProblemConstants.cs | 7 +-- .../Result/Result.Fail.cs | 46 +++++++++++++++---- .../ResultT/ResultT.Fail.cs | 44 ++++++++++++++---- 4 files changed, 104 insertions(+), 31 deletions(-) diff --git a/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs b/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs index 1f0cec3..0a208b3 100644 --- a/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs +++ b/ManagedCode.Communication/CollectionResultT/CollectionResultT.Fail.cs @@ -33,13 +33,13 @@ public static CollectionResult Fail(string title) var problem = Problem.Create(title, title, (int)HttpStatusCode.InternalServerError); return Create(false, default, 0, 0, 0, problem); } - + public static CollectionResult Fail(string title, string detail) { var problem = Problem.Create(title, detail); return Create(false, default, 0, 0, 0, problem); } - + public static CollectionResult Fail(string title, string detail, HttpStatusCode status) { var problem = Problem.Create(title, detail, (int)status); @@ -50,7 +50,7 @@ public static CollectionResult Fail(Exception exception) { return new CollectionResult(false, default, 0, 0, 0, Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); } - + public static CollectionResult Fail(Exception exception, HttpStatusCode status) { return new CollectionResult(false, default, 0, 0, 0, Problem.Create(exception, (int)status)); @@ -61,6 +61,26 @@ public static CollectionResult FailValidation(params (string field, string me return new CollectionResult(false, default, 0, 0, 0, Problem.Validation(errors)); } + public static CollectionResult FailBadRequest() + { + var problem = Problem.Create( + ProblemConstants.Titles.BadRequest, + ProblemConstants.Messages.BadRequest, + (int)HttpStatusCode.BadRequest); + + return Create(false, default, 0, 0, 0, problem); + } + + public static CollectionResult FailBadRequest(string detail) + { + var problem = Problem.Create( + ProblemConstants.Titles.BadRequest, + detail, + (int)HttpStatusCode.BadRequest); + + return Create(false, default, 0, 0, 0, problem); + } + public static CollectionResult FailUnauthorized() { var problem = Problem.Create( @@ -70,7 +90,7 @@ public static CollectionResult FailUnauthorized() return Create(false, default, 0, 0, 0, problem); } - + public static CollectionResult FailUnauthorized(string detail) { var problem = Problem.Create( @@ -90,7 +110,7 @@ public static CollectionResult FailForbidden() return Create(false, default, 0, 0, 0, problem); } - + public static CollectionResult FailForbidden(string detail) { var problem = Problem.Create( @@ -110,7 +130,7 @@ public static CollectionResult FailNotFound() return Create(false, default, 0, 0, 0, problem); } - + public static CollectionResult FailNotFound(string detail) { var problem = Problem.Create( @@ -125,7 +145,7 @@ public static CollectionResult Fail(TEnum errorCode) where TEnum : Enu { return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode)); } - + public static CollectionResult Fail(TEnum errorCode, string detail) where TEnum : Enum { return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode, detail)); @@ -135,9 +155,9 @@ public static CollectionResult Fail(TEnum errorCode, HttpStatusCode st { return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode, errorCode.ToString(), (int)status)); } - + public static CollectionResult Fail(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum { return new CollectionResult(false, default, 0, 0, 0, Problem.Create(errorCode, detail, (int)status)); } -} \ No newline at end of file +} diff --git a/ManagedCode.Communication/Constants/ProblemConstants.cs b/ManagedCode.Communication/Constants/ProblemConstants.cs index 75f3fcb..390980d 100644 --- a/ManagedCode.Communication/Constants/ProblemConstants.cs +++ b/ManagedCode.Communication/Constants/ProblemConstants.cs @@ -29,6 +29,7 @@ public static class Titles /// public static class Messages { + public const string BadRequest = "The request could not be understood by the server due to malformed syntax."; public const string UnauthorizedAccess = "Authentication is required to access this resource."; public const string ForbiddenAccess = "You do not have permission to access this resource."; public const string ResourceNotFound = "The requested resource was not found."; @@ -44,7 +45,7 @@ public static class Types { public const string AboutBlank = "about:blank"; public const string ValidationFailed = "https://tools.ietf.org/html/rfc7231#section-6.5.1"; - + public static string HttpStatus(int statusCode) => $"https://httpstatuses.io/{statusCode}"; } @@ -83,7 +84,7 @@ public static class ExtensionKeys /// public const string OriginalExceptionType = "originalExceptionType"; } - + /// /// Field names for validation errors. /// @@ -94,4 +95,4 @@ public static class ValidationFields /// public const string General = "_general"; } -} \ No newline at end of file +} diff --git a/ManagedCode.Communication/Result/Result.Fail.cs b/ManagedCode.Communication/Result/Result.Fail.cs index 4fc7f20..db9cdd6 100644 --- a/ManagedCode.Communication/Result/Result.Fail.cs +++ b/ManagedCode.Communication/Result/Result.Fail.cs @@ -31,7 +31,7 @@ public static Result Fail(string title) var problem = Problem.Create(title, title, HttpStatusCode.InternalServerError); return Create(false, problem); } - + /// /// Creates a failed result with a title and detail. /// @@ -40,7 +40,7 @@ public static Result Fail(string title, string detail) var problem = Problem.Create(title, detail, HttpStatusCode.InternalServerError); return Create(false, problem); } - + /// /// Creates a failed result with a title, detail and status. /// @@ -57,7 +57,7 @@ public static Result Fail(Exception exception) { return Create(false, Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); } - + /// /// Creates a failed result from an exception with specific status. /// @@ -74,6 +74,32 @@ public static Result FailValidation(params (string field, string message)[] erro return new Result(false, Problem.Validation(errors)); } + /// + /// Creates a failed result for bad request. + /// + public static Result FailBadRequest() + { + var problem = Problem.Create( + ProblemConstants.Titles.BadRequest, + ProblemConstants.Messages.BadRequest, + (int)HttpStatusCode.BadRequest); + + return Create(false, problem); + } + + /// + /// Creates a failed result for bad request with custom detail. + /// + public static Result FailBadRequest(string detail) + { + var problem = Problem.Create( + ProblemConstants.Titles.BadRequest, + detail, + (int)HttpStatusCode.BadRequest); + + return Create(false, problem); + } + /// /// Creates a failed result for unauthorized access. /// @@ -86,7 +112,7 @@ public static Result FailUnauthorized() return Create(false, problem); } - + /// /// Creates a failed result for unauthorized access with custom detail. /// @@ -112,7 +138,7 @@ public static Result FailForbidden() return Create(false, problem); } - + /// /// Creates a failed result for forbidden access with custom detail. /// @@ -138,7 +164,7 @@ public static Result FailNotFound() return Create(false, problem); } - + /// /// Creates a failed result for not found with custom detail. /// @@ -159,7 +185,7 @@ public static Result Fail(TEnum errorCode) where TEnum : Enum { return Create(false, Problem.Create(errorCode)); } - + /// /// Creates a failed result from a custom error enum with detail. /// @@ -175,12 +201,12 @@ public static Result Fail(TEnum errorCode, HttpStatusCode status) where T { return Create(false, Problem.Create(errorCode, errorCode.ToString(), (int)status)); } - + /// /// Creates a failed result from a custom error enum with detail and specific HTTP status. - /// і + /// і public static Result Fail(TEnum errorCode, string detail, HttpStatusCode status) where TEnum : Enum { return Create(false, Problem.Create(errorCode, detail, (int)status)); } -} \ No newline at end of file +} diff --git a/ManagedCode.Communication/ResultT/ResultT.Fail.cs b/ManagedCode.Communication/ResultT/ResultT.Fail.cs index 2161e14..76ca4bf 100644 --- a/ManagedCode.Communication/ResultT/ResultT.Fail.cs +++ b/ManagedCode.Communication/ResultT/ResultT.Fail.cs @@ -43,7 +43,7 @@ public static Result Fail(string title) var problem = Problem.Create(title, title, (int)HttpStatusCode.InternalServerError); return Create(false, default, problem); } - + /// /// Creates a failed result with a title and detail. /// @@ -52,7 +52,7 @@ public static Result Fail(string title, string detail) var problem = Problem.Create(title, detail); return Create(false, default, problem); } - + /// /// Creates a failed result with a title, detail and status. /// @@ -69,7 +69,7 @@ public static Result Fail(Exception exception) { return new Result(false, default, Problem.Create(exception, (int)HttpStatusCode.InternalServerError)); } - + /// /// Creates a failed result from an exception with status. /// @@ -86,6 +86,32 @@ public static Result FailValidation(params (string field, string message)[] e return new Result(false, default, Problem.Validation(errors)); } + /// + /// Creates a failed result for bad request. + /// + public static Result FailBadRequest() + { + var problem = Problem.Create( + ProblemConstants.Titles.BadRequest, + ProblemConstants.Messages.BadRequest, + (int)HttpStatusCode.BadRequest); + + return Create(false, default, problem); + } + + /// + /// Creates a failed result for bad request with custom detail. + /// + public static Result FailBadRequest(string detail) + { + var problem = Problem.Create( + ProblemConstants.Titles.BadRequest, + detail, + (int)HttpStatusCode.BadRequest); + + return Create(false, default, problem); + } + /// /// Creates a failed result for unauthorized access. /// @@ -98,7 +124,7 @@ public static Result FailUnauthorized() return Create(false, default, problem); } - + /// /// Creates a failed result for unauthorized access with custom detail. /// @@ -124,7 +150,7 @@ public static Result FailForbidden() return Create(false, default, problem); } - + /// /// Creates a failed result for forbidden access with custom detail. /// @@ -150,7 +176,7 @@ public static Result FailNotFound() return Create(false, default, problem); } - + /// /// Creates a failed result for not found with custom detail. /// @@ -171,7 +197,7 @@ public static Result Fail(TEnum errorCode) where TEnum : Enum { return new Result(false, default, Problem.Create(errorCode)); } - + /// /// Creates a failed result from a custom error enum with detail. /// @@ -187,7 +213,7 @@ public static Result Fail(TEnum errorCode, HttpStatusCode status) wher { return new Result(false, default, Problem.Create(errorCode, errorCode.ToString(), (int)status)); } - + /// /// Creates a failed result from a custom error enum with detail and specific HTTP status. /// @@ -195,4 +221,4 @@ public static Result Fail(TEnum errorCode, string detail, HttpStatusCo { return new Result(false, default, Problem.Create(errorCode, detail, (int)status)); } -} \ No newline at end of file +} From b0d255595918f04c5a42ee6516d3392cd0d40eb5 Mon Sep 17 00:00:00 2001 From: VladKovtun99 Date: Wed, 13 Aug 2025 15:06:02 +0200 Subject: [PATCH 2/2] Update version --- Directory.Build.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index da50def..8c0f03c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -26,8 +26,8 @@ https://github.com/managedcode/Communication https://github.com/managedcode/Communication Managed Code - Communication - 9.6.0 - 9.6.0 + 9.6.1 + 9.6.1