From f95a3beab3d74b845f42575686890783efeac2e7 Mon Sep 17 00:00:00 2001 From: Alexander M Date: Wed, 24 Apr 2024 14:32:59 +0300 Subject: [PATCH 1/8] Support for custom errors --- NorthwindCRUD/AuthenticationError.cs | 15 +++++++++++++++ .../Controllers/CustomersController.cs | 6 +++++- NorthwindCRUD/Errors.cs | 12 ++++++++++++ NorthwindCRUD/NorthwindCRUD.csproj | 1 + NorthwindCRUD/ValidationError.cs | 17 +++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 NorthwindCRUD/AuthenticationError.cs create mode 100644 NorthwindCRUD/Errors.cs create mode 100644 NorthwindCRUD/ValidationError.cs diff --git a/NorthwindCRUD/AuthenticationError.cs b/NorthwindCRUD/AuthenticationError.cs new file mode 100644 index 0000000..6f42398 --- /dev/null +++ b/NorthwindCRUD/AuthenticationError.cs @@ -0,0 +1,15 @@ +namespace NorthwindCRUD +{ + public class AuthenticationError + { + public AuthenticationError() + { + int statusCode = 401; + string message = "No! No! You are not authenticated!"; + } + + public int StatusCode { get; set; } + + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index 685fe2f..c3bb77d 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -3,9 +3,11 @@ using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using NorthwindCRUD; using NorthwindCRUD.Models.DbModels; using NorthwindCRUD.Models.Dtos; using NorthwindCRUD.Services; + using Swashbuckle.AspNetCore.Annotations; [ApiController] [Route("[controller]")] @@ -76,7 +78,9 @@ public ActionResult GetOrdersByCustomerId(string id) } [HttpPost] - [Authorize] + [SwaggerResponse(400, "No! No! Your inputs do not pass validation!", typeof(Errors), "text/json")] + [SwaggerResponse(401, "No! No! Not authenticated!", typeof(AuthenticationError), "text/json")] + // [Authorize] public ActionResult Create(CustomerDto model) { try diff --git a/NorthwindCRUD/Errors.cs b/NorthwindCRUD/Errors.cs new file mode 100644 index 0000000..8f6893f --- /dev/null +++ b/NorthwindCRUD/Errors.cs @@ -0,0 +1,12 @@ +namespace NorthwindCRUD +{ + public class Errors + { + public Errors() + { + ValidationError[] customErrors; + } + + public ValidationError[] CustomErrors { get; set; } + } +} diff --git a/NorthwindCRUD/NorthwindCRUD.csproj b/NorthwindCRUD/NorthwindCRUD.csproj index faf2e0a..de27111 100644 --- a/NorthwindCRUD/NorthwindCRUD.csproj +++ b/NorthwindCRUD/NorthwindCRUD.csproj @@ -24,6 +24,7 @@ + diff --git a/NorthwindCRUD/ValidationError.cs b/NorthwindCRUD/ValidationError.cs new file mode 100644 index 0000000..e418d82 --- /dev/null +++ b/NorthwindCRUD/ValidationError.cs @@ -0,0 +1,17 @@ +using System.ComponentModel; + +namespace NorthwindCRUD +{ + public class ValidationError + { + public ValidationError() + { + string dataField; + string message = "No! No! Your input does not pass validation!"; + } + + public string DataField { get; set; } + + public string Message { get; set; } + } +} From 89f0c83b1bdcb7ecc1bf8b2eca5295ea2bcf5160 Mon Sep 17 00:00:00 2001 From: Alexander M Date: Wed, 24 Apr 2024 14:44:40 +0300 Subject: [PATCH 2/8] Remove redundant package --- NorthwindCRUD/NorthwindCRUD.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/NorthwindCRUD/NorthwindCRUD.csproj b/NorthwindCRUD/NorthwindCRUD.csproj index de27111..faf2e0a 100644 --- a/NorthwindCRUD/NorthwindCRUD.csproj +++ b/NorthwindCRUD/NorthwindCRUD.csproj @@ -24,7 +24,6 @@ - From f120fd9a4a486ac644a4b7e69b21ccc9aee4a6ba Mon Sep 17 00:00:00 2001 From: Alexander M Date: Thu, 25 Apr 2024 10:26:55 +0300 Subject: [PATCH 3/8] Moving errors under 'Models' + ValidationError inherits CustomError --- NorthwindCRUD/AuthenticationError.cs | 15 --------------- .../Controllers/CustomersController.cs | 10 +++++++--- NorthwindCRUD/Models/Errors/CustomError.cs | 13 +++++++++++++ NorthwindCRUD/{ => Models/Errors}/Errors.cs | 3 +-- NorthwindCRUD/Models/Errors/ValidationError.cs | 13 +++++++++++++ NorthwindCRUD/ValidationError.cs | 17 ----------------- 6 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 NorthwindCRUD/AuthenticationError.cs create mode 100644 NorthwindCRUD/Models/Errors/CustomError.cs rename NorthwindCRUD/{ => Models/Errors}/Errors.cs (67%) create mode 100644 NorthwindCRUD/Models/Errors/ValidationError.cs delete mode 100644 NorthwindCRUD/ValidationError.cs diff --git a/NorthwindCRUD/AuthenticationError.cs b/NorthwindCRUD/AuthenticationError.cs deleted file mode 100644 index 6f42398..0000000 --- a/NorthwindCRUD/AuthenticationError.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace NorthwindCRUD -{ - public class AuthenticationError - { - public AuthenticationError() - { - int statusCode = 401; - string message = "No! No! You are not authenticated!"; - } - - public int StatusCode { get; set; } - - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index c3bb77d..bb0e738 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -3,9 +3,9 @@ using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - using NorthwindCRUD; using NorthwindCRUD.Models.DbModels; using NorthwindCRUD.Models.Dtos; + using NorthwindCRUD.Models.Errors; using NorthwindCRUD.Services; using Swashbuckle.AspNetCore.Annotations; @@ -79,7 +79,8 @@ public ActionResult GetOrdersByCustomerId(string id) [HttpPost] [SwaggerResponse(400, "No! No! Your inputs do not pass validation!", typeof(Errors), "text/json")] - [SwaggerResponse(401, "No! No! Not authenticated!", typeof(AuthenticationError), "text/json")] + [SwaggerResponse(401, "No! No! Not authenticated!", typeof(CustomError), "text/json")] + // [Authorize] public ActionResult Create(CustomerDto model) { @@ -130,7 +131,10 @@ public ActionResult Update(CustomerDto model) } [HttpDelete("{id}")] - [Authorize] + [SwaggerResponse(401, "No! No! Not authenticated!", typeof(CustomError), "text/json")] + [SwaggerResponse(404, "No! No! Your client is not found!", typeof(CustomError), "text/json")] + + // [Authorize] public ActionResult Delete(string id) { try diff --git a/NorthwindCRUD/Models/Errors/CustomError.cs b/NorthwindCRUD/Models/Errors/CustomError.cs new file mode 100644 index 0000000..a7defa1 --- /dev/null +++ b/NorthwindCRUD/Models/Errors/CustomError.cs @@ -0,0 +1,13 @@ +namespace NorthwindCRUD.Models.Errors +{ + public class CustomError + { + public CustomError() + { + } + + public int StatusCode { get; set; } + + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/NorthwindCRUD/Errors.cs b/NorthwindCRUD/Models/Errors/Errors.cs similarity index 67% rename from NorthwindCRUD/Errors.cs rename to NorthwindCRUD/Models/Errors/Errors.cs index 8f6893f..828e6be 100644 --- a/NorthwindCRUD/Errors.cs +++ b/NorthwindCRUD/Models/Errors/Errors.cs @@ -1,10 +1,9 @@ -namespace NorthwindCRUD +namespace NorthwindCRUD.Models.Errors { public class Errors { public Errors() { - ValidationError[] customErrors; } public ValidationError[] CustomErrors { get; set; } diff --git a/NorthwindCRUD/Models/Errors/ValidationError.cs b/NorthwindCRUD/Models/Errors/ValidationError.cs new file mode 100644 index 0000000..f95316a --- /dev/null +++ b/NorthwindCRUD/Models/Errors/ValidationError.cs @@ -0,0 +1,13 @@ +using System.ComponentModel; + +namespace NorthwindCRUD.Models.Errors +{ + public class ValidationError : CustomError + { + public ValidationError() + { + } + + public string DataField { get; set; } + } +} diff --git a/NorthwindCRUD/ValidationError.cs b/NorthwindCRUD/ValidationError.cs deleted file mode 100644 index e418d82..0000000 --- a/NorthwindCRUD/ValidationError.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.ComponentModel; - -namespace NorthwindCRUD -{ - public class ValidationError - { - public ValidationError() - { - string dataField; - string message = "No! No! Your input does not pass validation!"; - } - - public string DataField { get; set; } - - public string Message { get; set; } - } -} From 46e885f62c4d74e82c8f74b80f62320f0475b9bd Mon Sep 17 00:00:00 2001 From: Alexander M Date: Thu, 25 Apr 2024 11:33:09 +0300 Subject: [PATCH 4/8] Updating custom response descriptions --- NorthwindCRUD/Controllers/CustomersController.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index bb0e738..aacdce0 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -78,8 +78,8 @@ public ActionResult GetOrdersByCustomerId(string id) } [HttpPost] - [SwaggerResponse(400, "No! No! Your inputs do not pass validation!", typeof(Errors), "text/json")] - [SwaggerResponse(401, "No! No! Not authenticated!", typeof(CustomError), "text/json")] + [SwaggerResponse(400, "Your inputs do not pass validation!", typeof(Errors), "text/json")] + [SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")] // [Authorize] public ActionResult Create(CustomerDto model) @@ -131,8 +131,8 @@ public ActionResult Update(CustomerDto model) } [HttpDelete("{id}")] - [SwaggerResponse(401, "No! No! Not authenticated!", typeof(CustomError), "text/json")] - [SwaggerResponse(404, "No! No! Your client is not found!", typeof(CustomError), "text/json")] + [SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")] + [SwaggerResponse(404, "Your client is not found!", typeof(CustomError), "text/json")] // [Authorize] public ActionResult Delete(string id) From 3058f5eac0230bed54a43b16d538121f79bdf917 Mon Sep 17 00:00:00 2001 From: Alexander M Date: Wed, 15 May 2024 12:06:05 +0300 Subject: [PATCH 5/8] Update NorthwindCRUD/Controllers/CustomersController.cs Co-authored-by: Zdravko Kolev --- NorthwindCRUD/Controllers/CustomersController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index aacdce0..4bb6d3b 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -78,7 +78,7 @@ public ActionResult GetOrdersByCustomerId(string id) } [HttpPost] - [SwaggerResponse(400, "Your inputs do not pass validation!", typeof(Errors), "text/json")] + [SwaggerResponse(400, "Invalid input data!", typeof(Errors), "text/json")] [SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")] // [Authorize] From fb91f08f93c645213e913fb717385f70d0e5de36 Mon Sep 17 00:00:00 2001 From: Alexander M Date: Wed, 15 May 2024 12:06:29 +0300 Subject: [PATCH 6/8] Update NorthwindCRUD/Controllers/CustomersController.cs Co-authored-by: Zdravko Kolev --- NorthwindCRUD/Controllers/CustomersController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index 4bb6d3b..467d7e1 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -79,7 +79,7 @@ public ActionResult GetOrdersByCustomerId(string id) [HttpPost] [SwaggerResponse(400, "Invalid input data!", typeof(Errors), "text/json")] - [SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")] + [SwaggerResponse(401, "Unauthorized!", typeof(CustomError), "text/json")] // [Authorize] public ActionResult Create(CustomerDto model) From a7f8f19dba8a98d82a02a4e66d07327f03707155 Mon Sep 17 00:00:00 2001 From: Alexander M Date: Wed, 15 May 2024 12:06:41 +0300 Subject: [PATCH 7/8] Update NorthwindCRUD/Controllers/CustomersController.cs Co-authored-by: Zdravko Kolev --- NorthwindCRUD/Controllers/CustomersController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index 467d7e1..7f3fb28 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -131,7 +131,7 @@ public ActionResult Update(CustomerDto model) } [HttpDelete("{id}")] - [SwaggerResponse(401, "Not authenticated!", typeof(CustomError), "text/json")] + [SwaggerResponse(401, "Unauthorized!", typeof(CustomError), "text/json")] [SwaggerResponse(404, "Your client is not found!", typeof(CustomError), "text/json")] // [Authorize] From ea2082a65e5793762da3c359483fd632bf76e9f9 Mon Sep 17 00:00:00 2001 From: Alexander M Date: Wed, 15 May 2024 12:06:56 +0300 Subject: [PATCH 8/8] Update NorthwindCRUD/Controllers/CustomersController.cs Co-authored-by: Zdravko Kolev --- NorthwindCRUD/Controllers/CustomersController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NorthwindCRUD/Controllers/CustomersController.cs b/NorthwindCRUD/Controllers/CustomersController.cs index 7f3fb28..a64616c 100644 --- a/NorthwindCRUD/Controllers/CustomersController.cs +++ b/NorthwindCRUD/Controllers/CustomersController.cs @@ -132,7 +132,7 @@ public ActionResult Update(CustomerDto model) [HttpDelete("{id}")] [SwaggerResponse(401, "Unauthorized!", typeof(CustomError), "text/json")] - [SwaggerResponse(404, "Your client is not found!", typeof(CustomError), "text/json")] + [SwaggerResponse(404, "Customer not found!", typeof(CustomError), "text/json")] // [Authorize] public ActionResult Delete(string id)