@@ -45,27 +45,38 @@ class GotrueFetch {
45
45
if (error is ! Response ) {
46
46
throw AuthRetryableFetchException (message: error.toString ());
47
47
}
48
+ final response = error;
48
49
49
50
// If the status is 500 or above, it's likely a server error,
50
51
// and can be retried.
51
- if (error .statusCode >= 500 ) {
52
+ if (response .statusCode >= 500 ) {
52
53
throw AuthRetryableFetchException (
53
- message: error .body,
54
- statusCode: error .statusCode.toString (),
54
+ message: response .body,
55
+ statusCode: response .statusCode.toString (),
55
56
);
56
57
}
57
58
58
59
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
+ }
59
69
try {
60
- data = jsonDecode (error .body);
70
+ data = jsonDecode (response .body);
61
71
} catch (error) {
62
72
throw AuthUnknownException (
63
- message: error.toString (), originalError: error);
73
+ message: 'Failed to decode error response' ,
74
+ originalError: error,
75
+ );
64
76
}
65
-
66
77
String ? errorCode;
67
78
68
- final responseApiVersion = ApiVersion .fromResponse (error );
79
+ final responseApiVersion = ApiVersion .fromResponse (response );
69
80
70
81
if (responseApiVersion? .isSameOrAfter (ApiVersions .v20240101) ?? false ) {
71
82
errorCode = _getErrorCode (data, 'code' );
@@ -85,21 +96,21 @@ class GotrueFetch {
85
96
.isEmpty) {
86
97
throw AuthWeakPasswordException (
87
98
message: _getErrorMessage (data),
88
- statusCode: error .statusCode.toString (),
99
+ statusCode: response .statusCode.toString (),
89
100
reasons: List <String >.from (data['weak_password' ]['reasons' ]),
90
101
);
91
102
}
92
103
} else if (errorCode == ErrorCode .weakPassword.code) {
93
104
throw AuthWeakPasswordException (
94
105
message: _getErrorMessage (data),
95
- statusCode: error .statusCode.toString (),
106
+ statusCode: response .statusCode.toString (),
96
107
reasons: List <String >.from (data['weak_password' ]? ['reasons' ] ?? []),
97
108
);
98
109
}
99
110
100
111
throw AuthApiException (
101
112
_getErrorMessage (data),
102
- statusCode: error .statusCode.toString (),
113
+ statusCode: response .statusCode.toString (),
103
114
code: errorCode,
104
115
);
105
116
}
0 commit comments