Skip to content

Commit 3f980d8

Browse files
authored
Merge pull request #3 from carlopi/wasm_httplib_replacement2
Wasm httplib replacement2
2 parents ee56579 + a0caf01 commit 3f980d8

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/include/wasm_httplib_replacement.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <emscripten.h>
2+
#include "duckdb/common/exception.hpp"
23

34
namespace duckdb {
45

@@ -26,7 +27,11 @@ struct WasmClient {
2627
const xhr = new XMLHttpRequest();
2728
xhr.open("GET", url, false);
2829
xhr.responseType = "arraybuffer";
29-
xhr.send(null);
30+
try {
31+
xhr.send(null);
32+
} catch {
33+
return 0;
34+
}
3035
if (xhr.status != 200)
3136
return 0;
3237
var uInt8Array = xhr.response;
@@ -59,8 +64,8 @@ struct WasmClient {
5964
path.c_str());
6065

6166
if (!exe) {
62-
res._inner.status = 404;
63-
res._inner.reason = "Something went quack in Wasm land!";
67+
res._inner.status = 400;
68+
res._inner.reason = "Unknown error, something went quack in Wasm land! Please consult the console and or the docs at https://duckdb.org/community_extensions/extensions/webmacro";
6469
} else {
6570
res._inner.status = 200;
6671
uint64_t LEN = 0;
@@ -87,6 +92,8 @@ SetupHttpClient(const std::string &url) {
8792
}
8893

8994
static void HandleHttpError(const WasmResult &res,
90-
const std::string &request_type) {}
95+
const std::string &request_type) {
96+
throw HTTPException("Unknown problem while perfoming XMLHttpRequest");
97+
}
9198

9299
} // namespace duckdb

src/webmacro_extension.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static void LoadMacroFromUrlFunction(DataChunk &args, ExpressionState &state, Ve
168168
}
169169

170170
if (res->status != 200) {
171-
throw std::runtime_error("HTTP error " + std::to_string(res->status) + ": " + res->reason);
171+
throw HTTPException(std::to_string(res->status) + ": " + res->reason);
172172
}
173173

174174
// Get the SQL content
@@ -208,7 +208,9 @@ static void LoadMacroFromUrlFunction(DataChunk &args, ExpressionState &state, Ve
208208

209209
return StringVector::AddString(result, "Successfully loaded macro: " + macro_name);
210210

211-
} catch (std::exception &e) {
211+
} catch (duckdb::HTTPException &e) {
212+
throw;
213+
} catch (std::exception &e) {
212214
std::string error_msg = "Error: " + std::string(e.what());
213215
throw std::runtime_error(error_msg);
214216
}

0 commit comments

Comments
 (0)