In my service code, i am validating my objects and if it fails, it throws a ValidationException (using FluentValidation), this in turns throws an exception with error code 500.
Without going through all my methods and adding a try..catch, is it possible to add something in the mvc pipeline that will see if a ValidationException has been thrown, and if so return a BadRequest with the Message of the ValidationException in?
If i were to update all my methods, it would look something like this
try
{
await _service.AddAsync(entity);
// return created response
return Created(_httpContextAccessor.HttpContext.Request.GetDisplayUrl(), _mapper.Map<DatasheetModel>(entity));
}
catch (ValidationException e)
{
return BadRequest(e.Message);
}
Is this possible, recommended, or should i just update all my methods as above?