diff --git a/packages/storage_client/lib/src/types.dart b/packages/storage_client/lib/src/types.dart index 09978a5d5..a73c26af7 100644 --- a/packages/storage_client/lib/src/types.dart +++ b/packages/storage_client/lib/src/types.dart @@ -611,6 +611,13 @@ 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 json, int statusCode, @@ -618,7 +625,7 @@ class StorageApiException extends StorageException with SupabaseApiException { 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, ); } diff --git a/packages/storage_client/test/basic_test.dart b/packages/storage_client/test/basic_test.dart index 8a87053e8..7ddb13df7 100644 --- a/packages/storage_client/test/basic_test.dart +++ b/packages/storage_client/test/basic_test.dart @@ -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() + .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().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'); diff --git a/packages/storage_client/test/types_test.dart b/packages/storage_client/test/types_test.dart index 99b97478c..c2badb42c 100644 --- a/packages/storage_client/test/types_test.dart +++ b/packages/storage_client/test/types_test.dart @@ -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', () { diff --git a/packages/supabase_common/lib/src/supabase_exception.dart b/packages/supabase_common/lib/src/supabase_exception.dart index 474006b45..c8a3ec860 100644 --- a/packages/supabase_common/lib/src/supabase_exception.dart +++ b/packages/supabase_common/lib/src/supabase_exception.dart @@ -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;