Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active April 20, 2021 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/adf19124d24d7378ba2d28331ee567dc to your computer and use it in GitHub Desktop.
Save ankitvijay/adf19124d24d7378ba2d28331ee567dc to your computer and use it in GitHub Desktop.
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