Skip to content
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

Support for custom errors #29

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Support for custom errors #29

wants to merge 8 commits into from

Conversation

dafo
Copy link

@dafo dafo commented Apr 24, 2024

No description provided.

@dafo
Copy link
Author

dafo commented Apr 24, 2024

Here is how the swagger.json looks like after the change:
post1
post2

@dafo dafo marked this pull request as ready for review April 25, 2024 07:32
@dafo dafo requested review from Lipata, wnvko and dkalinovInfra April 25, 2024 07:33
NorthwindCRUD/Controllers/CustomersController.cs Outdated Show resolved Hide resolved
NorthwindCRUD/Controllers/CustomersController.cs Outdated Show resolved Hide resolved
NorthwindCRUD/Controllers/CustomersController.cs Outdated Show resolved Hide resolved
NorthwindCRUD/Controllers/CustomersController.cs Outdated Show resolved Hide resolved
@@ -76,7 +78,10 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id)
}

[HttpPost]
[Authorize]
[SwaggerResponse(400, "Your inputs do not pass validation!", typeof(Errors), "text/json")]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if model is not valid this will return:

return BadRequest(ModelState);

which will end up to something like this:
image

What should return this end point in this case is Errors. What you need to do is something like this:

                if (ModelState.IsValid == false)
                {
                    // TODO: Validate each field here
                    if (model.ContactName != "Infragistics")
                    {
                        return BadRequest(new Errors
                        {
                            CustomErrors = new ValidationError[]
                            {
                                new ValidationError
                                {
                                    DataField = nameof(model.ContactName),
                                    Message = "Contact Name is not valid",
                                    StatusCode = 401
                                },
                            },
                        });
                    }
                }

                var mappedModel = this.mapper.Map<CustomerDto, CustomerDb>(model);
                var customer = this.customerService.Create(mappedModel);
                return Ok(this.mapper.Map<CustomerDb, CustomerDto>(customer));

{
public class CustomError
{
public CustomError()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of empty constructor.

{
public class Errors
{
public Errors()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of empty constructor.

{
public class ValidationError : CustomError
{
public ValidationError()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of empty constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants