-
Notifications
You must be signed in to change notification settings - Fork 2
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
FEAT: 상황에 따른 에러 핸들링 #16
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ff20c62
create error enum
ittzggd 6b69591
FEAT: add more error cases
ittzggd eac3ca5
FEAT: homeVM에 에러 핸들링 적용
ittzggd 8a96b38
remove unused comment
ittzggd 02a3bc2
test
ittzggd ff14031
remove unused code
ittzggd 4a39ff5
resolve conflicts
ittzggd 55f1954
Merge branch 'dev' into feat/errorHandler/2
ittzggd 923ab7d
resolve conflicts in project.pbxproj
ittzggd 0791aa8
resolve conflicts
ittzggd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 코드가 클라이언트 에러를 구분하는 책임을 맡은 만큼 함수 이름에서도 errorFromHttpRequest처럼 그 부분이 보였으면 좋겠습니다.