-
-
Notifications
You must be signed in to change notification settings - Fork 753
throw specific HTTPStatusException on http request errors #5551
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1047,9 +1047,8 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien | |
| }; | ||
| client.onReceiveStatusLine = (HTTP.StatusLine l) { statusLine = l; }; | ||
| client.perform(); | ||
| enforce!CurlException(statusLine.code / 100 == 2, | ||
| format("HTTP request returned status code %d (%s)", | ||
| statusLine.code, statusLine.reason)); | ||
| enforce(statusLine.code / 100 == 2, new HTTPStatusException(statusLine.code, | ||
| format("HTTP request returned status code %d (%s)", statusLine.code, statusLine.reason))); | ||
|
|
||
| return _decodeContent!T(content.data, client.p.charset); | ||
| } | ||
|
|
@@ -1063,8 +1062,9 @@ private auto _basicHTTP(T)(const(char)[] url, const(void)[] sendData, HTTP clien | |
| assert(req.hdrs.canFind("GET /path")); | ||
| s.send(httpNotFound()); | ||
| }); | ||
| auto e = collectException!CurlException(get(testServer.addr ~ "/path")); | ||
| auto e = collectException!HTTPStatusException(get(testServer.addr ~ "/path")); | ||
| assert(e.msg == "HTTP request returned status code 404 (Not Found)"); | ||
| assert(e.status == 404); | ||
| } | ||
|
|
||
| // Bugzilla 14760 - content length must be reset after post | ||
|
|
@@ -4058,6 +4058,33 @@ class CurlTimeoutException : CurlException | |
| } | ||
| } | ||
|
|
||
| /++ | ||
| Exception thrown on HTTP request failures, e.g. 404 Not Found. | ||
| +/ | ||
| class HTTPStatusException : CurlException | ||
| { | ||
| /++ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eh, bug in -cov |
||
| Params: | ||
| status = The HTTP status code. | ||
| msg = The message for the exception. | ||
| file = The file where the exception occurred. | ||
| line = The line number where the exception occurred. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eh, another bug in -cov
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks weird https://codecov.io/gh/dlang/phobos/compare/ecef6f0e3b3da14d483b3dd6e05d8780d16fd719...97e9db7f02ea072acb1a3f1bd11e3a1f3fca193a/changes, but if I run the single test locally dmd reports correct coverage. |
||
| next = The previous exception in the chain of exceptions, if any. | ||
| +/ | ||
| @safe pure nothrow | ||
| this(int status, | ||
| string msg, | ||
| string file = __FILE__, | ||
| size_t line = __LINE__, | ||
| Throwable next = null) | ||
| { | ||
| super(msg, file, line, next); | ||
| this.status = status; | ||
| } | ||
|
|
||
| immutable int status; /// The HTTP status code | ||
| } | ||
|
|
||
| /// Equal to $(REF CURLcode, etc,c,curl) | ||
| alias CurlCode = CURLcode; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message seems to be rather generic - what was the motivation for not generating this message in
HTTPStatusException(or providing a second overload)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the baseline of a useful error message but doesn't contain enough information on it's own (e.g. the url or the status reason), those are only available by the calling code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that was my point - if no specific message is passed, this could be the baseline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So... not sure what the proposal is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sth. like below could be handy, but I don't feel strongly about this (I am not using std.net.curl anyways):