Align book errors with the published HTTP examples and record depth-order discrepancy

This commit is contained in:
2026-12-03 07:54:26 +08:00
parent 5dde2cd28e
commit 2ef0784ddc
2 changed files with 21 additions and 6 deletions
+19 -6
View File
@@ -28,14 +28,21 @@ type Reply = std::result::Result<Value, (u16, &'static str)>;
fn query(engine: &Mutex<Exchange>, params: Vec<BookParam>, single: bool) -> Reply {
if params.len() > MAX_BOOKS || params.iter().any(|p| p.token_id.trim().is_empty()) {
return Err((400, "Invalid book request"));
return Err((
400,
if single {
"Invalid token id"
} else {
"Invalid payload"
},
));
}
let tokens = params.into_iter().map(|p| p.token_id).collect::<Vec<_>>();
let mut books = engine
.lock()
.map_err(|_| (500, "Order book unavailable"))?
.map_err(|_| (500, "error getting the orderbook"))?
.public_books(&tokens)
.map_err(|_| (500, "Order book unavailable"))?;
.map_err(|_| (500, "error getting the orderbook"))?;
if single {
books.pop().ok_or((404, MISSING))
} else {
@@ -51,11 +58,11 @@ fn batch(req: &mut Request, engine: &Mutex<Exchange>) -> Reply {
req.as_reader()
.take(MAX_BODY + 1)
.read_to_end(&mut data)
.map_err(|_| (400, "Invalid request body"))?;
.map_err(|_| (400, "Invalid payload"))?;
if data.len() as u64 > MAX_BODY {
return Err((413, "Request body too large"));
}
let params = serde_json::from_slice(&data).map_err(|_| (400, "Invalid book request"))?;
let params = serde_json::from_slice(&data).map_err(|_| (400, "Invalid payload"))?;
query(engine, params, false)
}
@@ -161,7 +168,13 @@ mod tests {
("POST", "/books", "[]".into(), 200, Some(json!([]))),
("POST", "/books", json!(max_batch).to_string(), 200, None),
("POST", "/books", json!(too_many).to_string(), 400, None),
("POST", "/books", "{}".into(), 400, None),
(
"POST",
"/books",
"{}".into(),
400,
Some(json!({"error":"Invalid payload"})),
),
(
"POST",
"/books",