Skip to content

Commit

Permalink
Merge pull request #40 from niscy-eudiw/main
Browse files Browse the repository at this point in the history
Improved error handling for Service errors
  • Loading branch information
stzouvaras authored Dec 3, 2024
2 parents d493698 + 3c3443f commit 52c7756
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Sources/Domain/Entities/Localization/LocalizableKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public enum LocalizableKey: String, Sendable {
case cancelSigningProcessSubtitle
case genericErrorButtonRetry
case genericErrorMessage
case genericServiceErrorMessage
case genericErrorDescription
case genericErrorDocumentNotFound
case genericErrorQtspNotFound
case sharingDocument
case closeSharingDocument
case close
case share
case unableToOpenBrowser

func defaultTranslation(args: [String]) -> String {
let value = switch self {
Expand Down Expand Up @@ -78,13 +80,15 @@ public enum LocalizableKey: String, Sendable {
case .cancelSigningProcessSubtitle: "Cancel will will redirect you back to the document list without signing your document."
case .genericErrorButtonRetry: "TRY AGAIN"
case .genericErrorMessage: "Oups! Something went wrong"
case .genericServiceErrorMessage: "Oups! Something went wrong with the RQES Signing Service"
case .genericErrorDescription: "If the issue persists, please contact customer support"
case .genericErrorDocumentNotFound: "No Document data found"
case .genericErrorQtspNotFound: "No selected QTSP found"
case .sharingDocument: "Sharing document"
case .closeSharingDocument: "Closing will redirect you back to the dashboard without saving or sharing the document."
case .close: "Close"
case .share: "Share"
case .unableToOpenBrowser: "Unable to open browser"
}
return value.format(arguments: args)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,19 @@ final class CredentialSelectionViewModel<Router: RouterGraph>: ViewModel<Router,
}

func initiate() async {

do {
try await getDocument()
} catch {
setErrorState(.genericErrorDocumentNotFound) {
self.onCancel()
}
}

do {
try await fetchCredentials()
} catch {
setErrorState {
setErrorState(.genericServiceErrorMessage) {
self.onCancel()
}
}
Expand All @@ -82,12 +90,12 @@ final class CredentialSelectionViewModel<Router: RouterGraph>: ViewModel<Router,
self.onPause()

await UIApplication.shared.openURLIfPossible(authorizationUrl) {
self.setErrorState {
self.setErrorState(.unableToOpenBrowser) {
self.setState { $0.copy(error: nil) }
}
}
} catch {
self.setErrorState {
self.setErrorState(.genericServiceErrorMessage) {
self.setState { $0.copy(error: nil) }
}
}
Expand Down Expand Up @@ -131,13 +139,16 @@ final class CredentialSelectionViewModel<Router: RouterGraph>: ViewModel<Router,
}
}

private func setErrorState(cancelAction: @escaping () -> ()) {
private func setErrorState(
_ desc: LocalizableKey,
cancelAction: @escaping () -> ()
) {
setState {
$0.copy(
isLoading: false,
error: ContentErrorView.Config(
title: .genericErrorMessage,
description: .genericErrorDocumentNotFound,
description: desc,
cancelAction: cancelAction,
action: { Task { await self.initiate() } }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ServiceSelectionViewModel<Router: RouterGraph>: ViewModel<Router, ServiceS
.copy(error: nil)
}
} else {
setErrorState {
setErrorState(.genericErrorQtspNotFound) {
self.router.pop()
}
}
Expand All @@ -77,7 +77,7 @@ class ServiceSelectionViewModel<Router: RouterGraph>: ViewModel<Router, ServiceS
if let selectedItem {
try await interactor.createRQESService(selectedItem)
} else {
setErrorState {
setErrorState(.genericErrorQtspNotFound) {
self.setState { $0.copy(error: nil) }
}
}
Expand All @@ -86,25 +86,28 @@ class ServiceSelectionViewModel<Router: RouterGraph>: ViewModel<Router, ServiceS
self.onPause()

await UIApplication.shared.openURLIfPossible(authorizationUrl) {
self.setErrorState {
self.setErrorState(.unableToOpenBrowser) {
self.setState { $0.copy(error: nil) }
}
}
} catch {
setErrorState {
setErrorState(.genericServiceErrorMessage) {
self.router.pop()
}
}
}
}

private func setErrorState(cancelAction: @escaping () -> ()) {
private func setErrorState(
_ desc: LocalizableKey,
cancelAction: @escaping () -> ()
) {
setState {
$0.copy(
isLoading: false,
error: ContentErrorView.Config(
title: .genericErrorMessage,
description: .genericErrorQtspNotFound,
description: desc,
cancelAction: cancelAction,
action: { Task { await self.initiate() } }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ class SignedDocumentViewModel<Router: RouterGraph>: ViewModel<Router, SignedDocu
.copy(error: nil)
}
} else {
setErrorState {
setErrorState(.genericErrorDocumentNotFound) {
self.onCancel()
}
}
} catch {
setErrorState {
setErrorState(.genericServiceErrorMessage) {
Task { await self.initiate() }
}
}
Expand All @@ -90,13 +90,16 @@ class SignedDocumentViewModel<Router: RouterGraph>: ViewModel<Router, SignedDocu
router.navigateTo(.viewDocument(true))
}

private func setErrorState(retryAction: @escaping () -> ()) {
private func setErrorState(
_ desc: LocalizableKey,
retryAction: @escaping () -> ()
) {
setState {
$0.copy(
isLoading: false,
error: ContentErrorView.Config(
title: .genericErrorMessage,
description: .genericErrorDocumentNotFound,
description: desc,
cancelAction: onCancel,
action: retryAction
)
Expand Down

0 comments on commit 52c7756

Please sign in to comment.