Skip to content

Commit b1df300

Browse files
committed
Better error messages for wasm network errors
1 parent 85b406f commit b1df300

3 files changed

Lines changed: 71 additions & 30 deletions

File tree

src/wasm/response.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,16 @@ bool wasm_http_response::has_header_starts_with(const std::string& key, std::str
7171
return false;
7272
}
7373

74+
void wasm_http_response::set_git_error(std::string_view url) const
75+
{
76+
std::string extra = m_status_text.empty() ? "" : " (" + m_status_text + ")";
77+
git_error_set(
78+
GIT_ERROR_HTTP,
79+
"unexpected HTTP response %d%s to request %s, see the browser console and/or network tab for more details",
80+
m_status,
81+
extra.c_str(),
82+
url.data()
83+
);
84+
}
85+
7486
#endif // EMSCRIPTEN

src/wasm/response.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class wasm_http_response
2727

2828
bool has_header_starts_with(const std::string& key, std::string_view start) const;
2929

30+
void set_git_error(std::string_view url) const;
31+
3032
char* m_buffer; // Not owned.
3133
size_t m_buffer_size;
3234
size_t* m_bytes_read; // Not owned.

src/wasm/stream.cpp

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ EM_JS(void, js_delete_request, (int request_index), {
3131

3232
// Return the latest error string set in JS. Caller must delete the returned const char*.
3333
EM_JS(const char*, js_get_error, (void), {
34-
// clang-format off
35-
const err = Module["git2cpp_js_error"] ?? "";
36-
// clang-format on
37-
return stringToNewUTF8(err);
34+
const err = Module["git2cpp_js_error"];
35+
if (!err)
36+
{
37+
return stringToNewUTF8("unknown error");
38+
}
39+
let msg = err.message;
40+
if (err.name)
41+
{
42+
msg = err.name + ": " + msg;
43+
}
44+
return stringToNewUTF8(msg);
3845
});
3946

4047
EM_JS(
@@ -90,7 +97,9 @@ EM_JS(
9097
catch (err)
9198
{
9299
// Store error for later retrieval
93-
Module["git2cpp_js_error"] = String(err);
100+
// clang-format off
101+
Module["git2cpp_js_error"] = { name: err.name ?? "", message : err.message ?? "" };
102+
// clang-format on
94103
console.error(err);
95104
return -1;
96105
}
@@ -190,7 +199,9 @@ EM_JS(
190199
catch (err)
191200
{
192201
// Store error for later retrieval
193-
Module["git2cpp_js_error"] = String(err);
202+
// clang-format off
203+
Module["git2cpp_js_error"] = { name: err.name ?? "", message : err.message ?? "" };
204+
// clang-format on
194205
console.error(err);
195206
return -1;
196207
}
@@ -225,7 +236,9 @@ EM_JS(size_t, js_write, (int request_index, const char* buffer, size_t buffer_si
225236
catch (err)
226237
{
227238
// Store error for later retrieval
228-
Module["git2cpp_js_error"] = String(err);
239+
// clang-format off
240+
Module["git2cpp_js_error"] = { name: err.name ?? "", message : err.message ?? "" };
241+
// clang-format on
229242
console.error(err);
230243
return -1;
231244
}
@@ -242,11 +255,22 @@ static std::string base64_encode(std::string_view str)
242255
return ret;
243256
}
244257

245-
static void convert_js_to_git_error(void)
258+
static void convert_js_to_git_error(wasm_http_stream* stream)
246259
{
247260
// Convert error on JS side to git error.
248261
const char* error_str = js_get_error();
249-
git_error_set(GIT_ERROR_HTTP, "%s", error_str);
262+
if (std::string_view(error_str).starts_with("NetworkError:"))
263+
{
264+
git_error_set(
265+
GIT_ERROR_HTTP,
266+
"network error sending request to %s, see the browser console and/or network tab for more details",
267+
stream->m_unconverted_url.c_str()
268+
);
269+
}
270+
else
271+
{
272+
git_error_set(GIT_ERROR_HTTP, "%s", error_str);
273+
}
250274
delete error_str; // Delete const char* allocated in JavaScript.
251275
}
252276

@@ -278,21 +302,29 @@ static int read(wasm_http_stream* stream, wasm_http_response& response, bool is_
278302
// Response from a write.
279303
if (stream->m_request_index == -1)
280304
{
281-
git_error_set(GIT_ERROR_HTTP, "read_response called without pending request");
305+
git_error_set(
306+
GIT_ERROR_HTTP,
307+
"read_response called without pending request to %s",
308+
stream->m_unconverted_url.c_str()
309+
);
282310
return -1;
283311
}
284312
}
285313
else
286314
{
287315
if (stream->m_request_index != -1)
288316
{
289-
git_error_set(GIT_ERROR_HTTP, "read called with pending request");
317+
git_error_set(
318+
GIT_ERROR_HTTP,
319+
"read called with pending request to %s",
320+
stream->m_unconverted_url.c_str()
321+
);
290322
return -1;
291323
}
292324

293325
if (create_request(stream, stream->m_service.m_response_type.c_str()) < 0)
294326
{
295-
convert_js_to_git_error();
327+
convert_js_to_git_error(stream);
296328
return -1;
297329
}
298330
}
@@ -311,7 +343,7 @@ static int read(wasm_http_stream* stream, wasm_http_response& response, bool is_
311343
);
312344
if (bytes_read < 0)
313345
{
314-
convert_js_to_git_error();
346+
convert_js_to_git_error(stream);
315347
// Delete const char* allocated in JavaScript.
316348
delete status_text;
317349
delete response_headers;
@@ -346,8 +378,9 @@ static int read(wasm_http_stream* stream, wasm_http_response& response, bool is_
346378
// with it.
347379
git_error_set(
348380
GIT_ERROR_HTTP,
349-
"expected response content-type header '%s'",
350-
expected_response_type.c_str()
381+
"expected response content-type header '%s' to request %s",
382+
expected_response_type.c_str(),
383+
stream->m_unconverted_url.c_str()
351384
);
352385
return -1;
353386
}
@@ -364,15 +397,15 @@ static int write(wasm_http_stream* stream, const char* buffer, size_t buffer_siz
364397
// If there is not already a request opened, do so now.
365398
if (create_request(stream, stream->m_service.m_request_type.c_str()) < 0)
366399
{
367-
convert_js_to_git_error();
400+
convert_js_to_git_error(stream);
368401
return -1;
369402
}
370403
}
371404

372405
int error = js_write(stream->m_request_index, buffer, buffer_size);
373406
if (error < 0)
374407
{
375-
convert_js_to_git_error();
408+
convert_js_to_git_error(stream);
376409
return -1;
377410
}
378411

@@ -396,7 +429,11 @@ static int create_credential(wasm_http_stream* stream, const wasm_http_response&
396429
// Check that response headers show support for 'www-authenticate: Basic'.
397430
if (!response.has_header_starts_with("www-authenticate", "Basic"))
398431
{
399-
git_error_set(GIT_ERROR_HTTP, "remote host does not support Basic authentication");
432+
git_error_set(
433+
GIT_ERROR_HTTP,
434+
"remote host for request %s does not support Basic authentication",
435+
stream->m_unconverted_url.c_str()
436+
);
400437
return -1;
401438
}
402439

@@ -536,12 +573,7 @@ int wasm_http_stream_read(git_smart_subtransport_stream* s, char* buffer, size_t
536573

537574
if (response.m_status != GIT_HTTP_STATUS_OK)
538575
{
539-
git_error_set(
540-
GIT_ERROR_HTTP,
541-
"unexpected HTTP response: %d %s",
542-
response.m_status,
543-
response.m_status_text.c_str()
544-
);
576+
response.set_git_error(stream->m_unconverted_url);
545577
return -1;
546578
}
547579

@@ -560,12 +592,7 @@ int wasm_http_stream_read_response(git_smart_subtransport_stream* s, char* buffe
560592

561593
if (error == 0 && response.m_status != GIT_HTTP_STATUS_OK)
562594
{
563-
git_error_set(
564-
GIT_ERROR_HTTP,
565-
"unexpected HTTP response: %d %s",
566-
response.m_status,
567-
response.m_status_text.c_str()
568-
);
595+
response.set_git_error(stream->m_unconverted_url);
569596
error = -1;
570597
}
571598

0 commit comments

Comments
 (0)