Skip to content

Commit dd45b4e

Browse files
authored
Merge pull request #547 from input-output-hk/djo/better-error-log-message-4-http-server
Better error logs for /register-signer bad request
2 parents 2116739 + 27f51f6 commit dd45b4e

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

mithril-aggregator/src/http_server/routes/reply.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mithril_common::entities::InternalServerError;
1+
use mithril_common::entities::{ClientError, InternalServerError};
22
use serde::Serialize;
33
use warp::http::StatusCode;
44

@@ -16,6 +16,10 @@ pub fn empty(status_code: StatusCode) -> Box<dyn warp::Reply> {
1616
Box::new(warp::reply::with_status(warp::reply::reply(), status_code))
1717
}
1818

19+
pub fn bad_request(label: String, message: String) -> Box<dyn warp::Reply> {
20+
json(&ClientError::new(label, message), StatusCode::BAD_REQUEST)
21+
}
22+
1923
pub fn internal_server_error(message: String) -> Box<dyn warp::Reply> {
2024
json(
2125
&InternalServerError::new(message),

mithril-aggregator/src/http_server/routes/signer_routes.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ mod handlers {
4545
}
4646
Err(ProtocolError::Codec(err)) => {
4747
warn!("register_signer::failed_signer_decoding"; "error" => ?err);
48-
Ok(reply::empty(StatusCode::BAD_REQUEST))
48+
Ok(reply::bad_request(
49+
"failed_signer_decoding".to_string(),
50+
err,
51+
))
4952
}
5053
Err(ProtocolError::FailedSignerRegistration(err)) => {
5154
warn!("register_signer::failed_signer_registration"; "error" => ?err);
52-
Ok(reply::empty(StatusCode::BAD_REQUEST))
55+
Ok(reply::bad_request(
56+
"failed_signer_registration".to_string(),
57+
err.to_string(),
58+
))
5359
}
5460
Err(err) => {
5561
warn!("register_signer::error"; "error" => ?err);

mithril-common/src/apispec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a> APISpec<'a> {
118118
match schema {
119119
Null => match value {
120120
Null => Ok(self),
121-
_ => Err("null schema provided".to_string()),
121+
_ => Err(format!("Expected nothing but got: {:?}", value)),
122122
},
123123
_ => {
124124
let schema = &mut schema.as_object_mut().unwrap().clone();

mithril-common/src/entities/internal_server_error.rs renamed to mithril-common/src/entities/http_server_error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,20 @@ impl InternalServerError {
1313
InternalServerError { message }
1414
}
1515
}
16+
17+
/// Representation of a Client Error raised by an http server
18+
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
19+
pub struct ClientError {
20+
/// error label
21+
pub label: String,
22+
23+
/// error message
24+
pub message: String,
25+
}
26+
27+
impl ClientError {
28+
/// ClientError factory
29+
pub fn new(label: String, message: String) -> ClientError {
30+
ClientError { label, message }
31+
}
32+
}

mithril-common/src/entities/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod certificate_metadata;
77
mod certificate_pending;
88
mod epoch;
99
mod epoch_settings;
10-
mod internal_server_error;
10+
mod http_server_error;
1111
mod protocol_message;
1212
mod protocol_parameters;
1313
mod signer;
@@ -22,7 +22,7 @@ pub use certificate_metadata::CertificateMetadata;
2222
pub use certificate_pending::CertificatePending;
2323
pub use epoch::{Epoch, EpochError};
2424
pub use epoch_settings::EpochSettings;
25-
pub use internal_server_error::InternalServerError;
25+
pub use http_server_error::{ClientError, InternalServerError};
2626
pub use protocol_message::{ProtocolMessage, ProtocolMessagePartKey, ProtocolMessagePartValue};
2727
pub use protocol_parameters::ProtocolParameters;
2828
pub use signer::{Signer, SignerWithStake};

mithril-signer/src/certificate_handler.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl CertificateHandler for CertificateHandlerHTTPClient {
132132
Ok(response) => match response.status() {
133133
StatusCode::CREATED => Ok(()),
134134
StatusCode::BAD_REQUEST => Err(CertificateHandlerError::RemoteServerLogical(
135-
"bad request".to_string(),
135+
format!("bad request: {}", response.text().await.unwrap_or_default()),
136136
)),
137137
_ => Err(CertificateHandlerError::RemoteServerTechnical(
138138
response.text().await.unwrap_or_default(),
@@ -156,7 +156,7 @@ impl CertificateHandler for CertificateHandlerHTTPClient {
156156
Ok(response) => match response.status() {
157157
StatusCode::CREATED => Ok(()),
158158
StatusCode::BAD_REQUEST => Err(CertificateHandlerError::RemoteServerLogical(
159-
"bad request".to_string(),
159+
format!("bad request: {}", response.text().await.unwrap_or_default()),
160160
)),
161161
StatusCode::CONFLICT => Err(CertificateHandlerError::RemoteServerLogical(
162162
"already registered single signatures".to_string(),
@@ -262,6 +262,7 @@ impl CertificateHandler for DumbCertificateHandler {
262262
mod tests {
263263
use super::*;
264264
use httpmock::prelude::*;
265+
use mithril_common::entities::ClientError;
265266
use serde_json::json;
266267
use std::path::{Path, PathBuf};
267268

@@ -386,12 +387,21 @@ mod tests {
386387
let (server, config) = setup_test();
387388
let _snapshots_mock = server.mock(|when, then| {
388389
when.method(POST).path("/register-signer");
389-
then.status(400);
390+
then.status(400).body(
391+
serde_json::to_vec(&ClientError::new(
392+
"error".to_string(),
393+
"an error".to_string(),
394+
))
395+
.unwrap(),
396+
);
390397
});
391398
let certificate_handler = CertificateHandlerHTTPClient::new(config.aggregator_endpoint);
392399
let register_signer = certificate_handler.register_signer(single_signer).await;
393400
assert_eq!(
394-
CertificateHandlerError::RemoteServerLogical("bad request".to_string()).to_string(),
401+
CertificateHandlerError::RemoteServerLogical(
402+
"bad request: {\"label\":\"error\",\"message\":\"an error\"}".to_string()
403+
)
404+
.to_string(),
395405
register_signer.unwrap_err().to_string()
396406
);
397407
}
@@ -435,14 +445,23 @@ mod tests {
435445
let (server, config) = setup_test();
436446
let _snapshots_mock = server.mock(|when, then| {
437447
when.method(POST).path("/register-signatures");
438-
then.status(400);
448+
then.status(400).body(
449+
serde_json::to_vec(&ClientError::new(
450+
"error".to_string(),
451+
"an error".to_string(),
452+
))
453+
.unwrap(),
454+
);
439455
});
440456
let certificate_handler = CertificateHandlerHTTPClient::new(config.aggregator_endpoint);
441457
let register_signatures = certificate_handler
442458
.register_signatures(&single_signatures)
443459
.await;
444460
assert_eq!(
445-
CertificateHandlerError::RemoteServerLogical("bad request".to_string()).to_string(),
461+
CertificateHandlerError::RemoteServerLogical(
462+
"bad request: {\"label\":\"error\",\"message\":\"an error\"}".to_string()
463+
)
464+
.to_string(),
446465
register_signatures.unwrap_err().to_string()
447466
);
448467
}

openapi.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ paths:
184184
description: signer registration succeeded
185185
"400":
186186
description: signer registration bad request
187+
content:
188+
application/json:
189+
schema:
190+
$ref: "#/components/schemas/Error"
187191
default:
188192
description: signer registration error
189193
content:
@@ -207,6 +211,10 @@ paths:
207211
description: signatures registration succeeded
208212
"400":
209213
description: signer registration bad request
214+
content:
215+
application/json:
216+
schema:
217+
$ref: "#/components/schemas/Error"
210218
"409":
211219
description: signatures registration already done
212220
default:
@@ -636,6 +644,9 @@ components:
636644
required:
637645
- message
638646
properties:
647+
label:
648+
description: optional label
649+
type: string
639650
message:
640651
description: error message
641652
type: string

0 commit comments

Comments
 (0)