Skip to content
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
9 changes: 8 additions & 1 deletion packages/storage_client/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,21 @@ class StorageApiException extends StorageException with SupabaseApiException {
/// API documents, since a proxy or gateway in front of it can answer with a
/// shape of its own, so every field is read defensively. [statusCode] is used
/// when the body reports none.
///
/// [SupabaseException.errorCode] is read from the body's `code`, the
/// documented storage error code such as `NoSuchKey` or `AccessDenied`. The
/// body's `error` is the fallback for servers old enough not to send `code`,
/// and carries a plain sentence as often as an identifier.
///
/// See https://supabase.com/docs/guides/storage/debugging/error-codes
factory StorageApiException.fromJson(
Map<String, dynamic> json,
int statusCode,
) {
final message = json['message'];
return StorageApiException(
message is String ? message : json.toString(),
errorCode: json['error']?.toString(),
errorCode: (json['code'] ?? json['error'])?.toString(),
statusCode: int.tryParse('${json['statusCode']}') ?? statusCode,
);
}
Expand Down
43 changes: 43 additions & 0 deletions packages/storage_client/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,49 @@ void main() {
);
});

test('surfaces the service error code from the response body', () async {
addTearDown(() => customHttpClient.statusCode = 201);
customHttpClient.statusCode = 404;
// Mirrors a real storage payload, which sends statusCode, error and code.
customHttpClient.response = {
'statusCode': '404',
'error': 'not_found',
'code': 'NoSuchKey',
'message': 'Object not found',
};

await expectLater(
client.from('public_bucket').download('missing.txt'),
throwsA(
isA<StorageApiException>()
.having((e) => e.errorCode, 'errorCode', 'NoSuchKey')
.having((e) => e.statusCode, 'statusCode', 404)
.having((e) => e.message, 'message', 'Object not found'),
),
);
});

test('falls back to error when the response body has no code', () async {
addTearDown(() => customHttpClient.statusCode = 201);
customHttpClient.statusCode = 404;
customHttpClient.response = {
'statusCode': '404',
'error': 'not_found',
'message': 'Object not found',
};

await expectLater(
client.from('public_bucket').download('missing.txt'),
throwsA(
isA<StorageException>().having(
(e) => e.errorCode,
'errorCode',
'not_found',
),
),
);
});

test('should get public URL of a path', () {
final response = client.from('files').getPublicUrl('b.txt');
expect(response, '$objectUrl/public/files/b.txt');
Expand Down
28 changes: 28 additions & 0 deletions packages/storage_client/test/types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,34 @@ void main() {
expect(exception.statusCode, 502);
});

test('fromJson reads the service error code apart from statusCode', () {
final exception = StorageApiException.fromJson({
'message': 'Object not found',
'statusCode': '404',
'error': 'not_found',
'code': 'NoSuchKey',
}, 404);
expect(exception.errorCode, 'NoSuchKey');
expect(exception.statusCode, 404);
});

test('fromJson falls back to error when the body carries no code', () {
final exception = StorageApiException.fromJson({
'message': 'Object not found',
'statusCode': '404',
'error': 'not_found',
}, 404);
expect(exception.errorCode, 'not_found');
});

test('fromJson leaves the error code null when the body has neither', () {
final exception = StorageApiException.fromJson({
'message': 'not found',
'statusCode': '404',
}, 404);
expect(exception.errorCode, isNull);
});

test(
'fromJson falls back to the response status code and stringified body',
() {
Expand Down
2 changes: 1 addition & 1 deletion packages/supabase_common/lib/src/supabase_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class SupabaseException implements Exception {
final String message;

/// Identifier for the error, for example `weak_password` (auth), `PGRST116`
/// (postgrest) or `not_found` (storage).
/// (postgrest) or `NoSuchKey` (storage).
///
/// `null` when neither the service nor the client named the failure.
final String? errorCode;
Expand Down