Skip to content

Commit 433162a

Browse files
committed
error handling
1 parent e766a7f commit 433162a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
- Improve error messages on invalid sqlpage function calls. The messages now contain actionable advice.
1515
- Fix top navigation bar links color. They appeared "muted", with low contrast, since v0.33
1616
- update to apex charts v4.5.0. This fixes a bug where tick positions in scatter plots would be incorrect.
17+
- New function: `sqlpage.fetch_with_meta`
18+
- This function is similar to `sqlpage.fetch`, but it returns a json object with the following properties:
19+
- `status`: the http status code of the response.
20+
- `headers`: a json object with the response headers.
21+
- `body`: the response body.
22+
- `error`: an error message if the request failed.
23+
- This is useful when interacting with complex or unreliable external APIs.
1724

1825
## 0.33.0 (2025-02-15)
1926

src/webserver/database/sqlpage_functions/functions.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,13 @@ async fn fetch_with_meta(
240240
let mut obj = encoder.serialize_map(Some(3))?;
241241
match response_result {
242242
Ok(mut response) => {
243-
obj.serialize_entry("status", &response.status().as_u16())?;
243+
let status = response.status();
244+
obj.serialize_entry("status", &status.as_u16())?;
245+
let mut has_error = false;
246+
if status.is_server_error() {
247+
has_error = true;
248+
obj.serialize_entry("error", &format!("Server error: {status}"))?;
249+
}
244250

245251
let headers = response.headers();
246252

@@ -286,7 +292,12 @@ async fn fetch_with_meta(
286292
}
287293
Err(e) => {
288294
log::warn!("Failed to read response body: {e}");
289-
obj.serialize_entry("error", &format!("Failed to read response body: {e}"))?;
295+
if !has_error {
296+
obj.serialize_entry(
297+
"error",
298+
&format!("Failed to read response body: {e}"),
299+
)?;
300+
}
290301
}
291302
}
292303
}

0 commit comments

Comments
 (0)