Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] #256 - Mission error 로직 처리 #257

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iOS-NOTTODO/iOS-NOTTODO/Network/Base/APIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions iOS-NOTTODO/iOS-NOTTODO/Network/Base/ErrorReponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protocol MissionServiceProtocol {
typealias DefaultMissionService = BaseService<MissionAPI>

extension DefaultMissionService: MissionServiceProtocol {

func getDailyMission(date: String) -> AnyPublisher<DailyMissionData, Error> {
return requestWithCombine(MissionAPI.dailyMission(date: date))
}
Expand Down Expand Up @@ -47,9 +47,9 @@ protocol MissionServiceType {
func getRecentMission(completion: @escaping (RecentMissionData?) -> Void)
func deleteMission(id: Int, completion: @escaping (GeneralResponse<VoidType>?) -> 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 {
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down