From ee140a3d4eb14dc7dd0ea14e2cbf0a989742cbb8 Mon Sep 17 00:00:00 2001 From: talwinder kaur Date: Thu, 7 Sep 2023 14:41:09 -0400 Subject: [PATCH] fix(app): Add exception handling for the QR code scanning process (#592) Signed-off-by: Talwinder kaur --- .../walletsdk/flutter/app/MainActivity.kt | 2 +- demo/app/ios/Runner/OpenID4CI.swift | 18 ++++++++++----- demo/app/ios/Runner/WalletSDK.swift | 5 +++-- demo/app/ios/Runner/flutterPlugin.swift | 22 +++++++++---------- .../handle_openid_issuance_flow.dart | 19 ++++++++++++++-- demo/app/lib/views/custom_error.dart | 5 +++-- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/demo/app/android/app/src/main/kotlin/walletsdk/flutter/app/MainActivity.kt b/demo/app/android/app/src/main/kotlin/walletsdk/flutter/app/MainActivity.kt index 5b477218..6793d781 100644 --- a/demo/app/android/app/src/main/kotlin/walletsdk/flutter/app/MainActivity.kt +++ b/demo/app/android/app/src/main/kotlin/walletsdk/flutter/app/MainActivity.kt @@ -77,7 +77,7 @@ class MainActivity : FlutterActivity() { val err = Walleterror.parse(e.message) result.error( "Exception", - "Error while authorizing the oidc vc flow", + "Error while initializing the interaction", "code: ${err.code}, error: ${err.category}, details: ${err.details}" ) diff --git a/demo/app/ios/Runner/OpenID4CI.swift b/demo/app/ios/Runner/OpenID4CI.swift index 714fc2af..bf4eb1c6 100644 --- a/demo/app/ios/Runner/OpenID4CI.swift +++ b/demo/app/ios/Runner/OpenID4CI.swift @@ -15,22 +15,30 @@ public class OpenID4CI { private var initiatedInteraction: Openid4ciIssuerInitiatedInteraction - init (requestURI: String, didResolver: ApiDIDResolverProtocol, crypto: ApiCryptoProtocol, activityLogger: ApiActivityLoggerProtocol) { + init (requestURI: String, didResolver: ApiDIDResolverProtocol, crypto: ApiCryptoProtocol, activityLogger: ApiActivityLoggerProtocol) throws { self.didResolver = didResolver self.crypto = crypto self.activityLogger = activityLogger let trace = OtelNewTrace(nil) - - let args = Openid4ciNewIssuerInitiatedInteractionArgs(requestURI, self.crypto, self.didResolver) + let args = Openid4ciNewIssuerInitiatedInteractionArgs(requestURI, crypto, didResolver) + let opts = Openid4ciNewInteractionOpts() opts!.setActivityLogger(activityLogger) opts!.add(trace!.traceHeader()) - self.initiatedInteraction = Openid4ciNewIssuerInitiatedInteraction(args, opts, nil)! + + var error: NSError? + let interaction = Openid4ciNewIssuerInitiatedInteraction(args, opts, &error) + if let actualError = error { + throw actualError + } + + self.initiatedInteraction = interaction! } + func checkFlow() throws -> String { if ((initiatedInteraction.authorizationCodeGrantTypeSupported())){ return "auth-code-flow" @@ -55,7 +63,7 @@ public class OpenID4CI { let authorizationLink = initiatedInteraction.createAuthorizationURL(clientID, redirectURI: redirectURI, opts: opts, error: &error) if let actualError = error { - print("error in authorizations", error!.localizedDescription) + print("error while creating authorization link", error!.localizedDescription) throw actualError } diff --git a/demo/app/ios/Runner/WalletSDK.swift b/demo/app/ios/Runner/WalletSDK.swift index d5bfa745..04cd83f1 100644 --- a/demo/app/ios/Runner/WalletSDK.swift +++ b/demo/app/ios/Runner/WalletSDK.swift @@ -49,8 +49,9 @@ class WalletSDK { } activityLogger = MemNewActivityLogger() - - return OpenID4CI(requestURI: requestURI, didResolver: didResolver, crypto: crypto, activityLogger: activityLogger! ) + + + return try OpenID4CI(requestURI: requestURI, didResolver: didResolver, crypto: crypto, activityLogger: activityLogger! ) } func createOpenID4CIWalletInitiatedInteraction(issuerURI: String) throws -> WalletInitiatedOpenID4CI { diff --git a/demo/app/ios/Runner/flutterPlugin.swift b/demo/app/ios/Runner/flutterPlugin.swift index 71de46b5..ae8c9537 100644 --- a/demo/app/ios/Runner/flutterPlugin.swift +++ b/demo/app/ios/Runner/flutterPlugin.swift @@ -320,10 +320,10 @@ public class SwiftWalletSDKPlugin: NSObject, FlutterPlugin { docResolution["did"] = doc.id_(nil) docResolution["didDoc"] = doc.content result(docResolution) - } catch { + } catch let error as NSError { result(FlutterError.init(code: "NATIVE_ERR", message: "error while creating did", - details: error)) + details: error.localizedDescription)) } } @@ -418,10 +418,10 @@ public class SwiftWalletSDKPlugin: NSObject, FlutterPlugin { result(flowTypeData) - } catch { - result(FlutterError.init(code: "NATIVE_ERR", + } catch let error as NSError { + result(FlutterError.init(code: "Exception", message: "error while initializing issuance flow", - details: error)) + details: error.localizedDescription)) } } @@ -470,10 +470,10 @@ public class SwiftWalletSDKPlugin: NSObject, FlutterPlugin { result(issuerMetaDataRespList) - } catch { + } catch let error as NSError { result(FlutterError.init(code: "NATIVE_ERR", message: "error while getting issuer meta data", - details: error)) + details: error.localizedDescription)) } } @@ -602,10 +602,10 @@ public class SwiftWalletSDKPlugin: NSObject, FlutterPlugin { let authorizationURL = try walletInitiatedOpenID4CI.createAuthorizationURLWalletInitiatedFlow(scopes: scopes, credentialFormat: credentialFormat, credentialTypes: credentialTypes, clientID: clientID, redirectURI: redirectURI, issuerURI: issuerURI) result(authorizationURL) - } catch { + } catch let error as NSError { result(FlutterError.init(code: "NATIVE_ERR", message: "error while creating authorization URL in wallet initiated issuance flow", - details: error)) + details: error.localizedDescription)) } } @@ -626,10 +626,10 @@ public class SwiftWalletSDKPlugin: NSObject, FlutterPlugin { result(verifierDisplayData) - } catch { + } catch let error as NSError { result(FlutterError.init(code: "NATIVE_ERR", message: "error while getting verifier display data", - details: error)) + details: error.localizedDescription)) } } diff --git a/demo/app/lib/scenarios/handle_openid_issuance_flow.dart b/demo/app/lib/scenarios/handle_openid_issuance_flow.dart index ff0f54d0..a857f08f 100644 --- a/demo/app/lib/scenarios/handle_openid_issuance_flow.dart +++ b/demo/app/lib/scenarios/handle_openid_issuance_flow.dart @@ -13,6 +13,8 @@ import 'package:flutter/services.dart'; import 'package:app/models/credential_offer.dart'; import 'package:http/http.dart' as http; +import '../views/custom_error.dart'; + void handleOpenIDIssuanceFlow(BuildContext context, String qrCodeURL) async { var WalletSDKPlugin = WalletSDK(); var authCodeArgs; @@ -31,8 +33,21 @@ void handleOpenIDIssuanceFlow(BuildContext context, String qrCodeURL) async { }; } } - - var flowTypeData = await WalletSDKPlugin.initialize(qrCodeURL, authCodeArgs); + log("qr code url - $qrCodeURL"); + var flowTypeData; + try { + flowTypeData = await WalletSDKPlugin.initialize(qrCodeURL, authCodeArgs); + } catch (error) { + var errString = error.toString().replaceAll(r'\', ''); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => CustomError( + titleBarString: "QR Code Scanned", + requestErrorTitleMsg: "error while intializing the interaction", + requestErrorSubTitleMsg: "${errString}"))); + return; + } var flowTypeDataEncoded = json.encode(flowTypeData); Map responseJson = json.decode(flowTypeDataEncoded); var authorizeResultPinRequired = responseJson["pinRequired"]; diff --git a/demo/app/lib/views/custom_error.dart b/demo/app/lib/views/custom_error.dart index 2ccc13ac..719dd244 100644 --- a/demo/app/lib/views/custom_error.dart +++ b/demo/app/lib/views/custom_error.dart @@ -5,10 +5,11 @@ import 'package:flutter/material.dart'; import 'package:app/widgets/common_title_appbar.dart'; class CustomError extends StatefulWidget { + String? titleBarString; final String requestErrorTitleMsg; final String requestErrorSubTitleMsg ; - const CustomError({super.key, required this.requestErrorTitleMsg, required this.requestErrorSubTitleMsg}); + CustomError({super.key, required this.requestErrorTitleMsg, required this.requestErrorSubTitleMsg, this.titleBarString}); @override State createState() => CustomErrorPage(); @@ -18,7 +19,7 @@ class CustomErrorPage extends State { @override Widget build(BuildContext context) { return Scaffold( - appBar: const CustomTitleAppBar(pageTitle: '', addCloseIcon: true, height: 60), + appBar: CustomTitleAppBar(pageTitle: widget.titleBarString, addCloseIcon: true, height: 60), body: Center( child: Container ( padding: const EdgeInsets.all(12),