Automatically report errors displayed to the user

This commit is contained in:
Shadowfacts 2022-12-29 17:30:39 -05:00
parent 4f655bb80a
commit 5ab22e742b
1 changed files with 32 additions and 2 deletions

View File

@ -8,6 +8,7 @@
import UIKit import UIKit
import Pachyderm import Pachyderm
import Sentry
struct ToastConfiguration { struct ToastConfiguration {
var systemImageName: String? var systemImageName: String?
@ -38,7 +39,7 @@ extension ToastConfiguration {
init(from error: Error, with title: String, in viewController: UIViewController, retryAction: ((ToastView) -> Void)?) { init(from error: Error, with title: String, in viewController: UIViewController, retryAction: ((ToastView) -> Void)?) {
self.init(title: title) self.init(title: title)
// localizedDescription is statically dispatched, so we need to call it after the downcast // localizedDescription is statically dispatched, so we need to call it after the downcast
if let error = error as? Client.Error { if let error = error as? Pachyderm.Client.Error {
self.subtitle = error.localizedDescription self.subtitle = error.localizedDescription
self.systemImageName = error.systemImageName self.systemImageName = error.systemImageName
self.longPressAction = { [unowned viewController] toast in self.longPressAction = { [unowned viewController] toast in
@ -54,6 +55,35 @@ extension ToastConfiguration {
}) })
viewController.present(reporter, animated: true) viewController.present(reporter, animated: true)
} }
// TODO: this is a bizarre place to do this, but code path covers basically all errors
switch error.type {
case .invalidRequest, .invalidResponse, .invalidModel(_), .mastodonError(_):
SentrySDK.capture(error: error) { scope in
let crumb = Breadcrumb(level: .error, category: "error")
crumb.message = title
crumb.data = [
"request_method": error.requestMethod.name,
"request_endpoint": error.requestEndpoint.description,
]
switch error.type {
case .invalidRequest:
crumb.data!["error_type"] = "invalid_request"
case .invalidResponse:
crumb.data!["error_type"] = "invalid_response"
case .invalidModel(let error):
crumb.data!["error_type"] = "invalid_model"
crumb.data!["underlying_error"] = String(describing: error)
case .mastodonError(let error):
crumb.data!["error_type"] = "mastodon_error"
crumb.data!["underlying_error"] = error
default:
break
}
scope.add(crumb)
}
default:
break
}
} else { } else {
self.subtitle = error.localizedDescription self.subtitle = error.localizedDescription
self.systemImageName = "exclamationmark.triangle" self.systemImageName = "exclamationmark.triangle"
@ -73,7 +103,7 @@ extension ToastConfiguration {
} }
} }
fileprivate extension Client.Error { fileprivate extension Pachyderm.Client.Error {
var systemImageName: String { var systemImageName: String {
switch type { switch type {
case .networkError(_): case .networkError(_):