Skip to content

Commit

Permalink
fix(auth): Transform network exceptions (#2967)
Browse files Browse the repository at this point in the history
Transform network exceptions in the SDK layer so that individual plugin methods do not need to be wrapped.
  • Loading branch information
dnys1 authored May 3, 2023
1 parent 81409a8 commit 460b5dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ class ResourceConflictException extends core.AuthServiceException {
@internal
Object transformSdkException(Object e) {
if (e is! SmithyException) {
return e;
return e is Exception ? core.AuthException.fromException(e) : e;
}
final message = e.message ?? e.toString();
final shapeId = e.shapeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/sdk_exception.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:test/test.dart';

void main() {
group('LambdaException', () {
const error = 'something bad happened';
const message = 'PreConfirmation failed with error $error.';
group('SDK exception', () {
group('LambdaException', () {
const error = 'something bad happened';
const message = 'PreConfirmation failed with error $error.';

test('matches string', () {
expect(LambdaException.isLambdaException(message), isTrue);
test('matches string', () {
expect(LambdaException.isLambdaException(message), isTrue);

final exception = LambdaException(message);
expect(exception.message, error);
expect(exception.lambdaName, 'PreConfirmation');
});
final exception = LambdaException(message);
expect(exception.message, error);
expect(exception.lambdaName, 'PreConfirmation');
});

test('matches exception', () {
const wrapped = UserNotConfirmedException(message);
expect(LambdaException.isLambdaException(wrapped.toString()), isTrue);

test('matches exception', () {
const wrapped = UserNotConfirmedException(message);
expect(LambdaException.isLambdaException(wrapped.toString()), isTrue);
final exception = LambdaException(wrapped.toString());
expect(exception.message, error);
expect(exception.lambdaName, 'PreConfirmation');
});
});

final exception = LambdaException(wrapped.toString());
expect(exception.message, error);
expect(exception.lambdaName, 'PreConfirmation');
test('transforms network exceptions', () {
final networkException = AWSHttpException(
AWSHttpRequest.get(Uri.parse('https://example.com')),
);
expect(
transformSdkException(networkException),
isA<NetworkException>(),
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class $shapeName extends core.AuthServiceException {
@internal
Object transformSdkException(Object e) {
if (e is! SmithyException) {
return e;
return e is Exception ? core.AuthException.fromException(e) : e;
}
final message = e.message ?? e.toString();
final shapeId = e.shapeId;
Expand Down

0 comments on commit 460b5dc

Please sign in to comment.