Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Feb 24, 2025
1 parent e766a7f commit 433162a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
- Improve error messages on invalid sqlpage function calls. The messages now contain actionable advice.
- Fix top navigation bar links color. They appeared "muted", with low contrast, since v0.33
- update to apex charts v4.5.0. This fixes a bug where tick positions in scatter plots would be incorrect.
- New function: `sqlpage.fetch_with_meta`
- This function is similar to `sqlpage.fetch`, but it returns a json object with the following properties:
- `status`: the http status code of the response.
- `headers`: a json object with the response headers.
- `body`: the response body.
- `error`: an error message if the request failed.
- This is useful when interacting with complex or unreliable external APIs.

## 0.33.0 (2025-02-15)

Expand Down
15 changes: 13 additions & 2 deletions src/webserver/database/sqlpage_functions/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,13 @@ async fn fetch_with_meta(
let mut obj = encoder.serialize_map(Some(3))?;
match response_result {
Ok(mut response) => {
obj.serialize_entry("status", &response.status().as_u16())?;
let status = response.status();
obj.serialize_entry("status", &status.as_u16())?;
let mut has_error = false;
if status.is_server_error() {
has_error = true;
obj.serialize_entry("error", &format!("Server error: {status}"))?;
}

let headers = response.headers();

Expand Down Expand Up @@ -286,7 +292,12 @@ async fn fetch_with_meta(
}
Err(e) => {
log::warn!("Failed to read response body: {e}");
obj.serialize_entry("error", &format!("Failed to read response body: {e}"))?;
if !has_error {
obj.serialize_entry(
"error",
&format!("Failed to read response body: {e}"),
)?;
}
}
}
}
Expand Down

0 comments on commit 433162a

Please sign in to comment.