我的应用程序中有示例错误枚举:
public enum APIError: Error {
case IncorrectArguments(message: String)
case MissingArgument(message: String)
}
错误处理看起来像:
do {
return try next.respond(to: request)
} catch let error as APIError {
throw Abort(.badRequest, reason: error.message)
}
但我在这里得到了编译错误,说:
Value of type 'APIError' has no member 'message'
.
我知道我可以通过模式匹配获得参数,但我不想以完全相同的方式处理每种情况:
do {
return try next.respond(to: request)
} catch APIError.MissingArgument(let message) {
throw Abort(.badRequest, reason: message)
} catch APIError.IncorrectArguments(let message) {
throw Abort(.badRequest, reason: message)
}
将来枚举中可能会有更多的apiErrors,我不想使用单独的catch块来处理每个错误,因为每个人都将是完全相同的。
有没有办法用一般的方法来处理这些错误?