Skip to content

Commit

Permalink
Merge pull request #5551 from MartinNowak/http_status_exception
Browse files Browse the repository at this point in the history
throw specific HTTPStatusException on http request errors
merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
  • Loading branch information
dlang-bot committed Jul 8, 2017
2 parents 0b6b743 + 97e9db7 commit 4cfe0f3
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions std/net/curl.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -4072,6 +4072,33 @@ class CurlTimeoutException : CurlException
}
}

/++
Exception thrown on HTTP request failures, e.g. 404 Not Found.
+/
class HTTPStatusException : CurlException
{
/++
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.
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;

Expand Down

0 comments on commit 4cfe0f3

Please sign in to comment.