Last active
April 20, 2021 23:42
-
-
Save ankitvijay/adf19124d24d7378ba2d28331ee567dc to your computer and use it in GitHub Desktop.
Example of inconsistent error code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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