Skip to content

Commit

Permalink
feat #144: 200대 statusCode가 아닌 경우 ErrorResponse 값 반환 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
minsangKang committed Jun 9, 2024
1 parent c6aa580 commit e35e4ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Project_Timer/Data/Error/NetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Moya

enum NetworkError: Error {
case FAIL
Expand All @@ -17,6 +18,7 @@ enum NetworkError: Error {
case NOTFOUND(String?) // 404
case CONFLICT(String?) // 409
case SERVERERROR(String?) // 500
case ERRORRESPONSE(ErrorResponse) // TiTi ErrorResponse

static func error(_ result: NetworkResult) -> NetworkError {
switch result.status {
Expand Down Expand Up @@ -48,6 +50,14 @@ enum NetworkError: Error {
}
}

/// ErrorResponse 반환
static func errorResponse(_ response: Response) -> NetworkError {
guard let errorResponse = try? JSONDecoder().decode(ErrorResponse.self, from: response.data) else {
return .serverError(statusCode: response.statusCode)
}
return .ERRORRESPONSE(errorResponse)
}

/// 범용적으로 표시될 수 있는 alert title 값, CLIENTERROR의 경우 VM에서 처리
var title: String {
switch self {
Expand Down
6 changes: 6 additions & 0 deletions Project_Timer/Data/TTProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ final class TTProvider<T: TargetType>: MoyaProvider<T> {
super.request(token) { result in
switch result {
case .success(let response):
if (200...299).contains(response.statusCode) {
promise(.success(response))
} else {
// ErrorResponse 디코딩
promise(.failure(NetworkError.errorResponse(response)))
}
promise(.success(response))
case .failure(let error):
promise(.failure(self.handleError(error)))
Expand Down

0 comments on commit e35e4ad

Please sign in to comment.