diff --git a/iOS-NOTTODO/iOS-NOTTODO/Network/Base/APIError.swift b/iOS-NOTTODO/iOS-NOTTODO/Network/Base/APIError.swift index e260a0c4..62c1c813 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Network/Base/APIError.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Network/Base/APIError.swift @@ -7,7 +7,7 @@ import Foundation -public enum APIError: Error, Equatable { +enum APIError: Error, Equatable { case network(statusCode: Int, response: ErrorResponse) case unknown case tokenReissuanceFailed diff --git a/iOS-NOTTODO/iOS-NOTTODO/Network/Base/ErrorReponse.swift b/iOS-NOTTODO/iOS-NOTTODO/Network/Base/ErrorReponse.swift index 3aa21914..c0b7489c 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Network/Base/ErrorReponse.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Network/Base/ErrorReponse.swift @@ -7,7 +7,8 @@ import Foundation -public struct ErrorResponse: Decodable, Equatable { - public let statusCode: String - public let responseMessage: String +struct ErrorResponse: Decodable, Equatable { + let status: Int + let success: Bool + let message: String } diff --git a/iOS-NOTTODO/iOS-NOTTODO/Network/Service/Mission/MissionService.swift b/iOS-NOTTODO/iOS-NOTTODO/Network/Service/Mission/MissionService.swift index 69531f97..f40a6837 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Network/Service/Mission/MissionService.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Network/Service/Mission/MissionService.swift @@ -19,7 +19,7 @@ protocol MissionServiceProtocol { typealias DefaultMissionService = BaseService extension DefaultMissionService: MissionServiceProtocol { - + func getDailyMission(date: String) -> AnyPublisher { return requestWithCombine(MissionAPI.dailyMission(date: date)) } @@ -47,9 +47,9 @@ protocol MissionServiceType { func getRecentMission(completion: @escaping (RecentMissionData?) -> Void) func deleteMission(id: Int, completion: @escaping (GeneralResponse?) -> Void) func patchUpdateMissionStatus(id: Int, status: String, completion: @escaping (UpdateMissionStatus?) -> Void) - func postAnotherDay(id: Int, dates: [String], completion: @escaping (AddAnotherDay?) -> Void) - func postAddMission(request: AddMissionRequest, completion: @escaping(AddMissionsData?) -> Void) - func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(UpdateMissionData?) -> Void) + func postAnotherDay(id: Int, dates: [String], completion: @escaping (ErrorResponse?) -> Void) + func postAddMission(request: AddMissionRequest, completion: @escaping(ErrorResponse?) -> Void) + func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(ErrorResponse?) -> Void) } final class MissionService: MissionServiceType { @@ -202,56 +202,92 @@ final class MissionService: MissionServiceType { // MARK: - Post - func postAnotherDay(id: Int, dates: [String], completion: @escaping (AddAnotherDay?) -> Void) { + func postAnotherDay(id: Int, dates: [String], completion: @escaping (ErrorResponse?) -> Void) { provider.request(.addAnotherDay(id: id, dates: dates)) { result in switch result { case .success(let response): do { let response = try response.map(AddAnotherDay?.self) - completion(response) + completion(nil) } catch let err { print(err.localizedDescription, 500) + completion(nil) } case .failure(let err): - print(err.localizedDescription) - completion(nil) + if let response = err.response { + do { + let errorResponse = try response.map(ErrorResponse.self) + print("💎 Server Error 💎: \(errorResponse.message)") + completion(errorResponse) + } catch { + print("💎 Mapping Error 💎: \(error.localizedDescription)") + completion(nil) + } + } else { + print("💎 Network Error 💎: \(err.localizedDescription)") + completion(nil) + } } } } func postAddMission(request: AddMissionRequest, - completion: @escaping(AddMissionsData?) -> Void) { + completion: @escaping(ErrorResponse?) -> Void) { provider.request(.addMission(request: request)) { result in switch result { case .success(let response): do { let response = try response.map(AddMissionsData?.self) - completion(response) + completion(nil) } catch let err { print(err.localizedDescription, 500) + completion(nil) } case .failure(let err): print(err.localizedDescription) - completion(nil) + if let response = err.response { + do { + let errorResponse = try response.map(ErrorResponse.self) + print("💎 Server Error 💎: \(errorResponse.message)") + completion(errorResponse) + } catch { + print("💎 Mapping Error 💎: \(error.localizedDescription)") + completion(nil) + } + } else { + print("💎 Network Error 💎: \(err.localizedDescription)") + completion(nil) + } } } } // MARK: PUT - func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(UpdateMissionData?) -> Void) { + func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(ErrorResponse?) -> Void) { provider.request(.updateMission(request: request)) { result in switch result { case .success(let response): do { let response = try response.map(UpdateMissionData?.self) - completion(response) + completion(nil) } catch let err { print(err.localizedDescription, 500) } case .failure(let err): - print(err.localizedDescription) - completion(nil) + if let response = err.response { + do { + let errorResponse = try response.map(ErrorResponse.self) + print("💎 Server Error 💎: \(errorResponse.message)") + completion(errorResponse) + } catch { + print("💎 Mapping Error 💎: \(error.localizedDescription)") + completion(nil) + } + } else { + print("💎 Network Error 💎: \(err.localizedDescription)") + completion(nil) + } } } } diff --git a/iOS-NOTTODO/iOS-NOTTODO/Presentation/AddMission/ViewControllers/AddMissionViewController.swift b/iOS-NOTTODO/iOS-NOTTODO/Presentation/AddMission/ViewControllers/AddMissionViewController.swift index 01afc324..87416db5 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Presentation/AddMission/ViewControllers/AddMissionViewController.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Presentation/AddMission/ViewControllers/AddMissionViewController.swift @@ -280,49 +280,47 @@ extension AddMissionViewController { private func requestPostAddMission(title: String, situation: String, actions: [String]?, goal: String?, dates: [String]?) { let request = AddMissionRequest(title: title, situation: situation, actions: actions, goal: goal, dates: dates ?? [""]) - MissionService.shared.postAddMission(request: request) { response in - guard let response = response else { return } - switch response.status { - case 200..<300: - AmplitudeAnalyticsService.shared.send( - event: AnalyticsEvent.CreateMission.completeCreateMission( - date: dates ?? [], - goal: goal ?? "", - title: title, - situation: situation, - action: actions ?? [] - ) - ) - self.coordinator?.dismiss() - default: - let toastMessage = self.htmlToString(response.message ?? "")?.string ?? "" + MissionService.shared.postAddMission(request: request) { err in + if let err = err { + let toastMessage = self.htmlToString(err.message)?.string ?? "" self.checkToastMessage(toastMessage) self.showToast(message: toastMessage, controller: self) + return } + + AmplitudeAnalyticsService.shared.send( + event: AnalyticsEvent.CreateMission.completeCreateMission( + date: dates ?? [], + goal: goal ?? "", + title: title, + situation: situation, + action: actions ?? [] + ) + ) + self.coordinator?.dismiss() + } } private func requestPutUpdateMission(id: Int, title: String, situation: String, actions: [String]?, goal: String?) { let request = UpdateMissionRequest(id: id, title: title, situation: situation, actions: actions, goal: goal) - MissionService.shared.putUpdateMission(request: request) { response in - guard let response = response else { return } - print(response.status) - switch response.status { - case 200..<300: - AmplitudeAnalyticsService.shared.send( - event: AnalyticsEvent.UpdateMission.completeUpdateMission( - date: self.dateList, - goal: self.nottodoInfoList[4], - title: self.nottodoInfoList[1], - situation: self.nottodoInfoList[2], - action: self.nottodoInfoList[3]) - ) - self.coordinator?.dismiss() - default: - let toastMessage = self.htmlToString(response.message ?? "")?.string ?? "" + MissionService.shared.putUpdateMission(request: request) { err in + if let err = err { + let toastMessage = self.htmlToString(err.message)?.string ?? "" self.checkToastMessage(toastMessage) self.showToast(message: toastMessage, controller: self) + return } + + AmplitudeAnalyticsService.shared.send( + event: AnalyticsEvent.UpdateMission.completeUpdateMission( + date: self.dateList, + goal: self.nottodoInfoList[4], + title: self.nottodoInfoList[1], + situation: self.nottodoInfoList[2], + action: self.nottodoInfoList[3]) + ) + self.coordinator?.dismiss() } } diff --git a/iOS-NOTTODO/iOS-NOTTODO/Presentation/Home/ViewControllers/DetailCalendarViewController.swift b/iOS-NOTTODO/iOS-NOTTODO/Presentation/Home/ViewControllers/DetailCalendarViewController.swift index 839a7059..9f3a5784 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Presentation/Home/ViewControllers/DetailCalendarViewController.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Presentation/Home/ViewControllers/DetailCalendarViewController.swift @@ -211,27 +211,25 @@ extension DetailCalendarViewController: FSCalendarDelegate, FSCalendarDataSource extension DetailCalendarViewController { private func requestAddAnotherDay(id: Int, dates: [String]) { - MissionService.shared.postAnotherDay(id: id, dates: dates) { response in - guard response != nil else { return } - guard let statusCode = response?.status else { return } - switch statusCode { - case 200..<204: - self.setUI() - self.coordinator?.dismiss() - AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.SelectDate.closeAnotherDayModal) - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy.MM.dd" - if let earliestDate = dateFormatter.date(from: self.anotherDate.min() ?? "") { - let earliestDateString = dateFormatter.string(from: earliestDate) - self.movedateClosure?(earliestDateString) - } - default: - self.showToast(message: self.htmlToString(response?.message ?? "")?.string ?? "", controller: self) + MissionService.shared.postAnotherDay(id: id, dates: dates) { err in + if let err = err { + self.showToast(message: self.htmlToString(err.message)?.string ?? "", controller: self) AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.SelectDate.appearMaxedIssueMessage) + return + } + + self.setUI() + self.coordinator?.dismiss() + AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.SelectDate.closeAnotherDayModal) + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy.MM.dd" + if let earliestDate = dateFormatter.date(from: self.anotherDate.min() ?? "") { + let earliestDateString = dateFormatter.string(from: earliestDate) + self.movedateClosure?(earliestDateString) } } } - + func requestParticualrDatesAPI(id: Int) { MissionService.shared.particularMissionDates(id: id) { [weak self] response in guard let dates = response.data else { return }