mapSupabase<T> function
Runs body and maps vendor exceptions to AppFailure.
Wrap every public source method — unlike Dio, Supabase has no interceptor that cannot be forgotten.
Implementation
Future<T> mapSupabase<T>(Future<T> Function() body) async {
try {
return await body();
} on AppFailure {
rethrow;
} on PostgrestException catch (e, s) {
throw switch (e.code) {
'23505' => ValidationFailure({'_': 'duplicate'}, cause: e),
'23503' => ConflictFailure(code: e.code, cause: e),
'42501' => PermissionFailure('row', cause: e),
'PGRST116' => NotFoundFailure('row', cause: e),
_ => ServerFailure(code: e.code, cause: e, trace: s),
};
} on AuthException catch (e) {
throw AuthFailure(authReasonFrom(e), cause: e);
} on StorageException catch (e, s) {
throw StorageFailure(cause: e, trace: s);
} catch (e, s) {
if (_isNetworkException(e)) {
throw NetworkFailure(cause: e, trace: s);
}
throw UnknownFailure(cause: e, trace: s);
}
}