mastodon-ios/Mastodon/Extension/UIAlertController.swift

45 lines
1.1 KiB
Swift
Raw Normal View History

2021-02-03 09:01:08 +01:00
//
// UIAlertController.swift
// Mastodon
//
import UIKit
2021-03-01 09:27:24 +01:00
import MastodonSDK
2021-02-03 09:01:08 +01:00
// Reference:
// https://nshipster.com/swift-foundation-error-protocols/
extension UIAlertController {
convenience init(
for error: Error,
title: String?,
2021-02-03 09:01:08 +01:00
preferredStyle: UIAlertController.Style
) {
let _title: String
2021-02-03 09:01:08 +01:00
let message: String?
if let error = error as? LocalizedError {
var messages: [String?] = []
if let title = title {
_title = title
messages.append(error.errorDescription)
} else {
_title = error.errorDescription ?? "Error"
}
messages.append(contentsOf: [
2021-02-03 09:01:08 +01:00
error.failureReason,
error.recoverySuggestion
])
message = messages
.compactMap { $0 }
.joined(separator: " ")
2021-02-03 09:01:08 +01:00
} else {
_title = "Internal Error"
2021-02-03 09:01:08 +01:00
message = error.localizedDescription
}
self.init(
title: _title,
2021-02-03 09:01:08 +01:00
message: message,
preferredStyle: preferredStyle
)
}
}