-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from innovationacademy-kr/feat/errorHandler/2
FEAT: 상황에 따른 에러 핸들링
- Loading branch information
Showing
6 changed files
with
178 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// ErrorHandler.swift | ||
// 24HANE | ||
// | ||
// Created by Katherine JANG on 5/10/24. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
|
||
class ErrorHandler: ObservableObject { | ||
|
||
static let shared = ErrorHandler() | ||
|
||
var errorType: CustomError = .none | ||
@Published var showAlert: Bool = false | ||
@Published var signInRequired: Bool = false | ||
|
||
private init() { } | ||
|
||
func errorFromHttpRequest(_ statusCode: Int?) throws { | ||
switch statusCode { | ||
case 400: | ||
throw CustomError.wrongQueryType | ||
case 401: | ||
throw CustomError.unAuthorized | ||
case 500: | ||
throw CustomError.internalServer | ||
default: | ||
throw CustomError.unknownError("\(statusCode)") | ||
} | ||
} | ||
|
||
@MainActor | ||
func updateErrorView() { | ||
switch self.errorType { | ||
case .tokenExpired, .unAuthorized: | ||
self.signInRequired = true | ||
case .wrongQueryType, .networkDisconnected, .internalServer, .responseBodyEmpty, | ||
.decodeFailed, .unknownError, .invalidURL: | ||
self.showAlert = true | ||
case .none: | ||
break | ||
} | ||
} | ||
|
||
func verifyError(_ error: Error) async { | ||
switch error { | ||
case DecodingError.dataCorrupted: | ||
self.errorType = .decodeFailed | ||
case URLError.timedOut: | ||
self.errorType = .networkDisconnected | ||
case URLError.networkConnectionLost: | ||
self.errorType = .networkDisconnected | ||
case is CustomError: | ||
self.errorType = error as? CustomError ?? .none | ||
default: | ||
self.errorType = .unknownError(error.localizedDescription.description) | ||
} | ||
} | ||
|
||
@MainActor | ||
func handleError(_ error: Error) { | ||
Task { | ||
await self.verifyError(error) | ||
self.updateErrorView() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters