-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: main
Are you sure you want to change the base?
Conversation
@@ -76,7 +78,10 @@ public ActionResult<OrderDto[]> GetOrdersByCustomerId(string id) | |||
} | |||
|
|||
[HttpPost] | |||
[Authorize] | |||
[SwaggerResponse(400, "Your inputs do not pass validation!", typeof(Errors), "text/json")] |
There was a problem hiding this comment.
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:
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
Co-authored-by: Zdravko Kolev <[email protected]>
Co-authored-by: Zdravko Kolev <[email protected]>
Co-authored-by: Zdravko Kolev <[email protected]>
Co-authored-by: Zdravko Kolev <[email protected]>
No description provided.