Skip to content

Commit 3575ac3

Browse files
committed
feat: add logging to postgrest
1 parent 593816b commit 3575ac3

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

packages/postgrest/lib/src/postgrest.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:http/http.dart';
2+
import 'package:logging/logging.dart';
23
import 'package:postgrest/postgrest.dart';
34
import 'package:postgrest/src/constants.dart';
45
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
@@ -11,6 +12,7 @@ class PostgrestClient {
1112
final Client? httpClient;
1213
final YAJsonIsolate _isolate;
1314
final bool _hasCustomIsolate;
15+
final _log = Logger('supabase.postgrest');
1416

1517
/// To create a [PostgrestClient], you need to provide an [url] endpoint.
1618
///
@@ -42,6 +44,7 @@ class PostgrestClient {
4244
}
4345

4446
PostgrestClient setAuth(String? token) {
47+
_log.fine("setAuth with: $token");
4548
if (token != null) {
4649
headers['Authorization'] = 'Bearer $token';
4750
} else {
@@ -95,6 +98,7 @@ class PostgrestClient {
9598
}
9699

97100
Future<void> dispose() async {
101+
_log.fine("dispose client");
98102
if (!_hasCustomIsolate) {
99103
return _isolate.dispose();
100104
}

packages/postgrest/lib/src/postgrest_builder.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:core';
66

77
import 'package:http/http.dart' as http;
88
import 'package:http/http.dart';
9+
import 'package:logging/logging.dart';
910
import 'package:meta/meta.dart';
1011
import 'package:postgrest/postgrest.dart';
1112
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
@@ -44,6 +45,7 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
4445
final Client? _httpClient;
4546
final YAJsonIsolate? _isolate;
4647
final CountOption? _count;
48+
final _log = Logger('supabase.postgrest');
4749

4850
PostgrestBuilder({
4951
required Uri url,
@@ -132,6 +134,8 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
132134
_headers['Content-Type'] = 'application/json';
133135
}
134136
final bodyStr = jsonEncode(_body);
137+
_log.finer("Request: $uppercaseMethod $_url");
138+
135139
if (uppercaseMethod == METHOD_GET) {
136140
response = await (_httpClient?.get ?? http.get)(
137141
_url,
@@ -203,14 +207,17 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
203207
// Workaround for https://github.com/supabase/supabase-flutter/issues/560
204208
if (_maybeSingle && method.toUpperCase() == 'GET' && body is List) {
205209
if (body.length > 1) {
206-
throw PostgrestException(
210+
final exception = PostgrestException(
207211
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
208212
code: '406',
209213
details:
210214
'Results contain ${body.length} rows, application/vnd.pgrst.object+json requires 1 row',
211215
hint: null,
212216
message: 'JSON object requested, multiple (or no) rows returned',
213217
);
218+
219+
_log.fine('$exception for request $_url');
220+
throw exception;
214221
} else if (body.length == 1) {
215222
body = body.first;
216223
} else {
@@ -286,6 +293,8 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
286293
);
287294
}
288295

296+
_log.fine('$error for request $_url');
297+
289298
throw error;
290299
}
291300
}

packages/postgrest/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies:
1212
http: '>=0.13.0 <2.0.0'
1313
yet_another_json_isolate: 2.0.2
1414
meta: ^1.9.1
15+
logging: ^1.2.0
1516

1617
dev_dependencies:
1718
collection: ^1.16.0

0 commit comments

Comments
 (0)