Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions editoast/editoast_derive/src/view_error/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub(super) struct Args {
#[darling(with = ErrorAttrs::parse)]
attrs: ErrorAttrs,

/// Changes the base name of the error label
/// Changes the base path of the error label
#[darling(default)]
name: Option<String>,
path: Option<syn::Path>,

/// Whether to include type data into the error `context` field
///
Expand Down Expand Up @@ -71,7 +71,7 @@ struct VariantArgs {
#[darling(default)]
context: bool,
#[darling(default)]
name: Option<String>,
path: Option<syn::Path>,
}

#[derive(Debug, FromField)]
Expand Down Expand Up @@ -121,11 +121,14 @@ impl Args {
ident,
data,
attrs: ErrorAttrs { thiserror },
name,
path,
context: context_on_type,
args: TypeArgs { status },
} = self;
let label = name.unwrap_or_else(|| normalize_label(&ident));
let label = path
.as_ref()
.map(normalize_path)
.unwrap_or_else(|| normalize_label(&ident));
let mut view_error_impl = ViewErrorImpl::new(ident);

match data {
Expand Down Expand Up @@ -166,9 +169,12 @@ impl Args {
attrs: ErrorAttrs { thiserror },
status,
context: context_on_variant,
name,
path,
} = variant;
let variant_label = name.unwrap_or_else(|| normalize_label(&variant_ident));
let variant_label = path
.as_ref()
.map(normalize_path)
.unwrap_or_else(|| normalize_label(&variant_ident));
let pattern = fields.pattern(Some(&variant_ident));
if let Some(ForwardedField { binding, ty }) = fields.forwarded_view_error() {
view_error_impl.forward_view_error(pattern, binding, ty);
Expand Down Expand Up @@ -208,6 +214,14 @@ impl Args {
}
}

fn normalize_path(path: &syn::Path) -> String {
path.segments
.iter()
.map(|segment| normalize_label(&segment.ident))
.collect::<Vec<_>>()
.join(":")
}

/// Converts to snake_case, stripping any `_errors` or `_error` suffix
fn normalize_label(ident: &syn::Ident) -> String {
let label = ident.unraw().to_string().to_snek_case();
Expand Down Expand Up @@ -413,3 +427,16 @@ fn reject_unsupported_from(attrs: &[syn::Attribute]) -> Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn path_normalization() {
assert_eq!(
normalize_path(&syn::parse_quote! { DocumentErrors::NotFoundError }),
"document:not_found"
);
}
}
4 changes: 2 additions & 2 deletions editoast/openapi.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion editoast/src/views/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(in crate::views) enum DatabaseError {

#[derive(Debug, thiserror::Error, ViewError)]
#[error("Document '{document_key}' not found")]
#[view_error(status = NOT_FOUND, context, name = "document::not_found")]
#[view_error(status = NOT_FOUND, context, path = document::not_found)]
pub(in crate::views) struct DocumentNotFound {
document_key: i64,
}
Expand Down
30 changes: 15 additions & 15 deletions editoast/src/views/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mod tests {
fn attributes() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("ohno")]
#[view_error(name = "custom", status = NOT_FOUND, context)]
#[view_error(path = custom, status = NOT_FOUND, context)]
struct Unit;

assert_eq!(
Expand All @@ -203,7 +203,7 @@ mod tests {
fn attributes_and_context() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("value: {0}")]
#[view_error(name = "custom", status = UNAUTHORIZED, context)]
#[view_error(path = custom, status = UNAUTHORIZED, context)]
struct Newtype(String);

assert_eq!(
Expand All @@ -229,7 +229,7 @@ mod tests {
fn forwarded_view_error() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("inner error: {detail}")]
#[view_error(name = "inner", status = IM_A_TEAPOT, context)]
#[view_error(path = inner, status = IM_A_TEAPOT, context)]
struct InnerError {
detail: String,
}
Expand Down Expand Up @@ -275,7 +275,7 @@ mod tests {
fn context() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("tuple: {0}, {1}")]
#[view_error(name = "custom", status = BAD_REQUEST, context)]
#[view_error(path = custom, status = BAD_REQUEST, context)]
struct Tuple(String, u32);

assert_eq!(
Expand Down Expand Up @@ -307,7 +307,7 @@ mod tests {
fn forwarded_view_error_with_additional_field() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("inner error: {detail}")]
#[view_error(name = "inner", status = IM_A_TEAPOT, context)]
#[view_error(path = inner, status = IM_A_TEAPOT, context)]
struct InnerError {
detail: String,
}
Expand Down Expand Up @@ -338,7 +338,7 @@ mod tests {
fn attributes_and_context() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("incident {incident_id}: {cause}; fix: {fix}")]
#[view_error(name = "custom", status = SERVICE_UNAVAILABLE, context)]
#[view_error(path = custom, status = SERVICE_UNAVAILABLE, context)]
struct Named {
cause: String,
fix: String,
Expand Down Expand Up @@ -382,7 +382,7 @@ mod tests {
fn forwarded_view_error_with_additional_field() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("inner error: {detail}")]
#[view_error(name = "inner", status = IM_A_TEAPOT, context)]
#[view_error(path = inner, status = IM_A_TEAPOT, context)]
struct InnerError {
detail: String,
}
Expand Down Expand Up @@ -419,10 +419,10 @@ mod tests {
#[test]
fn variants() {
#[derive(Debug, thiserror::Error, ViewError)]
#[view_error(name = "ColorErrors")]
#[view_error(path = ColorErrors)]
enum Color {
#[error("red")]
#[view_error(name = "RED_ERROR")]
#[view_error(path = RED_ERROR)]
Red,
#[error("green")]
Green,
Expand All @@ -432,15 +432,15 @@ mod tests {

assert_eq!(
EditoastError::from(Color::Red),
EditoastError::new("editoast:ColorErrors:RED_ERROR", 500, "red")
EditoastError::new("editoast:color:red", 500, "red")
);
assert_eq!(
EditoastError::from(Color::Green),
EditoastError::new("editoast:ColorErrors:green", 500, "green")
EditoastError::new("editoast:color:green", 500, "green")
);
assert_eq!(
EditoastError::from(Color::BlueError),
EditoastError::new("editoast:ColorErrors:blue", 500, "blue")
EditoastError::new("editoast:color:blue", 500, "blue")
);
}
}
Expand All @@ -453,10 +453,10 @@ mod tests {
#[test]
fn variants() {
#[derive(Debug, thiserror::Error, ViewError)]
#[view_error(name = "mixed", context)]
#[view_error(path = mixed, context)]
enum Mixed {
#[error("unit")]
#[view_error(name = "custom_unit")]
#[view_error(path = custom_unit)]
Unit,
#[error("newtype: {0}")]
#[view_error(status = BAD_REQUEST)]
Expand Down Expand Up @@ -578,7 +578,7 @@ mod tests {
fn forwarded_view_errors() {
#[derive(Debug, thiserror::Error, ViewError)]
#[error("inner error: {detail}")]
#[view_error(name = "inner", status = IM_A_TEAPOT, context)]
#[view_error(path = inner, status = IM_A_TEAPOT, context)]
struct InnerError {
detail: String,
}
Expand Down
Loading