diff --git a/PixelfedKit/Sources/PixelfedKit/Errors/ReportError.swift b/PixelfedKit/Sources/PixelfedKit/Errors/ReportError.swift new file mode 100644 index 0000000..834fe2b --- /dev/null +++ b/PixelfedKit/Sources/PixelfedKit/Errors/ReportError.swift @@ -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.") + } + } +} diff --git a/PixelfedKit/Sources/PixelfedKit/PixelfedClient+Report.swift b/PixelfedKit/Sources/PixelfedKit/PixelfedClient+Report.swift index 6e8a826..66ffd4f 100644 --- a/PixelfedKit/Sources/PixelfedKit/PixelfedClient+Report.swift +++ b/PixelfedKit/Sources/PixelfedKit/PixelfedClient+Report.swift @@ -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) } } diff --git a/PixelfedKit/Sources/PixelfedKit/Resources/en.lproj/Localizable.strings b/PixelfedKit/Sources/PixelfedKit/Resources/en.lproj/Localizable.strings index 40f7506..8106e7b 100644 --- a/PixelfedKit/Sources/PixelfedKit/Resources/en.lproj/Localizable.strings +++ b/PixelfedKit/Sources/PixelfedKit/Resources/en.lproj/Localizable.strings @@ -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."; diff --git a/PixelfedKit/Sources/PixelfedKit/Resources/eu.lproj/Localizable.strings b/PixelfedKit/Sources/PixelfedKit/Resources/eu.lproj/Localizable.strings index 3de0a44..68e0fa1 100644 --- a/PixelfedKit/Sources/PixelfedKit/Resources/eu.lproj/Localizable.strings +++ b/PixelfedKit/Sources/PixelfedKit/Resources/eu.lproj/Localizable.strings @@ -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."; diff --git a/PixelfedKit/Sources/PixelfedKit/Resources/pl.lproj/Localizable.strings b/PixelfedKit/Sources/PixelfedKit/Resources/pl.lproj/Localizable.strings index 6d6bf82..f570b2c 100644 --- a/PixelfedKit/Sources/PixelfedKit/Resources/pl.lproj/Localizable.strings +++ b/PixelfedKit/Sources/PixelfedKit/Resources/pl.lproj/Localizable.strings @@ -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."; diff --git a/ServicesKit/Sources/ServicesKit/ErrorsService.swift b/ServicesKit/Sources/ServicesKit/ErrorsService.swift index c00d415..8892b52 100644 --- a/ServicesKit/Sources/ServicesKit/ErrorsService.swift +++ b/ServicesKit/Sources/ServicesKit/ErrorsService.swift @@ -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)