Skip to content

Commit 576557b

Browse files
committed
Return JSON-formatted errors
1 parent 473665e commit 576557b

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ereadifier"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
edition = "2024"
55

66
[lints.clippy]
@@ -12,6 +12,7 @@ chrono = "0.4"
1212
futures-util = "0.3"
1313
image = { version = "0.25", default-features = false, features = ["default-formats"] }
1414
regex = "1.12"
15+
serde = { version = "1.0", features = ["derive"] }
1516
tokio = { version = "1", features = ["full"] }
1617
warp = { version = "0.4", features = ["multipart", "server"] }
1718
webp = "0.3"

ROADMAP.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Improvements I want to make, in no particular order:
44

55
* Add a health check endpoint (and move conversion to an explicit /convert endpoint)
6-
* Return structured JSON errors
76
* Make presets data-driven to ease expanding the list later:
87
- TOML file which defines the dimensions and a list of devices for each named preset
98
- Server loads TOML file when parsing dimensions

src/main.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bytes::BufMut;
22
use futures_util::StreamExt;
3+
use serde::Serialize;
34
use warp::Filter;
45

56
#[derive(Debug)]
@@ -15,6 +16,12 @@ impl warp::reject::Reject for EncodeError {}
1516
impl warp::reject::Reject for DecodeError {}
1617
impl warp::reject::Reject for NoInput {}
1718

19+
#[derive(Serialize)]
20+
struct ErrorDetails {
21+
code: u16,
22+
message: String,
23+
}
24+
1825
struct Config {
1926
dimensions: Option<(u32, u32)>,
2027
lossy_quality: f32,
@@ -325,7 +332,12 @@ async fn handle_errors(err: warp::Rejection) -> Result<impl warp::Reply, std::co
325332
message
326333
);
327334

328-
Ok::<warp::reply::WithStatus<&str>, _>(warp::reply::with_status(message, code))
335+
let json = warp::reply::json(&ErrorDetails {
336+
code: code.as_u16(),
337+
message: message.into(),
338+
});
339+
340+
Ok(warp::reply::with_status(json, code))
329341
}
330342

331343
#[tokio::main]

0 commit comments

Comments
 (0)