<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On May 14, 2016, at 1:45 AM, Jon Shier via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Charles:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span class="Apple-tab-span" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">        </span><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Foundation error reporting is based on NSError, nothing else. In none of my apps have I used my own NSError’s and in the few frameworks I use or maintain, few use NSError, and then only to avoid having to declare a custom error type, to make the API perhaps a bit more familiar to consumers.</span></div></blockquote><div><br class=""></div><div>All of the error-handling mechanisms in Cocoa are NSError-based. -[NSApplication presentError:], -[NSDocument presentError:], and the like all take NSErrors.</div><br class=""><blockquote type="cite" class=""><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">All your concern about conversion to and from NSErrors is largely irrelevant to anyone who isn’t using Swift from Objective-C on a regular basis. It’s utterly irrelevant to the native Swift programmer. </span>I’m not sure where your issue about presenting non-NSError’s come from, since the mechanisms to present an error would have to be custom written no matter the error type.</div></blockquote><div><br class=""></div><div>Anyone who writes Cocoa apps on OS X or iOS using Swift is most certainly using Swift from Objective-C on a regular basis, because all of the underlying frameworks are Objective-C. For example, suppose you have a document-based application:</div><div><br class=""></div><div>class MyDocument: NSDocument {</div><div> enum Error: ErrorType {</div><div> case MadeAMistake</div><div> case RanOutOfCake</div><div> }</div><div><br class=""></div><div> …</div><div><br class=""></div><div> readFromData(data: NSData, ofType: String) {</div><div> …</div><div><br class=""></div><div> if someFailureCondition {</div><div> throw Error.RanOutOfCake</div><div> }</div><div><br class=""></div><div> ...</div><div> }</div><div><br class=""></div><div> ...</div><div>}</div><div><br class=""></div><div>The readFromData method above will be called by Objective-C, since the internal NSDocument mechanism will be the one calling it. Since it will have no idea how to represent that, the error dialog box that appears will simply say something unhelpful like “MyApp.MyDocument.Error error 1”. Now, there are two ways to solve this, both of which involve throwing an NSError rather than a custom error type:</div><div><br class=""></div><div>Solution 1: Get rid of the ErrorType.</div><div><br class=""></div><div><div>class MyDocument: NSDocument {</div><div> let ErrorDomain = “MyApp.MyDocument”</div><div> enum ErrorCode: Int {</div><div> case MadeAMistake = 1</div><div> case RanOutOfCake = 2</div><div> }</div><div><br class=""></div><div> …</div><div><br class=""></div><div> readFromData(data: NSData, ofType: String) {</div><div> …</div><div><br class=""></div><div> if someFailureCondition {</div><div> let userInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString(“Looks like we ran out of cake.”, comment: “Ran out of cake”)]</div><div> throw NSError(domain: ErrorDomain, code: ErrorCode.RanOutOfCake.rawValue, userInfo: userInfo)</div><div> }</div><div><br class=""></div><div> ...</div><div> }</div><div><br class=""></div><div> ...</div><div>}</div><div><br class=""></div><div>This has a couple of problems. First, it forces us to use NSError. Second, it’s ugly. Imagine a method with many possible failure points, all filled with the userInfo building above.</div><div><br class=""></div><div>The second solution is a little better:</div><div><br class=""></div><div><div>class MyDocument: NSDocument {</div><div> enum Error: ErrorType {</div><div> let ErrorDomain = “MyApp.MyDocument”</div><div><br class=""></div><div> case MadeAMistake</div><div> case RanOutOfCake</div><div><br class=""></div><div> func toNSError() -> NSError {</div><div> let failureReason: String</div><div> let code: Int</div><div><br class=""></div><div> switch self {</div><div> case .MadeAMistake:</div><div> failureReason = NSLocalizedString(“Looks like we made a mistake.”, comment: “Made a mistake”)</div><div> code = 1</div><div> case .RanOutOfCake:</div><div> failureReason = NSLocalizedString(“Looks like we ran out of cake.”, comment: “Ran out of cake”)</div><div> code = 2</div><div> }</div><div><br class=""></div><div> let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]</div><div><br class=""></div><div> return NSError(domain: self.ErrorDomain, code: code, userInfo: userInfo)</div><div> }</div><div> }</div><div><br class=""></div><div> …</div><div><br class=""></div><div> readFromData(data: NSData, ofType: String) {</div><div> …</div><div><br class=""></div><div> if someFailureCondition {</div><div> throw Error.RanOutOfCake.toNSError()</div><div> }</div><div><br class=""></div><div> ...</div><div> }</div><div><br class=""></div><div> ...</div><div>}</div></div></div><div><br class=""></div><div>The good news is that now the ugliness is removed from the actual program code and confined to the error type’s declaration. The bad news is that if we forget to put .toNSError() on an error we throw somewhere and that bubbles back to Cocoa, the user gets a meaningless error message. Furthermore, if we call through to some other error-throwing method, we have to catch and convert any errors it might throw:</div><div><br class=""></div><div><div>readFromData(data: NSData, ofType: String) {</div><div> …</div><div><br class=""></div><div> do {</div><div> try somethingThatThrows()</div><div> } catch {</div><div> if let myError = error as? Error {</div><div> throw myError.toNSError()</div><div> } else if let someOtherError = error as? SomeOtherErrorType {</div><div> // convert to NSError somehow</div><div> } else if let yetAnotherError = …</div><div> etc. etc. etc.</div><div> } else {</div><div> throw error</div><div> }</div><div> }</div><div><br class=""></div><div> …</div><div><br class=""></div><div>}</div></div><div><br class=""></div><div>At this point it’s probably just to use NSError all the way through. :-/</div><div><br class=""></div><div>With my proposal, all you’d do is this:</div><div><br class=""></div><div><div>class MyDocument: NSDocument {</div><div> enum Error: ErrorType {</div><div> case MadeAMistake</div><div> case RanOutOfCake</div><div><br class=""></div><div><div> var userInfo: [NSObject : AnyObject] {</div><div> let failureReason: String</div><div><br class=""></div><div> switch self {</div><div> case .MadeAMistake:</div><div> failureReason = NSLocalizedString(“Looks like we made a mistake.”, comment: “Made a mistake”)</div><div> case .RanOutOfCake:</div><div> failureReason = NSLocalizedString(“Looks like we ran out of cake.”, comment: “Ran out of cake”)</div><div> }</div><div><br class=""></div><div> return [NSLocalizedFailureReasonErrorKey: failureReason]</div><div> }</div></div><div> }</div><div><br class=""></div><div> …</div><div><br class=""></div><div> readFromData(data: NSData, ofType: String) {</div><div> …</div><div><br class=""></div><div> if someFailureCondition {</div><div> throw Error.RanOutOfCake</div><div> }</div><div><br class=""></div><div> ...</div><div> }</div><div><br class=""></div><div> ...</div><div>}</div><div><br class=""></div><div>Much simpler, much, much less failure prone, and cleaner too since we don’t have to worry about domains or codes anymore, as we can just use the default values for those now.</div></div><br class=""><blockquote type="cite" class=""><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">The drawbacks I see with exposing NSError’s properties on every ErrorType is that those properties are only there as a hack to interoperate with NSError. Really they’d go away as Swift evolves it’s own default error representation, or language leaves it to developer to build their own.<span class="Apple-converted-space"> </span></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span class="Apple-tab-span" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">        </span><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Essentially, I think that if you want to improve ErrorType, formalizing its relationship to NSError is the wrong way. NSErrors are not good error representations, especially in Swift, and we should move away from them as soon as possible.</span></div></blockquote></div><div class=""><br class=""></div><div class="">I don’t think Cocoa is going away for a very, very long time. Even if it did, you’d still need some consistent way to turn an ErrorType into something human-readable, and currently we don’t have that. NSError’s userInfo field actually does a fairly decent job, aside from the absurdly long constant names.</div><br class=""><div class="">Charles</div><div class=""><br class=""></div></body></html>