Skip to content

Error shapes with structure members don't implement Display for referenced structures #4080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
drganjoo opened this issue Apr 2, 2025 · 0 comments · May be fixed by #4081
Open

Error shapes with structure members don't implement Display for referenced structures #4080

drganjoo opened this issue Apr 2, 2025 · 0 comments · May be fixed by #4081

Comments

@drganjoo
Copy link
Contributor

drganjoo commented Apr 2, 2025

When a shape is annotated with @error, the code generator implements the std::error::Error and std::fmt::Display trait for that error structure. However, if this error structure contains a member field that references another structure, the code generator doesn't implement Display for the referenced structure, resulting in compilation errors.

For example, the following model generates uncompilable code:

@error("client")
structure ErrorInInput {
    message: ErrorMessage
}

structure ErrorMessage {
    @required
    statusCode: String
    @required
    errorMessage: String
    requestId: String
}

The root cause is that when a shape is annotated with @error, the code generator implements std::error::Error for that type:

impl std::error::Error for ErrorInInput {}

This implementation requires that the type also implements the Display trait. While the code generator correctly implements Display for ErrorInInput, it attempts to forward the display implementation to the message field. However, since ErrorMessage structure doesn't have a Display implementation, compilation fails.

Generated Code

pub struct ErrorInInput {
    #[allow(missing_docs)] // documentation missing in model
    pub message: ::std::option::Option<crate::model::ErrorMessage>,
}

impl ::std::fmt::Display for ErrorInInput {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        ::std::write!(f, "ErrorInInput")?;
        if let ::std::option::Option::Some(inner_2) = &self.message {
            {
                ::std::write!(f, ": {}", inner_2)?;
            }
        }
        Ok(())
    }
}
impl ::std::error::Error for ErrorInInput {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant