Skip to content

Commit ab134cf

Browse files
authored
update to thiserror 2 (#1312)
Signed-off-by: Craig Disselkoen <[email protected]>
1 parent 6d3ec20 commit ab134cf

File tree

9 files changed

+73
-30
lines changed

9 files changed

+73
-30
lines changed

Cargo.lock

+30-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cedar-policy-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ clap = { version = "4", features = ["derive", "env"] }
1717
serde = { version = "1.0", features = ["derive"] }
1818
serde_json = "1.0"
1919
miette = { version = "7.1.0", features = ["fancy"] }
20-
thiserror = "1.0"
20+
thiserror = "2.0"
2121
semver = "1.0.23"
2222

2323
[features]

cedar-policy-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ either = "1.8"
2222
itertools = "0.13"
2323
ref-cast = "1.0"
2424
rustc_lexer = "0.1"
25-
thiserror = "1.0"
25+
thiserror = "2.0"
2626
smol_str = { version = "0.3", features = ["serde"] }
2727
stacker = "0.1.15"
2828
arbitrary = { version = "1", features = ["derive"], optional = true }

cedar-policy-core/src/ast/policy.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl std::fmt::Display for Template {
303303
pub enum LinkingError {
304304
/// An error with the slot arguments provided
305305
// INVARIANT: `unbound_values` and `extra_values` can't both be empty
306-
#[error("{}", describe_arity_error(.unbound_values, .extra_values))]
306+
#[error(fmt = describe_arity_error)]
307307
ArityError {
308308
/// Error for when some Slots were not provided values
309309
unbound_values: Vec<SlotId>,
@@ -338,14 +338,18 @@ impl LinkingError {
338338
}
339339
}
340340

341-
fn describe_arity_error(unbound_values: &[SlotId], extra_values: &[SlotId]) -> String {
341+
fn describe_arity_error(
342+
unbound_values: &[SlotId],
343+
extra_values: &[SlotId],
344+
fmt: &mut std::fmt::Formatter<'_>,
345+
) -> std::fmt::Result {
342346
match (unbound_values.len(), extra_values.len()) {
343347
// PANIC SAFETY 0,0 case is not an error
344348
#[allow(clippy::unreachable)]
345349
(0,0) => unreachable!(),
346-
(_unbound, 0) => format!("the following slots were not provided as arguments: {}", unbound_values.iter().join(",")),
347-
(0, _extra) => format!("the following slots were provided as arguments, but did not exist in the template: {}", extra_values.iter().join(",")),
348-
(_unbound, _extra) => format!("the following slots were not provided as arguments: {}. The following slots were provided as arguments, but did not exist in the template: {}", unbound_values.iter().join(","), extra_values.iter().join(","))
350+
(_unbound, 0) => write!(fmt, "the following slots were not provided as arguments: {}", unbound_values.iter().join(",")),
351+
(0, _extra) => write!(fmt, "the following slots were provided as arguments, but did not exist in the template: {}", extra_values.iter().join(",")),
352+
(_unbound, _extra) => write!(fmt, "the following slots were not provided as arguments: {}. The following slots were provided as arguments, but did not exist in the template: {}", unbound_values.iter().join(","), extra_values.iter().join(",")),
349353
}
350354
}
351355

cedar-policy-core/src/est/expr.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,10 @@ impl TryFrom<&Node<Option<cst::Member>>> for Expr {
14361436
item = match item {
14371437
Either::Left(name) => {
14381438
return Err(node
1439-
.to_ast_err(ToASTErrorKind::InvalidAccess(name, id.to_smolstr()))
1439+
.to_ast_err(ToASTErrorKind::InvalidAccess {
1440+
lhs: name,
1441+
field: id.to_smolstr(),
1442+
})
14401443
.into())
14411444
}
14421445
Either::Right(expr) => Either::Right(Expr::get_attr(expr, id.to_smolstr())),
@@ -1505,7 +1508,10 @@ impl TryFrom<&Node<Option<cst::Member>>> for Expr {
15051508
item = match item {
15061509
Either::Left(name) => {
15071510
return Err(node
1508-
.to_ast_err(ToASTErrorKind::InvalidIndex(name, s))
1511+
.to_ast_err(ToASTErrorKind::InvalidIndex {
1512+
lhs: name,
1513+
field: s,
1514+
})
15091515
.into())
15101516
}
15111517
Either::Right(expr) => Either::Right(Expr::get_attr(expr, s)),

cedar-policy-core/src/parser/cst_to_ast.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1274,16 +1274,19 @@ impl Node<Option<cst::Member>> {
12741274
// attribute access on an arbitrary name - error
12751275
(Name { name, .. }, [Field(f), ..]) => {
12761276
return Err(self
1277-
.to_ast_err(ToASTErrorKind::InvalidAccess(
1278-
name.clone(),
1279-
f.to_string().into(),
1280-
))
1277+
.to_ast_err(ToASTErrorKind::InvalidAccess {
1278+
lhs: name.clone(),
1279+
field: f.to_smolstr(),
1280+
})
12811281
.into());
12821282
}
12831283
// index style attribute access on an arbitrary name - error
12841284
(Name { name, .. }, [Index(i), ..]) => {
12851285
return Err(self
1286-
.to_ast_err(ToASTErrorKind::InvalidIndex(name.clone(), i.clone()))
1286+
.to_ast_err(ToASTErrorKind::InvalidIndex {
1287+
lhs: name.clone(),
1288+
field: i.clone(),
1289+
})
12871290
.into());
12881291
}
12891292

cedar-policy-core/src/parser/err.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,21 @@ pub enum ToASTErrorKind {
323323
#[error("function calls must be of the form `<name>(arg1, arg2, ...)`")]
324324
ExpressionCall,
325325
/// Returned when a policy attempts to access the fields of a value with no fields
326-
#[error("invalid member access `{0}.{1}`, `{0}` has no fields or methods")]
327-
InvalidAccess(ast::Name, SmolStr),
326+
#[error("invalid member access `{lhs}.{field}`, `{lhs}` has no fields or methods")]
327+
InvalidAccess {
328+
/// what we attempted to access a field of
329+
lhs: ast::Name,
330+
/// field we attempted to access
331+
field: SmolStr,
332+
},
328333
/// Returned when a policy attempts to index on a fields of a value with no fields
329-
#[error("invalid indexing expression `{0}[\"{}\"]`, `{0}` has no fields", .1.escape_debug())]
330-
InvalidIndex(ast::Name, SmolStr),
334+
#[error("invalid indexing expression `{lhs}[\"{}\"]`, `{lhs}` has no fields", .field.escape_debug())]
335+
InvalidIndex {
336+
/// what we attempted to access a field of
337+
lhs: ast::Name,
338+
/// field we attempted to access
339+
field: SmolStr,
340+
},
331341
/// Returned when the contents of an indexing expression is not a string literal
332342
#[error("the contents of an index expression must be a string literal")]
333343
NonStringIndex,

cedar-policy-validator/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ serde = { version = "1.0", features = ["derive"] }
1616
serde_json = { version = "1.0", features = ["preserve_order"] }
1717
serde_with = "3.0"
1818
miette = "7.1.0"
19-
thiserror = "1.0"
19+
thiserror = "2.0"
2020
itertools = "0.13"
2121
ref-cast = "1.0"
2222
unicode-security = "0.1.0"

cedar-policy/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ serde_json = "1.0"
2020
lalrpop-util = { version = "0.22.0", features = ["lexer"] }
2121
itertools = "0.13"
2222
miette = "7.1.0"
23-
thiserror = "1.0"
23+
thiserror = "2.0"
2424
smol_str = { version = "0.3", features = ["serde"] }
2525
dhat = { version = "0.3.2", optional = true }
2626
serde_with = "3.3.0"

0 commit comments

Comments
 (0)