NCIOCPL / NCI.OCPL.Api.Shared

Common code shared between APIs
0 stars 1 forks source link

Enabler: Convert controller actions to use ActionResult<T> for error handling. #7

Closed blairlearn closed 1 year ago

blairlearn commented 3 years ago

Our controllers throw APIErrorException for errors and a global exception handler to return a suitable HTTP status code. A consequence of this arrangement is that all non-200 status codes (e.g. "Not Found") are recorded in the console log as an unhandled exception. If we turn on logging, this will result in a log entry for every attempt to retrieve a non-existent value.

Starting with ASP.NET Core 2.1, it is now possible to wrap the return type in an ActionResult<T> and report the status code without throwing an exception. (For asynchronous code, this may be wrapped in a Task<T>.)

Example

    public async Task<ActionResult<string>> Exists(int id)
    {
        bool found = await svcItemExists(id);
        if(found)
            // Alternatively: return Ok("Found it");
            return "Found it";
        else
            return NotFound();
    }

ESTIMATE TBD

Resources:

Prerequisites

Sub-Tasks

Notes

blairlearn commented 1 year ago

This will be handled by #50