diff --git a/src/rest.rs b/src/rest.rs index 4cde8d887..5a42fee0d 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -795,7 +795,14 @@ fn handle_request( let txs = query .chain() - .get_block_txs(&hash, start_index, CHAIN_TXS_PER_PAGE)? + .get_block_txs(&hash, start_index, CHAIN_TXS_PER_PAGE) + .map_err(|e| { + if e.description().contains("block not found") { + HttpError::not_found("Block not found".to_string()) + } else { + HttpError::from(e) + } + })? .into_iter() .map(|tx| (tx, blockid)) .collect(); diff --git a/tests/rest.rs b/tests/rest.rs index 002798941..8686a7327 100644 --- a/tests/rest.rs +++ b/tests/rest.rs @@ -342,6 +342,26 @@ fn test_rest_block() -> Result<()> { let res = get_plain(rest_addr, &format!("/block/{}/txid/1", blockhash))?; assert_eq!(res, txid.to_string()); + // Test GET /block/:hash/txid/:index + // Should fail with 400 code when block hash is invalid + let invalid_hash_resp = ureq::get(&format!("http://{}/block/{}/txs/1", rest_addr, "invalid_hash")) + .config() + .http_status_as_error(false) + .build() + .call()?; + assert_eq!(invalid_hash_resp.status(), 400); + assert_eq!(invalid_hash_resp.into_body().read_to_string()?, "Invalid hex string"); + + // Test GET /block/:hash/txid/:index + // Should fail with 404 code when block isn't found + let invalid_hash_resp = ureq::get(&format!("http://{}/block/{}/txs/0", rest_addr, "0000000000000000000000000000000000000000000000000000000000000000")) + .config() + .http_status_as_error(false) + .build() + .call()?; + assert_eq!(invalid_hash_resp.status(), 404); + assert_eq!(invalid_hash_resp.into_body().read_to_string()?, "Block not found"); + rest_handle.stop(); Ok(()) }