[ToastView]: Fix: restore back to printing localizedDescription as before

This commit is contained in:
Magesh K
2025-01-21 21:53:29 +05:30
parent c0a81edf6b
commit 1641f6e93f
3 changed files with 25 additions and 12 deletions

View File

@@ -65,13 +65,23 @@ class ToastView: RSTToastView
self.opensErrorLog = opensLog
}
convenience init(error: Error)
enum InfoMode: String {
case fullError
case localizedDescription
}
convenience init(error: Error){
self.init(error: error, mode: .localizedDescription)
}
convenience init(error: Error, mode: InfoMode)
{
let error = error as NSError
let mode = mode == .fullError ? ErrorProcessing.InfoMode.fullError : ErrorProcessing.InfoMode.localizedDescription
let text = error.localizedTitle ?? NSLocalizedString("Operation Failed", comment: "")
let detailText = ErrorProcessing(.fullError).getDescription(error: error)
let detailText = ErrorProcessing(mode).getDescription(error: error)
self.init(text: text, detailText: detailText)
}

View File

@@ -316,7 +316,7 @@ extension LaunchViewController
let errorDesc = ErrorProcessing(.fullError).getDescription(error: error as NSError)
print("Failed to update sources on launch. \(errorDesc)")
let toastView = ToastView(error: error)
let toastView = ToastView(error: error, mode: .fullError)
toastView.addTarget(self.destinationViewController, action: #selector(TabBarController.presentSources), for: .touchUpInside)
toastView.show(in: self.destinationViewController.selectedViewController ?? self.destinationViewController)
}

View File

@@ -27,7 +27,7 @@ class ErrorProcessing {
self.recur = recur
}
private func processError(_ error: NSError, getMoreErrors: (_ error: NSError)->String) -> String{
private func processError(_ error: NSError, ignoreTitle: Bool = false, getMoreErrors: (_ error: NSError)->String) -> String{
// if unique was requested and if this error is duplicate, ignore processing it
let serializedError = "\(error)"
if unique && errors.contains(serializedError) {
@@ -39,7 +39,7 @@ class ErrorProcessing {
var desc = ""
switch (info){
case .localizedDescription:
title = (error.localizedTitle.map{$0+"\n"} ?? "")
title = !ignoreTitle ? (error.localizedTitle.map{$0+"\n"} ?? "") : ""
desc = error.localizedDescription
case .fullError:
desc = serializedError
@@ -54,13 +54,14 @@ class ErrorProcessing {
return getDescriptionText(error: error)
}
private lazy var recurseErrors = { error in
self.getDescriptionText(error: error) // recursively process underlying error(s) if any
}
func getDescriptionText(error: NSError) -> String{
func getDescriptionText(error: NSError,_ depth: Int = 0) -> String{
// closure
let recurseErrors = { error in
self.getDescriptionText(error: error, depth+1) // recursively process underlying error(s) if any
}
var description = ""
// process current error only if recur was not requested
let processMoreErrors = recur ? recurseErrors : {_ in ""}
@@ -74,7 +75,9 @@ class ErrorProcessing {
let error = underlyingError as NSError
description += processError(error, getMoreErrors: processMoreErrors)
} else {
description += processError(error, getMoreErrors: processMoreErrors)
// ignore the title for the base error since we wanted this to be description
let isBaseError = (depth == 0)
description += processError(error, ignoreTitle: isBaseError, getMoreErrors: processMoreErrors)
}
return description
}