@@ -45,27 +45,38 @@ class GotrueFetch {
4545 if (error is ! Response ) {
4646 throw AuthRetryableFetchException (message: error.toString ());
4747 }
48+ final response = error;
4849
4950 // If the status is 500 or above, it's likely a server error,
5051 // and can be retried.
51- if (error .statusCode >= 500 ) {
52+ if (response .statusCode >= 500 ) {
5253 throw AuthRetryableFetchException (
53- message: error .body,
54- statusCode: error .statusCode.toString (),
54+ message: response .body,
55+ statusCode: response .statusCode.toString (),
5556 );
5657 }
5758
5859 final dynamic data;
60+
61+ // Catch this case as trying to decode it will throw a misleading [FormatException]
62+ if (response.body.isEmpty) {
63+ throw AuthUnknownException (
64+ message:
65+ 'Received an empty response with status code ${response .statusCode }' ,
66+ originalError: response,
67+ );
68+ }
5969 try {
60- data = jsonDecode (error .body);
70+ data = jsonDecode (response .body);
6171 } catch (error) {
6272 throw AuthUnknownException (
63- message: error.toString (), originalError: error);
73+ message: 'Failed to decode error response' ,
74+ originalError: error,
75+ );
6476 }
65-
6677 String ? errorCode;
6778
68- final responseApiVersion = ApiVersion .fromResponse (error );
79+ final responseApiVersion = ApiVersion .fromResponse (response );
6980
7081 if (responseApiVersion? .isSameOrAfter (ApiVersions .v20240101) ?? false ) {
7182 errorCode = _getErrorCode (data, 'code' );
@@ -85,21 +96,21 @@ class GotrueFetch {
8596 .isEmpty) {
8697 throw AuthWeakPasswordException (
8798 message: _getErrorMessage (data),
88- statusCode: error .statusCode.toString (),
99+ statusCode: response .statusCode.toString (),
89100 reasons: List <String >.from (data['weak_password' ]['reasons' ]),
90101 );
91102 }
92103 } else if (errorCode == ErrorCode .weakPassword.code) {
93104 throw AuthWeakPasswordException (
94105 message: _getErrorMessage (data),
95- statusCode: error .statusCode.toString (),
106+ statusCode: response .statusCode.toString (),
96107 reasons: List <String >.from (data['weak_password' ]? ['reasons' ] ?? []),
97108 );
98109 }
99110
100111 throw AuthApiException (
101112 _getErrorMessage (data),
102- statusCode: error .statusCode.toString (),
113+ statusCode: response .statusCode.toString (),
103114 code: errorCode,
104115 );
105116 }
0 commit comments