Skip to content

Commit

Permalink
Optimise error handling #2606
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Apr 19, 2024
1 parent 7c83872 commit c343a2f
Show file tree
Hide file tree
Showing 26 changed files with 320 additions and 81 deletions.
14 changes: 12 additions & 2 deletions lib/app/shared/enum/type/blockchain_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ extension BlockchainTypeX on BlockchainType {
String name = '';
switch (this) {
case BlockchainType.tezos:
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Chain is not supported for tezos.',
},
);

case BlockchainType.ethereum:
name = '1';
Expand All @@ -87,7 +92,12 @@ extension BlockchainTypeX on BlockchainType {
int get chainId {
switch (this) {
case BlockchainType.tezos:
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Chain is not supported for tezos.',
},
);

case BlockchainType.ethereum:
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,12 @@ extension CredentialSubjectTypeExtension on CredentialSubjectType {
} else if (this == CredentialSubjectType.ageRange) {
return Urls.ageRangeAIValidationUrl;
} else {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Url is not specified for $name.',
},
);
}
}

Expand Down
119 changes: 112 additions & 7 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ String getIssuersName(String constraints) {
BlockchainType getBlockchainType(AccountType accountType) {
switch (accountType) {
case AccountType.ssi:
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Invalid request.',
},
);
case AccountType.tezos:
return BlockchainType.tezos;
case AccountType.ethereum:
Expand Down Expand Up @@ -322,7 +327,12 @@ Future<String> getPrivateKey({
.get(SecureStorageKeys.walletAttestationData);

if (walletAttestationData == null) {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'The wallet attestation data has some issue.',
},
);
}

final p256KeyForWallet =
Expand Down Expand Up @@ -535,7 +545,12 @@ Future<(String, String)> getDidAndKid({
.get(SecureStorageKeys.walletAttestationData);

if (walletAttestationData == null) {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'The wallet attestation data has some issue.',
},
);
}

final walletAttestationDataPayload =
Expand Down Expand Up @@ -1005,7 +1020,13 @@ String getCredentialData(dynamic credential) {
.toList();
cred = credentialSupported.last;
} else {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description':
'The format of credentail should be either String or Map.',
},
);
}

return cred;
Expand All @@ -1030,7 +1051,14 @@ Future<String> getHost({
scannedResponse: uri.toString(),
dioClient: client,
);
if (credentialOfferJson == null) throw Exception();
if (credentialOfferJson == null) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'The credential offer is required.',
},
);
}

return Uri.parse(
credentialOfferJson['credential_issuer'].toString(),
Expand Down Expand Up @@ -1097,20 +1125,97 @@ MessageHandler getMessageHandler(dynamic e) {
);
} else {
final stringException = e.toString().replaceAll('Exception: ', '');
if (stringException == 'CREDENTIAL_SUPPORT_DATA_ERROR') {
if (stringException.contains('CREDENTIAL_SUPPORT_DATA_ERROR')) {
return ResponseMessage(
data: {
'error': 'unsupported_credential_format',
'error_description': 'The credential support format has some issues.',
},
);
} else if (stringException == 'AUTHORIZATION_DETAIL_ERROR') {
} else if (stringException.contains('AUTHORIZATION_DETAIL_ERROR')) {
return ResponseMessage(
data: {
'error': 'unsupported_format',
'error_description': 'Invalid token response format.',
},
);
} else if (stringException.contains('INVALID_TOKEN')) {
return ResponseMessage(
data: {
'error': 'invalid_format',
'error_description': 'Failed to extract header from jwt.',
},
);
} else if (stringException.contains('INVALID_TOKEN')) {
return ResponseMessage(
data: {
'error': 'invalid_format',
'error_description': 'Failed to extract payload from jwt.',
},
);
} else if (stringException.contains('SSI_ISSUE')) {
return ResponseMessage(
data: {
'error': 'invalid_format',
'error_description': 'SSI does not support this process.',
},
);
} else if (stringException.contains('OPENID-CONFIGURATION-ISSUE')) {
return ResponseMessage(
data: {
'error': 'unsupported_format',
'error_description': 'Openid configuration response issue.',
},
);
} else if (stringException.contains('NOT_A_VALID_OPENID_URL')) {
return ResponseMessage(
data: {
'error': 'unsupported_format',
'error_description': 'Not a valid openid url to initiate issuance.',
},
);
} else if (stringException.contains('JWKS_URI_IS_NULL')) {
return ResponseMessage(
data: {
'error': 'unsupported_format',
'error_description': 'The jwks_uri is null.',
},
);
} else if (stringException.contains('Issue while getting')) {
return ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': stringException,
},
);
} else if (stringException.contains('SECURE_STORAGE_ISSUE')) {
return ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Secure Storage issue with this device',
},
);
} else if (stringException.contains('ISSUE_WHILE_ADDING_IDENTITY')) {
return ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Issue while adding identity.',
},
);
} else if (stringException.contains('ISSUE_WHILE_GETTING_CLAIMS')) {
return ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Issue while getting claims.',
},
);
} else if (stringException.contains('ISSUE_WHILE_RESTORING_CLAIMS')) {
return ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Issue while restoring claims.',
},
);
} else {
return ResponseMessage(
message:
Expand Down
15 changes: 13 additions & 2 deletions lib/app/shared/launch_url/launch_url.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:altme/app/app.dart';
import 'package:url_launcher/url_launcher.dart';

class LaunchUrl {
Expand All @@ -7,7 +8,12 @@ class LaunchUrl {
}) async {
await canLaunchUrl(Uri.parse(url))
? await launchUrl(Uri.parse(url), mode: launchMode)
: throw Exception('Could not launch $url');
: throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description': 'Could not launch $url',
},
);
}

static Future<void> launchUri(
Expand All @@ -16,6 +22,11 @@ class LaunchUrl {
}) async {
await canLaunchUrl(uri)
? await launchUrl(uri, mode: launchMode)
: throw Exception('Could not launch $uri');
: throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description': 'Could not launch $uri',
},
);
}
}
17 changes: 15 additions & 2 deletions lib/credentials/cubit/credentials_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,15 @@ class CredentialsCubit extends Cubit<CredentialsState> {
final supportAssociatedCredential =
supportCryptoCredential(profileCubit.state.model);

if (!supportAssociatedCredential) throw Exception();
if (!supportAssociatedCredential) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description':
'The crypto associated credential is not supported.',
},
);
}

final didKeyType = profileCubit.state.model.profileSetting
.selfSovereignIdentityOptions.customOidc4vcProfile.defaultDid;
Expand Down Expand Up @@ -601,7 +609,12 @@ class CredentialsCubit extends Cubit<CredentialsState> {
final did = oldCredential.credentialPreview.credentialSubjectModel.id;

if (did == null) {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'DID is required.',
},
);
}

final didKeyType = profileCubit.state.model.profileSetting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ class _CongratulationsAccountCreationViewState
String message = '';
switch (widget.accountType) {
case AccountType.ssi:
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Invalid request.',
},
);
case AccountType.tezos:
message = l10n.tezosAccountCreationCongratulations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ class ImportAccountCubit extends Cubit<ImportAccountState> {
final String? mnemonicOrKey = await getSecureStorage
.get(SecureStorageKeys.importAccountStep2Mnemonics);

if (mnemonicOrKey == null) throw Exception();
if (mnemonicOrKey == null) {
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Please provide the mnemonics or private key.',
},
);
}

await walletCubit.createCryptoWallet(
accountName: accountName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,12 @@ class OperationCubit extends Cubit<OperationState> {

switch (transactionAccountData.blockchainType) {
case BlockchainType.tezos:
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Tezos does not support rpcNodeUrl.',
},
);
case BlockchainType.ethereum:
rpcUrl = await web3RpcMainnetInfuraURL();
case BlockchainType.fantom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,24 @@ class CredentialManifestPickCubit extends Cubit<CredentialManifestPickState> {
} else if (atLeast != null) {
isButtonEnabled = selected.length >= atLeast;
} else {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description':
'The count or min parameter should be provided in the '
'submissionRequirements of presentation_definition.',
},
);
}
} else {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description':
'The submissionRequirements should be provided in the '
'presentation_definition.',
},
);
}
} else {
/// normal case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ class SelectiveDisclosureCubit extends Cubit<SelectiveDisclosureState> {
}

if (index == null) {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Issue with the dislosuer of jwt.',
},
);
}

final bool isSelected = state.selectedSDIndexInJWT.contains(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ class _SelectiveDisclosurePickViewState
qrCodeScanCubit: context.read<QRCodeScanCubit>(),
);
} else {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_request',
'error_description': 'Issue with the disclosure encryption of jwt.',
},
);
}
}
}
7 changes: 6 additions & 1 deletion lib/dashboard/profile/models/profile_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,12 @@ class CustomOidc4VcProfile extends Equatable {
} else if (value == '12' || value == '13') {
return OIDC4VCIDraftType.draft13;
} else {
throw Exception();
throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description': 'Error with oidc4vc draft type.',
},
);
}
}

Expand Down
Loading

0 comments on commit c343a2f

Please sign in to comment.