Lucid 0.5.0

Lucid 0.5.0

Levi Bostian 维护。



Lucid 0.5.0

Lucid

使 Moya 错误更容易读懂。向您的应用程序用户展示他们可以理解的错误消息。

Swift 4.0.x

Version License Platform

为什么?

当使用 Moya 时,如果您的应用程序遇到错误,例如没有网络连接,Moya 会给出一个 error.localizedDescription,例如 "状态码不在范围内"。我不想向用户展示这个错误消息。我更愿意告诉他们,“您没有网络连接。请连接后重试。”这就是 Lucid 诞生的原因。

如何?

  • 创建一个继承自 LucidErrorMessageProvider 协议的类。这个类将在发生错误时被通知,并返回根据发生的错误而返回的错误消息。
class MyLucidErrorMessageProvider: LucidErrorMessageProvider {
    ...
}
  • 将您创建的这个新类设置为所有 Moya 端点的默认错误处理器
LucidConfiguration.setDefaultErrorHandler(MyLucidErrorMessageProvider())
  • 在您的 Moya 错误处理器中,从原始错误中获取 Lucid 错误
provider = MoyaProvider<GitHub>()
provider.request(.zen) { result in
    switch result {
    case let .success(moyaResponse):
        do {
            try! moyaResponse.filterSuccessfulStatusCodes()

            let data = moyaResponse.data
            let statusCode = moyaResponse.statusCode
            // do something with the response data or statusCode
        } catch (error: LucidMoyaError) {
            let humanReadableError = error.localizedDescription
        }
    case let .failure(error):
        let humanReadableError = error.getLucidError()

        // Feel confident showing `humanReadableError.localizedDescription` to your app users as the error message will actually be helpful to them. 

        // this means there was a network failure - either the request
        // wasn't sent (connectivity), or no response was received (server
        // timed out).  If the server responds with a 4xx or 5xx error, that
        // will be sent as a ".success"-ful response.
    }
}

您可以在从 Moya 返回的任何错误上使用扩展 .getLucidError(),或者您可以使用 Lucid 提供的 .filter(invalidStatusCodes: ) 函数组对 Moya 响应进行筛选以获取 LucidMoyaError。一旦您有一个 LucidMoyaError,您可以确信您的 .localizedDescription 是您提供的可读性人类错误,并可以显示给用户。


RxSwift

Lucid提供了RxSwift功能,使得Lucid更容易使用。

provider = MoyaProvider<GitHub>()
provider.rx
  .request(.userProfile("ashfurrow"))
  .filterSuccessfulStatusCodes() // Lucid, as well as Moya, provide functions to filter out status codes that are 'invalid' for your API. 
  .processErrors() // Will run `.getLucidError()` on an error if it is thrown in the stream. If you choose not to use this line, you can manually call `.getLucidError()` on the error returned from this function. 
  .subscribe { event in
    switch event {
    case let .success(response):
        image = UIImage(data: response.data)
    case let .error(error):
        let humanReadableError = error.localizedDescription
        // Because we used `.processErrors()` above, we can feel confident that our error message is human readable and will be helpful to our user. 
    }
}

安装

您可以通过CocoaPods安装Lucid,只需在Podfile中添加以下行:

注意:我建议将cocoapod的版本附加到Podfile行条目,如下所示:pod "Lucid", '~> 0.4.0',因为在这个时刻,该库不符合向后兼容的保证。在使用时请注意,API可能会随时更改。我不希望您的代码在您下次调用pod update时中断:)。

当前最新版本是:Version

pod "Lucid"

// Or, use RxSwift version
pod "Lucid/RxSwift"

作者

Levi Bostian image

许可证

Lucid在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。

文档

请查看此处文档.

开发

文档

文档由 Cocoapods 自动生成和托管在 cocoadocs 上。

文档通过 jazzy 命令生成:jazzy --podspec Lucid.podspec(假设已安装 jazzy。如果没有:使用 gem install jazzy 安装)

致谢