Swift 3.1:将自定义错误转换为NSError以访问其域属性时发生崩溃

时间:2021-07-11 00:18:11

My Swift app has a custom error system where MyError is just a simple class conforming to Error. Now the app seems to crash whenever a third-party library (PromiseKit) tries to convert this error to NSError and then to access its domain property. In my own code, doing this works as expected, so why does it crash in the library and what's the proper way of dealing with it?

我的Swift应用程序有一个自定义错误系统,其中MyError只是一个符合Error的简单类。现在,只要第三方库(PromiseKit)尝试将此错误转换为NSError然后访问其域属性,应用程序就会崩溃。在我自己的代码中,这样做会按预期工作,那么为什么它在库中崩溃以及处理它的正确方法是什么?

Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x1011d86d8 _hidden#19226_ (__hidden#19178_:1788)
1  libswiftCore.dylib             0x1011cda3c _hidden#19206_ (__hidden#19447_:4045)
2  libswiftCore.dylib             0x1011cda3c _hidden#19206_ (__hidden#19447_:4045)
3  libswiftCore.dylib             0x1011cdc90 swift_getTypeName (__hidden#19406_:1731)
4  AppName                        0x1001dafec specialized (_adHocPrint_unlocked<A, B where ...> (A, Mirror, inout B, isDebugPrint : Bool) -> ()).(printTypeName #1)<A, B where ...> (Any.Type) -> () (MyError.swift)
5  AppName                        0x1001db4f0 specialized specialized _adHocPrint_unlocked<A, B where ...> (A, Mirror, inout B, isDebugPrint : Bool) -> () (MyError.swift)
6  AppName                        0x1001dafb4 specialized _debugPrint_unlocked<A, B where ...> (A, inout B) -> () (MyError.swift)
7  AppName                        0x1001dac00 protocol witness for Error._domain.getter in conformance MyError (MyError.swift)
8  libswiftCore.dylib             0x10104fa14 swift_stdlib_getErrorDomainNSString (__hidden#18979_:140)
9  libswiftCore.dylib             0x1011f96d8 _hidden#21248_ (__hidden#21275_:440)
10 PromiseKit                     0x100dc7d4c Error.isCancelledError.getter (Error.swift:145)

1 个解决方案

#1


2  

While casting from Error to NSError it is trying to access the errorCode and errorDomain. Adding these extensions fixed my problem in the same case.

从Error转换为NSError时,它尝试访问errorCode和errorDomain。添加这些扩展在同一个案例中修复了我的问题。

extension CustomError: LocalizedError {
    public var errorDescription: String? {
        return "Some localized description"
    }
}

extension CustomError: CustomNSError {
    public static var errorDomain: String {
        return "Some Domain Name"
    }
    public var errorCode: Int {
        return 204 //Should be your custom error code.
    }
}

#1


2  

While casting from Error to NSError it is trying to access the errorCode and errorDomain. Adding these extensions fixed my problem in the same case.

从Error转换为NSError时,它尝试访问errorCode和errorDomain。添加这些扩展在同一个案例中修复了我的问题。

extension CustomError: LocalizedError {
    public var errorDescription: String? {
        return "Some localized description"
    }
}

extension CustomError: CustomNSError {
    public static var errorDomain: String {
        return "Some Domain Name"
    }
    public var errorCode: Int {
        return 204 //Should be your custom error code.
    }
}