Skip to content

Commit

Permalink
add url info into async request error log
Browse files Browse the repository at this point in the history
  • Loading branch information
lixin1234qqq committed Sep 4, 2020
1 parent dfad0e7 commit 4e1398d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions base/request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ HTTPRequest::HTTPRequest(v8::Isolate* isolate, v8::Local<v8::Value> conf) {
v8::HandleScope handle_scope(isolate);
auto tmp = config->Get(context, NewV8Key(isolate, "url")).FromMaybe(undefined);
if (tmp->IsString()) {
SetUrl(*v8::String::Utf8Value(isolate, tmp));
url = *v8::String::Utf8Value(isolate, tmp);
SetUrl(url);
}
}
{
Expand Down Expand Up @@ -220,6 +221,10 @@ HTTPResponse HTTPRequest::GetResponse() {
}
}

std::string HTTPRequest::GetUrl() const {
return this->url;
}

size_t AsyncRequest::pool_size = 1;
size_t AsyncRequest::queue_cap = 1;

Expand All @@ -229,10 +234,11 @@ bool AsyncRequest::Submit(std::shared_ptr<HTTPRequest> request) {
return pool->Post([request]() {
auto response = request->GetResponse();
if (response.error) {
Platform::logger(std::string("async request failed: ") + response.error.message + std::string("\n"));
Platform::logger(std::string("async request failed; url: ") + request->GetUrl() +
", errMsg: " + response.error.message + std::string("\n"));
} else if (response.status_code != 200) {
Platform::logger(std::string("async request status: ") + std::to_string(response.status_code) +
std::string(" body: ") + response.text.substr(0, 1024) + std::string("\n"));
Platform::logger(std::string("async request status: ") + std::to_string(response.status_code) + ", url: " +
request->GetUrl() + ", body: " + response.text.substr(0, 1024) + std::string("\n"));
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions base/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ class HTTPRequest : public cpr::Session {
HTTPRequest(v8::Isolate* isolate, v8::Local<v8::Value> conf);
void SetMethod(const std::string& method) { this->method = method; }
HTTPResponse GetResponse();
std::string GetUrl() const;

private:
std::string method;
std::string url;
std::string error;
};

Expand Down
4 changes: 2 additions & 2 deletions base/tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,8 @@ TEST_CASE("AsyncRequest") {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
REQUIRE_THAT(message,
Catch::Matchers::Contains(
"async request failed: Uncaught TypeError: Cannot convert undefined or null to object\n"
"async request failed: Uncaught TypeError: Cannot convert undefined or null to object\n"));
"async request failed; url: , errMsg: Uncaught TypeError: Cannot convert undefined or null to object\n"
"async request failed; url: , errMsg: Uncaught TypeError: Cannot convert undefined or null to object\n"));
}

SECTION("400", "[!mayfail]") {
Expand Down

0 comments on commit 4e1398d

Please sign in to comment.