Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of inconsistent error code
[HttpPatch]
public async Task<IActionResult> Update([FromBody] OrderDto order)
{
try
{
if (!ModelState.IsValid)
{
// Returns response of type `ModelState` on "Bad Request"
return BadRequest(ModelState);
}
if (DoSomeCustomValidation(order))
{
// Returns pain-text on "Bad Request"
return BadRequest("Order is invalid");
}
if (!await _repository.OrderExists(order.Id))
{
// Returns pain-text on "Not Found"
return NotFound("Order is not found");
}
await _repository.UpdateOrder(order);
return NoContent();
}
catch (Exception e)
{
// Only returns status code 500 on unhandled error with empty response
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment