Improves error messages when there’s an underlying error

This commit is contained in:
Riley Testut
2020-06-05 15:32:10 -07:00
parent db87d9ca7b
commit 0be1be5769

View File

@@ -8,6 +8,12 @@
import Roxas import Roxas
extension TimeInterval
{
static let shortToastViewDuration = 4.0
static let longToastViewDuration = 8.0
}
class ToastView: RSTToastView class ToastView: RSTToastView
{ {
var preferredDuration: TimeInterval var preferredDuration: TimeInterval
@@ -16,11 +22,11 @@ class ToastView: RSTToastView
{ {
if detailedText == nil if detailedText == nil
{ {
self.preferredDuration = 4.0 self.preferredDuration = .shortToastViewDuration
} }
else else
{ {
self.preferredDuration = 8.0 self.preferredDuration = .longToastViewDuration
} }
super.init(text: text, detailText: detailedText) super.init(text: text, detailText: detailedText)
@@ -38,7 +44,22 @@ class ToastView: RSTToastView
convenience init(error: Error) convenience init(error: Error)
{ {
let error = error as NSError var error = error as NSError
var underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError
var preferredDuration: TimeInterval?
if
let unwrappedUnderlyingError = underlyingError,
error.domain == AltServerErrorDomain && error.code == ALTServerError.Code.underlyingError.rawValue
{
// Treat underlyingError as the primary error.
error = unwrappedUnderlyingError
underlyingError = nil
preferredDuration = .longToastViewDuration
}
let text: String let text: String
let detailText: String? let detailText: String?
@@ -46,20 +67,25 @@ class ToastView: RSTToastView
if let failure = error.localizedFailure if let failure = error.localizedFailure
{ {
text = failure text = failure
detailText = error.localizedFailureReason ?? error.localizedRecoverySuggestion ?? error.localizedDescription detailText = error.localizedFailureReason ?? error.localizedRecoverySuggestion ?? underlyingError?.localizedDescription ?? error.localizedDescription
} }
else if let reason = error.localizedFailureReason else if let reason = error.localizedFailureReason
{ {
text = reason text = reason
detailText = error.localizedRecoverySuggestion detailText = error.localizedRecoverySuggestion ?? underlyingError?.localizedDescription
} }
else else
{ {
text = error.localizedDescription text = error.localizedDescription
detailText = nil detailText = underlyingError?.localizedDescription
} }
self.init(text: text, detailText: detailText) self.init(text: text, detailText: detailText)
if let preferredDuration = preferredDuration
{
self.preferredDuration = preferredDuration
}
} }
required init(coder aDecoder: NSCoder) { required init(coder aDecoder: NSCoder) {