Skip to content

fix(gotrue): Handle empty error response #1143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions packages/gotrue/lib/src/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,38 @@ class GotrueFetch {
if (error is! Response) {
throw AuthRetryableFetchException(message: error.toString());
}
final response = error;

// If the status is 500 or above, it's likely a server error,
// and can be retried.
if (error.statusCode >= 500) {
if (response.statusCode >= 500) {
throw AuthRetryableFetchException(
message: error.body,
statusCode: error.statusCode.toString(),
message: response.body,
statusCode: response.statusCode.toString(),
);
}

final dynamic data;

// Catch this case as trying to decode it will throw a misleading [FormatException]
if (response.body.isEmpty) {
throw AuthUnknownException(
message:
'Received an empty response with status code ${response.statusCode}',
originalError: response,
);
}
try {
data = jsonDecode(error.body);
data = jsonDecode(response.body);
} catch (error) {
throw AuthUnknownException(
message: error.toString(), originalError: error);
message: 'Failed to decode error response',
originalError: error,
);
}

String? errorCode;

final responseApiVersion = ApiVersion.fromResponse(error);
final responseApiVersion = ApiVersion.fromResponse(response);

if (responseApiVersion?.isSameOrAfter(ApiVersions.v20240101) ?? false) {
errorCode = _getErrorCode(data, 'code');
Expand All @@ -85,21 +96,21 @@ class GotrueFetch {
.isEmpty) {
throw AuthWeakPasswordException(
message: _getErrorMessage(data),
statusCode: error.statusCode.toString(),
statusCode: response.statusCode.toString(),
reasons: List<String>.from(data['weak_password']['reasons']),
);
}
} else if (errorCode == ErrorCode.weakPassword.code) {
throw AuthWeakPasswordException(
message: _getErrorMessage(data),
statusCode: error.statusCode.toString(),
statusCode: response.statusCode.toString(),
reasons: List<String>.from(data['weak_password']?['reasons'] ?? []),
);
}

throw AuthApiException(
_getErrorMessage(data),
statusCode: error.statusCode.toString(),
statusCode: response.statusCode.toString(),
code: errorCode,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,8 @@ class GoTrueClient {

return AuthResponse(session: session);
}
} catch (error) {
notifyException(error);
} catch (error, stackTrace) {
notifyException(error, stackTrace);
rethrow;
}
}
Expand Down
32 changes: 30 additions & 2 deletions packages/gotrue/lib/src/types/auth_exception.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:gotrue/src/types/error_code.dart';
import 'package:http/http.dart' as http;

class AuthException implements Exception {
/// Human readable error message associated with the error.
Expand Down Expand Up @@ -46,24 +47,47 @@ class AuthSessionMissingException extends AuthException {
message ?? 'Auth session missing!',
statusCode: '400',
);
@override
String toString() =>
'AuthSessionMissingException(message: $message, statusCode: $statusCode)';
}

class AuthRetryableFetchException extends AuthException {
AuthRetryableFetchException({
String message = 'AuthRetryableFetchException',
super.statusCode,
}) : super(message);

@override
String toString() =>
'AuthRetryableFetchException(message: $message, statusCode: $statusCode)';
}

class AuthApiException extends AuthException {
AuthApiException(super.message, {super.statusCode, super.code});

@override
String toString() =>
'AuthApiException(message: $message, statusCode: $statusCode, code: $code)';
}

class AuthUnknownException extends AuthException {
/// May contain a non 2xx [http.Response] object or the original thrown error.
final Object originalError;

AuthUnknownException({required String message, required this.originalError})
: super(message);
AuthUnknownException({
required String message,
required this.originalError,
}) : super(
message,
statusCode: originalError is http.Response
? originalError.statusCode.toString()
: null,
);

@override
String toString() =>
'AuthUnknownException(message: $message, originalError: $originalError, statusCode: $statusCode)';
}

class AuthWeakPasswordException extends AuthException {
Expand All @@ -74,4 +98,8 @@ class AuthWeakPasswordException extends AuthException {
required super.statusCode,
required this.reasons,
}) : super(message, code: ErrorCode.weakPassword.code);

@override
String toString() =>
'AuthWeakPasswordException(message: $message, statusCode: $statusCode, reasons: $reasons)';
}
2 changes: 1 addition & 1 deletion packages/gotrue/lib/src/types/auth_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class AuthState {

@override
String toString() {
return 'AuthState{event: $event, session: $session, fromBroadcast: $fromBroadcast}';
return 'AuthState(event: $event, session: $session, fromBroadcast: $fromBroadcast)';
}
}
7 changes: 5 additions & 2 deletions packages/gotrue/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,11 @@ void main() {
try {
await client.signInWithPassword(email: email1, password: password);
} catch (error) {
expect(error, isA<AuthException>());
expect((error as AuthException).statusCode, '420');
expect(error, isA<AuthUnknownException>());
error as AuthUnknownException;
expect(error.statusCode, '420');
expect(error.originalError, isA<http.Response>());
expect(error.message, contains('empty response'));
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/gotrue/test/custom_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomHttpClient extends BaseClient {
Future<StreamedResponse> send(BaseRequest request) async {
//Return custom status code to check for usage of this client.
return StreamedResponse(
request.finalize(),
Stream.empty(),
420,
request: request,
);
Expand Down
Loading