Skip to content

Commit 4e1398d

Browse files
committed
add url info into async request error log
1 parent dfad0e7 commit 4e1398d

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

base/request.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ HTTPRequest::HTTPRequest(v8::Isolate* isolate, v8::Local<v8::Value> conf) {
7676
v8::HandleScope handle_scope(isolate);
7777
auto tmp = config->Get(context, NewV8Key(isolate, "url")).FromMaybe(undefined);
7878
if (tmp->IsString()) {
79-
SetUrl(*v8::String::Utf8Value(isolate, tmp));
79+
url = *v8::String::Utf8Value(isolate, tmp);
80+
SetUrl(url);
8081
}
8182
}
8283
{
@@ -220,6 +221,10 @@ HTTPResponse HTTPRequest::GetResponse() {
220221
}
221222
}
222223

224+
std::string HTTPRequest::GetUrl() const {
225+
return this->url;
226+
}
227+
223228
size_t AsyncRequest::pool_size = 1;
224229
size_t AsyncRequest::queue_cap = 1;
225230

@@ -229,10 +234,11 @@ bool AsyncRequest::Submit(std::shared_ptr<HTTPRequest> request) {
229234
return pool->Post([request]() {
230235
auto response = request->GetResponse();
231236
if (response.error) {
232-
Platform::logger(std::string("async request failed: ") + response.error.message + std::string("\n"));
237+
Platform::logger(std::string("async request failed; url: ") + request->GetUrl() +
238+
", errMsg: " + response.error.message + std::string("\n"));
233239
} else if (response.status_code != 200) {
234-
Platform::logger(std::string("async request status: ") + std::to_string(response.status_code) +
235-
std::string(" body: ") + response.text.substr(0, 1024) + std::string("\n"));
240+
Platform::logger(std::string("async request status: ") + std::to_string(response.status_code) + ", url: " +
241+
request->GetUrl() + ", body: " + response.text.substr(0, 1024) + std::string("\n"));
236242
}
237243
});
238244
}

base/request.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ class HTTPRequest : public cpr::Session {
3838
HTTPRequest(v8::Isolate* isolate, v8::Local<v8::Value> conf);
3939
void SetMethod(const std::string& method) { this->method = method; }
4040
HTTPResponse GetResponse();
41+
std::string GetUrl() const;
4142

4243
private:
4344
std::string method;
45+
std::string url;
4446
std::string error;
4547
};
4648

base/tests/main.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,8 @@ TEST_CASE("AsyncRequest") {
730730
std::this_thread::sleep_for(std::chrono::milliseconds(100));
731731
REQUIRE_THAT(message,
732732
Catch::Matchers::Contains(
733-
"async request failed: Uncaught TypeError: Cannot convert undefined or null to object\n"
734-
"async request failed: Uncaught TypeError: Cannot convert undefined or null to object\n"));
733+
"async request failed; url: , errMsg: Uncaught TypeError: Cannot convert undefined or null to object\n"
734+
"async request failed; url: , errMsg: Uncaught TypeError: Cannot convert undefined or null to object\n"));
735735
}
736736

737737
SECTION("400", "[!mayfail]") {

0 commit comments

Comments
 (0)