Skip to content

Commit 4639087

Browse files
committed
Replace AuthException with AuthSessionMissingException
1 parent d4566e8 commit 4639087

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

packages/gotrue/lib/src/gotrue_client.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ class GoTrueClient {
607607
/// If the current session's refresh token is invalid, an error will be thrown.
608608
Future<AuthResponse> refreshSession([String? refreshToken]) async {
609609
if (currentSession?.accessToken == null) {
610-
throw AuthException('Not logged in.');
610+
throw AuthSessionMissingException();
611611
}
612612

613613
final currentSessionRefreshToken =
@@ -626,7 +626,7 @@ class GoTrueClient {
626626
Future<void> reauthenticate() async {
627627
final session = currentSession;
628628
if (session == null) {
629-
throw AuthException('Not logged in.');
629+
throw AuthSessionMissingException();
630630
}
631631

632632
final options =
@@ -691,7 +691,7 @@ class GoTrueClient {
691691
/// Gets the current user details from current session or custom [jwt]
692692
Future<UserResponse> getUser([String? jwt]) async {
693693
if (jwt == null && currentSession?.accessToken == null) {
694-
throw AuthException('Cannot get user: no current session.');
694+
throw AuthSessionMissingException();
695695
}
696696
final options = GotrueRequestOptions(
697697
headers: _headers,
@@ -712,7 +712,7 @@ class GoTrueClient {
712712
}) async {
713713
final accessToken = currentSession?.accessToken;
714714
if (accessToken == null) {
715-
throw AuthException('Not logged in.');
715+
throw AuthSessionMissingException();
716716
}
717717

718718
final body = attributes.toJson();
@@ -736,7 +736,7 @@ class GoTrueClient {
736736
/// Sets the session data from refresh_token and returns the current session.
737737
Future<AuthResponse> setSession(String refreshToken) async {
738738
if (refreshToken.isEmpty) {
739-
throw AuthException('No current session.');
739+
throw AuthSessionMissingException('Refresh token cannot be empty');
740740
}
741741
return await _callRefreshToken(refreshToken);
742742
}

packages/gotrue/lib/src/types/auth_exception.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class AuthException implements Exception {
2727
}
2828

2929
class AuthPKCEGrantCodeExchangeError extends AuthException {
30-
AuthPKCEGrantCodeExchangeError(String message) : super(message);
30+
AuthPKCEGrantCodeExchangeError(super.message);
3131
}
3232

3333
class AuthSessionMissingException extends AuthException {
34-
AuthSessionMissingException()
34+
AuthSessionMissingException([String? message])
3535
: super(
36-
'Auth session missing!',
36+
message ?? 'Auth session missing!',
3737
statusCode: '400',
3838
errorCode: AuthErrorCode.sessionNotFound.code,
3939
);
@@ -48,8 +48,7 @@ class AuthRetryableFetchException extends AuthException {
4848
}
4949

5050
class AuthApiException extends AuthException {
51-
AuthApiException(String message, {String? statusCode, String? errorCode})
52-
: super(message, statusCode: statusCode, errorCode: errorCode);
51+
AuthApiException(super.message, {super.statusCode, super.errorCode});
5352
}
5453

5554
class AuthUnknownException extends AuthException {

packages/gotrue/test/client_test.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ void main() {
111111
} on AuthException catch (error) {
112112
expect(error, isA<AuthWeakPasswordException>());
113113
expect(error.errorCode, AuthErrorCode.weakPassword.code);
114+
} catch (error) {
115+
fail('signUp threw ${error.runtimeType} instead of AuthException');
114116
}
115117
});
116118

@@ -246,7 +248,7 @@ void main() {
246248
expect(user.appMetadata['provider'], 'email');
247249
});
248250

249-
test('signInWithPassword() with phone', () async {
251+
test('signInWithPassword() with phone', () async {
250252
final response =
251253
await client.signInWithPassword(phone: phone1, password: password);
252254
final data = response.session;
@@ -278,6 +280,17 @@ void main() {
278280
expect(newClient.currentSession?.accessToken ?? '', isNotEmpty);
279281
});
280282

283+
test(
284+
'Set session with an empty refresh token throws AuthSessionMissingException',
285+
() async {
286+
try {
287+
await client.setSession('');
288+
fail('setSession did not throw');
289+
} catch (error) {
290+
expect(error, isA<AuthSessionMissingException>());
291+
}
292+
});
293+
281294
test('Update user', () async {
282295
await client.signInWithPassword(email: email1, password: password);
283296

@@ -298,11 +311,11 @@ void main() {
298311
expect(user?.userMetadata?['arabic'], 'عربى');
299312
});
300313

301-
test('Update user on with the same password throws AuthException',
302-
() async {
314+
test('Update user with the same password throws AuthException', () async {
303315
await client.signInWithPassword(email: email1, password: password);
304316
try {
305317
await client.updateUser(UserAttributes(password: password));
318+
fail('updateUser did not throw');
306319
} on AuthException catch (error) {
307320
expect(error.errorCode, AuthErrorCode.samePassword.code);
308321
}

0 commit comments

Comments
 (0)