Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
blairlearn opened this issue May 25, 2021 · 1 comment

Comments

@blairlearn
Copy link
Contributor

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
Copy link
Contributor Author

This will be addressed by NCIOCPL/NCI.OCPL.Api.Shared#50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant