Swift 3错误以及其他数据 [英] Swift 3 errors with additional data

查看:78
本文介绍了Swift 3错误以及其他数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:

  1. 定义表示错误类型的外部enum错误,并针对每种情况接受一个参数,该参数是包含其他异常详细信息的对象,或者
  2. 将该对象用作实际的Error,并在从enum传递给其构造函数的情况下表示实际的错误情况.
  1. Define an outer enum error that represents the type of error, and for each case accept a parameter that is an object that contains the additional exception details, or
  2. Use the object as the actual Error and pass in a case from an enum to its constructor to represent the actual error condition.

这两种方法对我来说都是不干净的,因为它们采用两个独立的概念来表示一个简单的想法,即错误,我只是想知道是否有更好的方法可以做到这一点.

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协议来 提供合理的错误描述,即使 捕获程序不知道具体的错误类型(比较

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错误以及其他数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆