在Swift 3中,推荐的方法是将(可能很多)附加信息放入错误/异常中,以使捕获器可以用来解决/处理问题?在我所看到的所有示例中,它们都使用enum和关联的值,对于很多信息来说似乎过于繁琐/冗长.
In Swift 3, what is the recommended way to put (potentially lots of) additional information in an error/exception that the catcher can use to solve/handle the problem? In all the examples I've seen, they use enums with associated values, and that seems overly cumbersome/verbose for lots of information.
特别是,我正在编写一个简单的解析器,并希望有一个位置来存储受影响的行号和列号(以及将来可能的其他信息),但是不要求每个处理程序显式地将其声明为关联值,那样就可以了.呼叫者的负担.
Specifically, I am writing a simple parser and want a place to store the affected line and column numbers (and potentially other information in the future), but without requiring that every handler explicitly declare those as associated values, as that would be a burden on the caller.
在这一点上,我基本上可以看到两种方法,这两种方法似乎都不是特别优雅,并且两种方法都需要定义两个不同的东西:
At this point I can basically see two ways of doing this, neither of which seems particularly elegant and both of which require defining two different things:
这两种方法对我来说都是不干净的,因为它们采用两个独立的概念来表示一个简单的想法,即错误,我只是想知道是否有更好的方法可以做到这一点.
Both of these feel somewhat unclean to me though as they take two separate concepts to represent a simple idea, an error, and I'm just wondering if there's a nicer way to do this.
是否存在处理可能包含大量附加信息的错误的约定或推荐方法?
Are there any conventions or recommended ways to handle errors that need to contain potentially lots of additional information?
推荐答案我不知道是否存在推荐"方式,也许其他人可以 回答这个问题或提供更好的解决方案.
I don't know if there is a "recommended" way, perhaps someone else can answer that or provide a better solution.
但是一种可能方法是使用struct(带有属性)作为错误类型,并对需要的值使用可选属性 没有提供.示例:
But one possible approach would be to use a struct (with properties) as the error type and use optional properties for values which need not be provided. Example:
struct ParserError: Error { enum Reason { case invalidCharacter case unexpectedEOF } let reason: Reason let line: Int? let column: Int? init(reason: Reason, line: Int? = nil, column: Int? = nil) { self.reason = reason self.line = line self.column = column } }可能还想采用LocalizedError协议来 提供合理的错误描述,即使 捕获程序不知道具体的错误类型(比较如何在Swift中提供带有错误类型的本地化描述?):
One might also want to adopt the LocalizedError protocol to provide sensible error descriptions even if the concrete error type is not known by the catcher (compare How to provide a localized description with an Error type in Swift?):
extension ParserError: LocalizedError { public var errorDescription: String? { var description: String switch reason { case .invalidCharacter: description = "Invalid Character in input file" case .unexpectedEOF: description = "Unexpected end of file" } if let line = line { description += ", line \(line)" } if let column = column { description += ", column \(column)" } return description } }用法示例:
func parse() throws { // Throw error with line number, but without column: throw ParserError(reason: .invalidCharacter, line: 13) } do { try parse() } catch let error { print(error.localizedDescription) }输出:
Invalid Character in input file, line 13更多推荐
Swift 3错误以及其他数据
发布评论