Add error information to reports
This commit is contained in:
parent
8594446dd1
commit
36ca378313
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// https://mczachurski.dev
|
||||
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
||||
// Licensed under the Apache License 2.0.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum ReportError: Error {
|
||||
case noSelfReports
|
||||
case invalidObjectId
|
||||
case duplicate
|
||||
case invalidParameters
|
||||
case invalidType
|
||||
case invalidObject
|
||||
}
|
||||
|
||||
extension ReportError: LocalizedError {
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
case .noSelfReports:
|
||||
return NSLocalizedString("report.error.noSelfReports",
|
||||
bundle: Bundle.module,
|
||||
comment: "Self-reporting is not allowed.")
|
||||
case .invalidObjectId:
|
||||
return NSLocalizedString("report.error.invalidObjectId",
|
||||
bundle: Bundle.module,
|
||||
comment: "Incorrect object Id.")
|
||||
case .duplicate:
|
||||
return NSLocalizedString("report.error.duplicate",
|
||||
bundle: Bundle.module,
|
||||
comment: "The report has already been sent.")
|
||||
case .invalidParameters:
|
||||
return NSLocalizedString("report.error.invalidParameters",
|
||||
bundle: Bundle.module,
|
||||
comment: "Invalid report parameters.")
|
||||
case .invalidType:
|
||||
return NSLocalizedString("report.error.invalidType",
|
||||
bundle: Bundle.module,
|
||||
comment: "Invalid report type.")
|
||||
case .invalidObject:
|
||||
return NSLocalizedString("report.error.invalidObject",
|
||||
bundle: Bundle.module,
|
||||
comment: "Invalid object type.")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,29 @@ public extension PixelfedClientAuthenticated {
|
|||
withBearerToken: token
|
||||
)
|
||||
|
||||
return try await downloadJson(Report.self, request: request)
|
||||
// API reports returns bad request even if data is correct.
|
||||
let (data, response) = try await urlSession.data(for: request)
|
||||
|
||||
guard (response as? HTTPURLResponse)?.status?.responseType == .success else {
|
||||
if let json = String(data: data, encoding: .utf8) {
|
||||
if json.contains("ERROR_NO_SELF_REPORTS") {
|
||||
throw ReportError.noSelfReports
|
||||
} else if json.contains("ERROR_INVALID_OBJECT_ID") {
|
||||
throw ReportError.invalidObjectId
|
||||
} else if json.contains("ERROR_REPORT_DUPLICATE") {
|
||||
throw ReportError.duplicate
|
||||
} else if json.contains("ERROR_INVALID_PARAMS") {
|
||||
throw ReportError.invalidParameters
|
||||
} else if json.contains("ERROR_TYPE_INVALID") {
|
||||
throw ReportError.invalidType
|
||||
} else if json.contains("ERROR_REPORT_OBJECT_TYPE_INVALID") {
|
||||
throw ReportError.invalidObject
|
||||
}
|
||||
}
|
||||
|
||||
throw NetworkError.notSuccessResponse(response)
|
||||
}
|
||||
|
||||
return try JSONDecoder().decode(Report.self, from: data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
// MARK: Network errors.
|
||||
"global.error.notSuccessResponse" = "Server response: %@.";
|
||||
"global.error.unknownError" = "Unexpected error.";
|
||||
|
||||
// Mark: Report errors.
|
||||
"report.error.noSelfReports" = "Self-reporting is not allowed.";
|
||||
"report.error.invalidObjectId" = "Incorrect object Id.";
|
||||
"report.error.duplicate" = "The report has already been sent.";
|
||||
"report.error.invalidParameters" = "Invalid report parameters.";
|
||||
"report.error.invalidType" = "Invalid report type.";
|
||||
"report.error.invalidObject" = "Invalid object type.";
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
// MARK: Network errors.
|
||||
"global.error.notSuccessResponse" = "Zerbitzariaren erantzuna: %@.";
|
||||
"global.error.unknownError" = "Espero ez zen errorea.";
|
||||
|
||||
// Mark: Report errors.
|
||||
"report.error.noSelfReports" = "Self-reporting is not allowed.";
|
||||
"report.error.invalidObjectId" = "Incorrect object Id.";
|
||||
"report.error.duplicate" = "The report has already been sent.";
|
||||
"report.error.invalidParameters" = "Invalid report parameters.";
|
||||
"report.error.invalidType" = "Invalid report type.";
|
||||
"report.error.invalidObject" = "Invalid object type.";
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
// MARK: Network errors.
|
||||
"global.error.notSuccessResponse" = "Odpowiedź serwera: %@.";
|
||||
"global.error.unknownError" = "Nieznany błąd serwera.";
|
||||
|
||||
// Mark: Report errors.
|
||||
"report.error.noSelfReports" = "Zgłaszanie siebie jest niedozwolone.";
|
||||
"report.error.invalidObjectId" = "Niepoprawny Id obiektu.";
|
||||
"report.error.duplicate" = "Zgłoszenie zostało już wysłane.";
|
||||
"report.error.invalidParameters" = "Niepoprawne parametry zgłoszenia.";
|
||||
"report.error.invalidType" = "Niepoprawny typ raportu.";
|
||||
"report.error.invalidObject" = "Niepoprawny typ obiektu.";
|
||||
|
|
|
@ -14,7 +14,7 @@ public class ErrorService {
|
|||
public func handle(_ error: Error, message: String, showToastr: Bool = false) {
|
||||
if showToastr {
|
||||
switch error {
|
||||
case is NetworkError, is URLError:
|
||||
case is LocalizedError:
|
||||
ToastrService.shared.showError(title: message, subtitle: error.localizedDescription)
|
||||
default:
|
||||
ToastrService.shared.showError(subtitle: message)
|
||||
|
|
Loading…
Reference in New Issue