Respond with a reasonable error message in a case when _No connection could be made because the target machine actively refused it_. And add an extension point to override the message and status code (e. g. 502 Bad Gateway) for this case of course.
The error usually looks like the following in logs:
warn: Ocelot.Requester.Middleware.HttpRequesterMiddleware[0]
requestId: 0HLGKQK19POEE:00000002, previousRequestId: no previous request id, message: Error making http request, exception: System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Ocelot.Requester.HttpClientHttpRequester.GetResponse(DownstreamContext context)
More smooth UX.
Reroute to a host/port, which doesn't run a web server.
@Deilan what would you suggest for the error message? Also why do we need an extension point? This error does not get returned to users? :o
@TomPallister
"_No connection could be made because the target machine actively refused it_" would be pretty good,
This error does get returned to users, the message and status could be overrided, and message could be omitted at all (like how it works now).
@Deilan can you give me an example of where this error gets returned to a user? I cannot replicate the issue! When Ocelot records an error it appends to a list of errors internally and then before returning it logs the errors, maps the errors into a status code and returns an empty response.
@TomPallister It's a feature request, not a bug report. Currently the end-user just receives an empty body with 500 status code.
Or are you interested in a log file?
@Deilan I understand that it is a feature request but I don't have much free time so I try and get straight to the point!
I don't fully understand what it is you are suggesting.
I get that you want a smoother error message, but in the logs surely you want everything? Not just the message e.g. "No connection could be made because the target machine actively refused it", you want the stack etc. Which is what we currently have.
Are you saying when we get a 500 there should be an extension point that lets the developer specify what should be returned to users? This would make sense, friendly error messages for users of an API.
I'm not sure about what error codes should be returned in what circumstances. If there is an error calling a downstream service I just want to return a 500, I'm not sure how you know this should be a 502? The users request is to Ocelot so if Ocelot gets an error calling downstream it is just a server error really? Ocelot worked fine it was calling Downstream that didn't work?
We should probably look at how other API gateways handle this.
@TomPallister when I had issues with Cloudflare not being able to connect to my server I was getting a 502 error. Maybe @Deilan was looking at it from the same point of view of Cloudflare?
According to https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 502 is Bad Gateway - The server was acting as a gateway or proxy and received an invalid response from the upstream server. This seems to fit the bill for what you'd use Ocelot for.
This is the Cloudflare page on this topic https://support.cloudflare.com/hc/en-us/articles/218378978-What-should-I-do-after-seeing-a-502-or-504-gateway-error-on-my-site-
Any news on this? This is a pretty important thing for a gateway to implement properly.
Returning 500 Internal Error tells nothing useful to the client; Ocelot should really return 502 Bad Gateway when it can't reach the downstream server.
I have more than one machine, one of which is out of order, how to find the available machine automatically(Using static routing tables)
fixed by #1149