In the case of a failed call to git_remote_connect(), is there any way to get the response HTTP response status code? I can use git_error_last() to get an error message of "unexpected http status code: " followed by the HTTP response status code, but I need some way to just get the status code, rather than get a string with the code embedded in it.
Looking at the libgit2 code at http.c, it seems like the status code is discarded and there is no way for the caller to retrieve it. If that is indeed the case, any instructions as to making a feature request and/or related pull would be much appreciated.
Related
I would like this exception to be caused:
// ErrBodyNotAllowed is returned by ResponseWriter.Write calls
// when the HTTP method or response code does not permit a
// body.
ErrBodyNotAllowed = errors.New("http: request method or response status code does not allow body")
When I send HEAD request with body using fiddler I get 400/504 error codes, but I don't see any error log in my application.
I assume you're talking about a go server that you control. You should see this error returned when you call writer.Write() on a HEAD request. You need to handle it in your application by checking for that error. Find where you are calling Write, and check for the error, then display it to the user. You probably need to replace:
writer.Write(data)
with something like:
_,err := writer.Write(data)
Our system is accepting a text file upload and is supposed to have a pre-determined line count. If the line count doesn't match up, I want to send a warning of sorts back to the user asking to confirm that they want to upload anyway.
Is there a particular status code I can use for something like this?
You can use 422.
The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions.
You can check this out.
rest API Tutorial - HTTP Status Codes
I want to show an error to users of my web app. While handling a request, I might raise an HTTPError like this:
raise tornado.web.HTTPError(403, reason="You're not authorised")
When running it in my development environment, that results in a response status like this:
403 You're not authorised
But when I run it in production, I get:
403 Forbidden
Changing the serve_traceback and debug options doesn't help: the traceback is returned in the response body, but the status message is still just "Forbidden".
Why does it return the wrong message in production?
Tornado 4.1
Are you running behind some sort of proxy that may be replacing this string?
In general you shouldn't be using the reason string for anything you care about. Proxies often replace it, browsers don't do anything with it, and in HTTP/2 it's completely removed. The main reason Tornado allows this string to be customized at all is that if you want to use a non-standard error code we have to put something there, so the reason argument is required if your status code is not in httplib.
In Tornado, raise HTTPError(status) is to be used when all you care about is the status. When you want to send a message, use this pattern instead:
self.set_status(403)
self.write("You're not authorized")
# or self.render("error.html", reason="You're not authorized"))
return
# or raise tornado.web.Finish() if you're too deep in the stack to return
This is a theoretical question.
I believe I know the answer but I've received contradicting answers, so I figured I'd ask here.
On the W3C site it says:
Client Error 4xx
The 4xx class of status code is intended for cases in which the client
seems to have erred.
It also says
Server Error 5xx
Response status codes beginning with the digit "5" indicate cases in
which the server is aware that it has erred or is incapable of
performing the request.
I take this to mean that if a request is syntactically correct, but logically wrong, such as an attempt to create an object with an invalid value on a specific property, then my API should throw a 5xx Error, because the server DID understand the request, but found it to be invalid.
I have, on the other hand, been told that it should be a 4xx error (specifically 400 Bad Request) because the logical error was on the client side, as it sent an invalid value in the first place.
So, what error code SHOULD I be reporting?
5xx error will occur when the problem is on the server side. For example when you make a request with a method or protocol which is not understood by the server, when the proxy did not respond, etc. Per short: when the server was unable to fulfill the request.
In your example a 4xx error is more appropriate, because the request initiator is the source of the problem. More specific, "422 Unprocessable Entity" error is appropriate, because as RFC 4918 states:
The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions.
From various reasons some API designers are trying to limit themselves to a set of 3 - 5 status codes that will be used. In general this is done to ease the work for the API users, which sounds good, but sometimes this philosophy can have bigger implications.
For example, if I send a request to some API to add a new comment, I would expect a few things to be granted, like (but not limited to):
The request is POST or send me back a 405 status if not.
If the comment was added I will get back 201 response with a link to my new comment in the body.
What do I get sometimes ?
If the request method is not POST, I will get a 400 error.
If the request is POST, I will get back a 200 status and sometimes no link to my new comment.
Sounds confusing ? For me it does.
What HTTP status should I return if my script throws an exception?
200 OK
or
500 Internal Server Error
Let's say user request parameters are correct but there is a bug in my script which causes an error message to appear instead of a proper response (XML, JSON or other format). What should be the HTTP status?
500 Internal Server Error is the correct status if the error can't be fixed by the client changing their request.
Use any of the 4XX statuses if the client might be able to fix their request to avoid the error (or 404 if the resource wasn't found).
200 OK is not the appropriate status in almost any error situation, because then the client thinks things are running normally (which they are not) and may continue to make the same error-causing requests.
Familiarize yourself with the available status codes in RFC2616 and find one that most appropriately fits the situation.
It depends on why the exception is thrown since they can be used for almost any error. If it's thrown because some id in the URI is not found in the database I'd say 404. On the other hand if it's because the database is down I would throw a 500. If an exception is thrown but the resulting page would still be useful to the user I would say return 200.
Review the Status Code Definitions. 500 or 400 should do for general issues, however, the more detailed you can be then the more useful the returned status will be.