Skip to content

Commit d26fcaf

Browse files
committed
Use change_context
1 parent e0ef80b commit d26fcaf

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

crates/common/src/cookies.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! used in the trusted server system.
55
66
use cookie::{Cookie, CookieJar};
7-
use error_stack::Report;
7+
use error_stack::{Report, ResultExt};
88
use fastly::http::header;
99
use fastly::Request;
1010

@@ -42,12 +42,12 @@ pub fn handle_request_cookies(
4242
) -> Result<Option<CookieJar>, Report<TrustedServerError>> {
4343
match req.get_header(header::COOKIE) {
4444
Some(header_value) => {
45-
let header_value_str = header_value.to_str().map_err(|e| {
46-
Report::new(TrustedServerError::InvalidHeaderValue {
47-
message: "Cookie header contains invalid UTF-8".to_string(),
48-
})
49-
.attach_printable(e.to_string())
50-
})?;
45+
let header_value_str =
46+
header_value
47+
.to_str()
48+
.change_context(TrustedServerError::InvalidHeaderValue {
49+
message: "Cookie header contains invalid UTF-8".to_string(),
50+
})?;
5151
let jar = parse_cookies_to_jar(header_value_str);
5252
Ok(Some(jar))
5353
}

crates/common/src/synthetic.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module provides functionality for generating privacy-preserving synthetic IDs
44
//! based on various request parameters and a secret key.
55
6-
use error_stack::Report;
6+
use error_stack::{Report, ResultExt};
77
use fastly::http::header;
88
use fastly::Request;
99
use handlebars::Handlebars;
@@ -62,20 +62,15 @@ pub fn generate_synthetic_id(
6262

6363
let input_string = handlebars
6464
.render_template(&settings.synthetic.template, data)
65-
.map_err(|e| {
66-
Report::new(TrustedServerError::Template {
67-
message: "Failed to render synthetic ID template".to_string(),
68-
})
69-
.attach_printable(e.to_string())
65+
.change_context(TrustedServerError::Template {
66+
message: "Failed to render synthetic ID template".to_string(),
7067
})?;
68+
7169
log::info!("Input string for fresh ID: {} {}", input_string, data);
7270

73-
let mut mac =
74-
HmacSha256::new_from_slice(settings.synthetic.secret_key.as_bytes()).map_err(|e| {
75-
Report::new(TrustedServerError::SyntheticId {
76-
message: "Failed to create HMAC instance".to_string(),
77-
})
78-
.attach_printable(e.to_string())
71+
let mut mac = HmacSha256::new_from_slice(settings.synthetic.secret_key.as_bytes())
72+
.change_context(TrustedServerError::SyntheticId {
73+
message: "Failed to create HMAC instance".to_string(),
7974
})?;
8075
mac.update(input_string.as_bytes());
8176
let fresh_id = hex::encode(mac.finalize().into_bytes());

0 commit comments

Comments
 (0)