Skip to content

Commit

Permalink
Merge pull request #44 from amzn/conform_identifiable
Browse files Browse the repository at this point in the history
Conform error types to Identifiable
  • Loading branch information
tachyonics authored Aug 15, 2023
2 parents 12411f4 + c855135 commit dc3f740
Showing 1 changed file with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SmokeFrameworkModelErrorsDelegate: ModelErrorsDelegate {
let optionSetGeneration: ErrorOptionSetGeneration =
.generateWithCustomConformance(libraryImport: "SmokeOperations", conformanceType: "ErrorIdentifiableByDescription")
let generateEncodableConformance: Bool = true
let generateCustomStringConvertibleConformance: Bool = true
let generateCustomStringConvertibleConformance: Bool = false
let canExpectValidationError: Bool = true

func errorTypeAdditionalImportsGenerator(fileBuilder: FileBuilder,
Expand Down Expand Up @@ -102,6 +102,31 @@ struct SmokeFrameworkModelErrorsDelegate: ModelErrorsDelegate {

func errorTypeAdditionalDescriptionCases(fileBuilder: FileBuilder,
errorTypes: [ErrorType]) {
// nothing to do
}

func errorTypeAdditionalExtensions(fileBuilder: FileBuilder,
errorTypes: [ErrorType],
baseName: String) {
// create an option set to specify the possible errors from an operation

fileBuilder.appendLine("""
extension \(baseName)Error: Identifiable {
public var identity: String {
switch self {
""")

fileBuilder.incIndent()
fileBuilder.incIndent()
// for each of the errors
for error in errorTypes {
let internalName = error.normalizedName
fileBuilder.appendLine("""
case .\(internalName):
return \(internalName)Identity
""")
}

fileBuilder.appendLine("""
case .validationError:
return __validationErrorIdentity
Expand All @@ -110,5 +135,48 @@ struct SmokeFrameworkModelErrorsDelegate: ModelErrorsDelegate {
case .unknownError:
return __unknownErrorIdentity
""")

fileBuilder.decIndent()
fileBuilder.decIndent()

fileBuilder.appendLine("""
}
}
}
extension \(baseName)Error: CustomStringConvertible {
public var description: String {
switch self {
""")

fileBuilder.incIndent()
fileBuilder.incIndent()
// for each of the errors
for error in errorTypes {
let internalName = error.normalizedName
fileBuilder.appendLine("""
case .\(internalName)(let details):
return "\(baseName)Error.\\(details)"
""")
}

fileBuilder.appendLine("""
case .validationError(reason: let reason):
return "\(baseName)Error.\\(__validationErrorIdentity): \\(reason)"
case .unrecognizedError(let errorReason, let errorMessage):
return "\(baseName)Error.\\(errorReason) (\\(__unrecognizedErrorIdentity)): \\(errorMessage)"
case .unknownError:
return "\(baseName)Error.\\(__unknownErrorIdentity)"
""")

fileBuilder.decIndent()
fileBuilder.decIndent()

fileBuilder.appendLine("""
}
}
}
""")
fileBuilder.appendEmptyLine()
}
}

0 comments on commit dc3f740

Please sign in to comment.