Skip to content

Commit

Permalink
[Refactor] Discord Error Handler 객체 변경 (#620)
Browse files Browse the repository at this point in the history
* feat: discord ErrorHandler 리팩토링

* feat: refactor코드 변경, SUSUError 객체 변경
  • Loading branch information
MaraMincho authored Oct 6, 2024
1 parent d045577 commit a30a70d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Projects/Core/FeatureAction/Sources/ssRun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public extension Effect {
column: UInt = #column
) -> Self {
// Show Default Network Alert
let defaultErrorHandler: (@Sendable (_ error: Error, _ send: Send<Action>) async -> Void) = { _, _ in
let defaultErrorHandler: @Sendable (_ error: Error, _ send: Send<Action>) async -> Void = { _, _ in
NotificationCenter.default.post(name: SSNotificationName.showDefaultNetworkErrorAlert, object: nil)
}
return .run(priority: priority, operation: operation) { error, send in
Expand Down
20 changes: 14 additions & 6 deletions Projects/Core/SSNetwork/Sources/Moya+Asynchronous.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ public extension MoyaProvider {
if 200 ..< 300 ~= response.statusCode {
guard let res = try? JSONDecoder.default.decode(T.self, from: response.data) else {
let data = String(data: response.data, encoding: .utf8) ?? ""
os_log("JSON mapping error occurred \n \(data)")
continuation.resume(throwing: MoyaError.jsonMapping(response))
return
}
continuation.resume(returning: res)
return
}
continuation.resume(with: .failure(MoyaError.statusCode(response)))
continuation.resume(throwing: SUSUError(error: MoyaError.statusCode(response), response: response))

case let .failure(error):
if let errorDescription = error.errorDescription {
Expand All @@ -65,11 +64,11 @@ public extension MoyaProvider {
switch error {
case let .statusCode(response):
let requestData = String(data: response.request?.httpBody, encoding: .utf8)
willSendError = SUSUError(error: error, response: response, requestData: requestData)
willSendError = SUSUError(error: error, response: response)

case let .underlying(error, response):
let requestData = String(data: response?.request?.httpBody, encoding: .utf8)
willSendError = SUSUError(error: error, response: response, requestData: requestData)
willSendError = SUSUError(error: error, response: response)

default:
willSendError = error
Expand Down Expand Up @@ -126,14 +125,23 @@ public extension MoyaProvider {
// MARK: - SUSUError

public struct SUSUError<E: Error>: LocalizedError {
public var errorDescription: String?
let error: E
let response: Moya.Response?
let errorResponseMessage: String?
let statusCode: Int?
let requestData: String?
let urlString: String?
let httpMethod: String?

public init(error: E, response: Moya.Response?, requestData: String?) {
public init(error: E, response: Moya.Response?) {
self.error = error
self.response = response
self.requestData = requestData
requestData = String(data: response?.request?.httpBody, encoding: .utf8)
statusCode = response?.statusCode
urlString = response?.request?.url?.absoluteString
httpMethod = response?.request?.method?.rawValue
errorResponseMessage = String(data: response?.data, encoding: .utf8)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public struct SpecificEnvelopeDetailReducer: Sendable {
return .ssRun { send in
guard let property else {
let errorMessage = "envelopeDetailProperty가 생성되지 않은 상태에서 edit화면으로 이동하려고 합니다."
throw SUSUError(error: NSError(), response: nil, requestData: errorMessage)
throw SUSUError(error: NSError(), response: nil)
}
await send(.delegate(.tappedEnvelopeEditButton(property)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import SSNotification

// MARK: - DiscordErrorHandler

public final class DiscordErrorHandler: @unchecked Sendable {
public static let shared = DiscordErrorHandler()
public final class DiscordErrorHandler {
public nonisolated(unsafe) static let shared = DiscordErrorHandler()
var subscription: AnyCancellable?
private init() {}

public func registerDiscordLogSystem() {
subscription = NotificationCenter.default.publisher(for: SSNotificationName.logError)
.sink { @Sendable errorObjectOutput in
.sink { errorObjectOutput in
let errorObject = errorObjectOutput.object as? String
Self.sendErrorToDiscord(errorMessage: errorObject)
}
Expand Down

0 comments on commit a30a70d

Please sign in to comment.