mirror of https://github.com/smithy-lang/smithy-rs
Only generate `@error` `message` getter when defined in the model (#1441)
We are currently generating a useless `message` getter that returns `None` even when the `message` field is not defined in the model. Granted, in the client this field is _almost always_ there, because the `AddErrorMessage` model transformer is applied unless the user opts out in their `smithy-build.json`.
This commit is contained in:
parent
1875448dbb
commit
2d5bf6fc2c
|
@ -69,13 +69,6 @@ class ErrorGenerator(
|
|||
val symbol = symbolProvider.toSymbol(shape)
|
||||
val messageShape = shape.errorMessageMember()
|
||||
val errorKindT = RuntimeType.errorKind(symbolProvider.config().runtimeConfig)
|
||||
val (returnType, message) = messageShape?.let {
|
||||
if (symbolProvider.toSymbol(messageShape).isOptional()) {
|
||||
"Option<&str>" to "self.${symbolProvider.toMemberName(it)}.as_deref()"
|
||||
} else {
|
||||
"&str" to "self.${symbolProvider.toMemberName(it)}.as_ref()"
|
||||
}
|
||||
} ?: "Option<&str>" to "None"
|
||||
writer.rustBlock("impl ${symbol.name}") {
|
||||
val retryKindWriteable = shape.modeledRetryKind(error)?.writable(symbolProvider.config().runtimeConfig)
|
||||
if (retryKindWriteable != null) {
|
||||
|
@ -84,12 +77,20 @@ class ErrorGenerator(
|
|||
retryKindWriteable(this)
|
||||
}
|
||||
}
|
||||
rust(
|
||||
"""
|
||||
/// Returns the error message.
|
||||
pub fn message(&self) -> $returnType { $message }
|
||||
"""
|
||||
)
|
||||
if (messageShape != null) {
|
||||
val (returnType, message) = if (symbolProvider.toSymbol(messageShape).isOptional()) {
|
||||
"Option<&str>" to "self.${symbolProvider.toMemberName(messageShape)}.as_deref()"
|
||||
} else {
|
||||
"&str" to "self.${symbolProvider.toMemberName(messageShape)}.as_ref()"
|
||||
}
|
||||
|
||||
rust(
|
||||
"""
|
||||
/// Returns the error message.
|
||||
pub fn message(&self) -> $returnType { $message }
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
/*
|
||||
* If we're generating for a server, the `name` method is added to enable
|
||||
|
|
|
@ -18,11 +18,11 @@ import java.util.logging.Logger
|
|||
fun StructureShape.errorMessageMember(): MemberShape? = this.getMember("message").or { this.getMember("Message") }.orNull()
|
||||
|
||||
/**
|
||||
* Ensure that all errors have error messages
|
||||
* Ensure that all errors have error messages.
|
||||
*
|
||||
* Not all errors are modeled with an error message field. However, in many cases, the server can still send an error.
|
||||
* If an error, specifically, a structure shape with the error trait does not have a member `message` or `Message`,
|
||||
* this transformer will add a `message` member targetting a string.
|
||||
* this transformer will add a `message` member targeting a string.
|
||||
*
|
||||
* This ensures that we always generate a modeled error message field enabling end users to easily extract the error
|
||||
* message when present.
|
||||
|
@ -33,7 +33,7 @@ object AddErrorMessage {
|
|||
private val logger = Logger.getLogger("AddErrorMessage")
|
||||
|
||||
/**
|
||||
* Ensure that all errors have error messages
|
||||
* Ensure that all errors have error messages.
|
||||
*/
|
||||
fun transform(model: Model): Model {
|
||||
return ModelTransformer.create().mapShapes(model) { shape ->
|
||||
|
|
Loading…
Reference in New Issue