Skip to content

Commit

Permalink
fix: use ValidationErrorKind metric_label in ApiError (#1038)
Browse files Browse the repository at this point in the history
* fix: use ValidationErrorKind metric_label in ApiError
* removes extra metric call
* fixes up label to match existing metrics

Co-authored-by: Philip Jenvey <[email protected]>
  • Loading branch information
jrconlin and pjenvey authored Apr 15, 2021
1 parent 71c62be commit 4dc77af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl ApiErrorKind {
match self {
ApiErrorKind::Db(err) => err.metric_label(),
ApiErrorKind::Hawk(err) => err.metric_label(),
ApiErrorKind::Validation(err) => err.metric_label(),
_ => None,
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/web/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ impl ValidationError {
pub fn kind(&self) -> &ValidationErrorKind {
self.inner.get_context()
}

pub fn metric_label(&self) -> Option<String> {
match self.kind() {
ValidationErrorKind::FromDetails(
_description,
ref _location,
Some(ref _name),
metric_label,
) => metric_label.clone(),
ValidationErrorKind::FromValidationErrors(_errors, _location, metric_label) => {
metric_label.clone()
}
_ => None,
}
}
}

/// Causes of extractor errors.
Expand Down
10 changes: 4 additions & 6 deletions src/web/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ impl FromRequest for BsoBodies {
/// No collection id is used, so payload checks are not done here.
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
// Only try and parse the body if its a valid content-type
let metrics = metrics::Metrics::from(req);
let ctype = match ContentType::parse(req) {
Ok(v) => v,
Err(e) => {
Expand All @@ -167,7 +166,7 @@ impl FromRequest for BsoBodies {
format!("Unreadable Content-Type: {:?}", e),
RequestErrorLocation::Header,
Some("Content-Type".to_owned()),
label!("request.validate.bad_content_type"),
label!("request.error.invalid_content_type"),
)
.into(),
))
Expand All @@ -177,13 +176,12 @@ impl FromRequest for BsoBodies {
trace!("BSO Body content_type: {:?}", &content_type);

if !ACCEPTED_CONTENT_TYPES.contains(&content_type.as_ref()) {
metrics.incr("request.error.invalid_content_type");
return Box::pin(future::err(
ValidationErrorKind::FromDetails(
format!("Invalid Content-Type {:?}", content_type),
RequestErrorLocation::Header,
Some("Content-Type".to_owned()),
label!("request.validate.bad_content_type"),
label!("request.error.invalid_content_type"),
)
.into(),
));
Expand Down Expand Up @@ -364,7 +362,7 @@ impl FromRequest for BsoBody {
format!("Unreadable Content-Type: {:?}", e),
RequestErrorLocation::Header,
Some("Content-Type".to_owned()),
label!("request.validate.bad_content_type"),
label!("request.error.invalid_content_type"),
)
.into(),
))
Expand All @@ -377,7 +375,7 @@ impl FromRequest for BsoBody {
"Invalid Content-Type".to_owned(),
RequestErrorLocation::Header,
Some("Content-Type".to_owned()),
label!("request.validate.bad_content_type"),
label!("request.error.invalid_content_type"),
)
.into(),
));
Expand Down

0 comments on commit 4dc77af

Please sign in to comment.