简体   繁体   中英

What response do I send when my web api can't perform an action due to 3rd party service

I'm working on a C# Web API written in dotnet core. I'm struggling with the return of an action that attempts to retrieve data from a 3rd party service on another server. In the event that something goes wrong while attempting to get data from said 3rd party service, the HTTP response that made the most sense to me when looking at the spec was 502 Bad Gateway because

The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.

But I can't find a way to return this in my action. IActionResult doesn't seem to have an implementation for this code. Am I thinking this through correctly?

Web Api contains a few shortcut methods you should use when possible but they only have a few for some of the more common errorcodes that are thrown.

The following will return just the StatusCode back:

return StatusCode(HttpStatusCode.BadGateway);

the most common returns would be :

return NotFound() // 404
return Ok() //200
return InternalServerError() //500

What you should be asking yourself is it absolutely necessary to return that exact error code. Does your front end need to know about it? If you are already handling logging of the exception in the API you should probably return something more generic like InternalServerError() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM