FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Better error messages for wasm network errors by ianthomas23 · Pull Request #145 · QuantStack/git2cpp · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (2) .hpp  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions src/wasm/response.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@ bool wasm_http_response::has_header_starts_with(const std::string& key, std::str
return false;
}

void wasm_http_response::set_git_error(std::string_view url) const
{
std::string extra = m_status_text.empty() ? "" : " (" + m_status_text + ")";
git_error_set(
GIT_ERROR_HTTP,
"unexpected HTTP response %d%s to request %s, see the browser console and/or network tab for more details",
m_status,
extra.c_str(),
url.data()
);
}

#endif // EMSCRIPTEN
2 changes: 2 additions & 0 deletions src/wasm/response.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class wasm_http_response

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

void set_git_error(std::string_view url) const;

char* m_buffer; // Not owned.
size_t m_buffer_size;
size_t* m_bytes_read; // Not owned.
Expand Down
87 changes: 57 additions & 30 deletions src/wasm/stream.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ EM_JS(void, js_delete_request, (int request_index), {

// Return the latest error string set in JS. Caller must delete the returned const char*.
EM_JS(const char*, js_get_error, (void), {
// clang-format off
const err = Module["git2cpp_js_error"] ?? "";
// clang-format on
return stringToNewUTF8(err);
const err = Module["git2cpp_js_error"];
if (!err)
{
return stringToNewUTF8("unknown error");
}
let msg = err.message;
if (err.name)
{
msg = err.name + ": " + msg;
}
return stringToNewUTF8(msg);
});

EM_JS(
Expand Down Expand Up @@ -90,7 +97,9 @@ EM_JS(
catch (err)
{
// Store error for later retrieval
Module["git2cpp_js_error"] = String(err);
// clang-format off
Module["git2cpp_js_error"] = { name: err.name ?? "", message : err.message ?? "" };
// clang-format on
console.error(err);
return -1;
}
Expand Down Expand Up @@ -190,7 +199,9 @@ EM_JS(
catch (err)
{
// Store error for later retrieval
Module["git2cpp_js_error"] = String(err);
// clang-format off
Module["git2cpp_js_error"] = { name: err.name ?? "", message : err.message ?? "" };
// clang-format on
console.error(err);
return -1;
}
Expand Down Expand Up @@ -225,7 +236,9 @@ EM_JS(size_t, js_write, (int request_index, const char* buffer, size_t buffer_si
catch (err)
{
// Store error for later retrieval
Module["git2cpp_js_error"] = String(err);
// clang-format off
Module["git2cpp_js_error"] = { name: err.name ?? "", message : err.message ?? "" };
// clang-format on
console.error(err);
return -1;
}
Expand All @@ -242,11 +255,22 @@ static std::string base64_encode(std::string_view str)
return ret;
}

static void convert_js_to_git_error(void)
static void convert_js_to_git_error(wasm_http_stream* stream)
{
// Convert error on JS side to git error.
const char* error_str = js_get_error();
git_error_set(GIT_ERROR_HTTP, "%s", error_str);
if (std::string_view(error_str).starts_with("NetworkError:"))
{
git_error_set(
GIT_ERROR_HTTP,
"network error sending request to %s, see the browser console and/or network tab for more details",
stream->m_unconverted_url.c_str()
);
}
else
{
git_error_set(GIT_ERROR_HTTP, "%s", error_str);
}
delete error_str; // Delete const char* allocated in JavaScript.
}

Expand Down Expand Up @@ -278,21 +302,29 @@ static int read(wasm_http_stream* stream, wasm_http_response& response, bool is_
// Response from a write.
if (stream->m_request_index == -1)
{
git_error_set(GIT_ERROR_HTTP, "read_response called without pending request");
git_error_set(
GIT_ERROR_HTTP,
"read_response called without pending request to %s",
stream->m_unconverted_url.c_str()
);
return -1;
}
}
else
{
if (stream->m_request_index != -1)
{
git_error_set(GIT_ERROR_HTTP, "read called with pending request");
git_error_set(
GIT_ERROR_HTTP,
"read called with pending request to %s",
stream->m_unconverted_url.c_str()
);
return -1;
}

if (create_request(stream, stream->m_service.m_response_type.c_str()) < 0)
{
convert_js_to_git_error();
convert_js_to_git_error(stream);
return -1;
}
}
Expand All @@ -311,7 +343,7 @@ static int read(wasm_http_stream* stream, wasm_http_response& response, bool is_
);
if (bytes_read < 0)
{
convert_js_to_git_error();
convert_js_to_git_error(stream);
// Delete const char* allocated in JavaScript.
delete status_text;
delete response_headers;
Expand Down Expand Up @@ -346,8 +378,9 @@ static int read(wasm_http_stream* stream, wasm_http_response& response, bool is_
// with it.
git_error_set(
GIT_ERROR_HTTP,
"expected response content-type header '%s'",
expected_response_type.c_str()
"expected response content-type header '%s' to request %s",
expected_response_type.c_str(),
stream->m_unconverted_url.c_str()
);
return -1;
}
Expand All @@ -364,15 +397,15 @@ static int write(wasm_http_stream* stream, const char* buffer, size_t buffer_siz
// If there is not already a request opened, do so now.
if (create_request(stream, stream->m_service.m_request_type.c_str()) < 0)
{
convert_js_to_git_error();
convert_js_to_git_error(stream);
return -1;
}
}

int error = js_write(stream->m_request_index, buffer, buffer_size);
if (error < 0)
{
convert_js_to_git_error();
convert_js_to_git_error(stream);
return -1;
}

Expand All @@ -396,7 +429,11 @@ static int create_credential(wasm_http_stream* stream, const wasm_http_response&
// Check that response headers show support for 'www-authenticate: Basic'.
if (!response.has_header_starts_with("www-authenticate", "Basic"))
{
git_error_set(GIT_ERROR_HTTP, "remote host does not support Basic authentication");
git_error_set(
GIT_ERROR_HTTP,
"remote host for request %s does not support Basic authentication",
stream->m_unconverted_url.c_str()
);
return -1;
}

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

if (response.m_status != GIT_HTTP_STATUS_OK)
{
git_error_set(
GIT_ERROR_HTTP,
"unexpected HTTP response: %d %s",
response.m_status,
response.m_status_text.c_str()
);
response.set_git_error(stream->m_unconverted_url);
return -1;
}

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

if (error == 0 && response.m_status != GIT_HTTP_STATUS_OK)
{
git_error_set(
GIT_ERROR_HTTP,
"unexpected HTTP response: %d %s",
response.m_status,
response.m_status_text.c_str()
);
response.set_git_error(stream->m_unconverted_url);
error = -1;
}

Expand Down

Back | FazBrowse Home | New Git URL