2

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?

Community
  • 1
  • 1
Jake Smith
  • 2,332
  • 1
  • 30
  • 68
  • 1
    Did you check the [HttpStatusCode Enumeration docs](https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.110).aspx). Looks like BadGateway is in there... – maccettura Mar 06 '18 at 17:11

1 Answers1

3

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().

RichardMc
  • 844
  • 1
  • 9
  • 33
  • If things go well, I'm intending to return a collection of information. Is there a common return type that will allow me to send the Bad Gateway Status Code OR something like an `ObjectResult`? – Jake Smith Mar 06 '18 at 17:28
  • @JakeSmith I'm not really sure what you are asking for here. The return StatusCode(HttpStatusCode.BadGateway); I posted above will return you the desired status code. – RichardMc Mar 06 '18 at 17:35
  • Think about returning JSon object when API call fails. Have your custom object, construct when failure occur and serialize before responding from API. Refer https://stackoverflow.com/questions/22607031/how-to-return-json-error-msg-in-asp-net-web-api – LifeOfPi Mar 06 '18 at 17:37
  • @RichardMc, I get your answer and it makes sense. What I am asking is if there is a common type that will allow me to return this statuscode as you have given an example for when the control flow of the method happens the way it does because of an error, or an ObjectResult of the collection I was successful in retrieving from the 3rd party service. – Jake Smith Mar 06 '18 at 17:39
  • I apologize. IActionResult is indeed an interface that both of these implement. I incorrectly assumed that I was getting an error because of the return type of my method, but I actually had to cast the enum to an int. `StatusCode` takes an int and I don't see an extension method or anything that takes the `HttpStatusCode` enum as a parameter. Thanks! – Jake Smith Mar 06 '18 at 17:52
  • @JakeSmith Its weird you have to cast it. What IDE are you using? – RichardMc Mar 06 '18 at 17:57
  • @RichardMc - Visual Studio 2017. I thought that was weird too. But Resharper was NOT happy about it lol – Jake Smith Mar 06 '18 at 19:49