Skip to content

Commit

Permalink
feat: add logging to postgrest
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 26, 2024
1 parent 593816b commit 3575ac3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/postgrest/lib/src/postgrest.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:http/http.dart';
import 'package:logging/logging.dart';
import 'package:postgrest/postgrest.dart';
import 'package:postgrest/src/constants.dart';
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
Expand All @@ -11,6 +12,7 @@ class PostgrestClient {
final Client? httpClient;
final YAJsonIsolate _isolate;
final bool _hasCustomIsolate;
final _log = Logger('supabase.postgrest');

/// To create a [PostgrestClient], you need to provide an [url] endpoint.
///
Expand Down Expand Up @@ -42,6 +44,7 @@ class PostgrestClient {
}

PostgrestClient setAuth(String? token) {
_log.fine("setAuth with: $token");
if (token != null) {
headers['Authorization'] = 'Bearer $token';
} else {
Expand Down Expand Up @@ -95,6 +98,7 @@ class PostgrestClient {
}

Future<void> dispose() async {
_log.fine("dispose client");
if (!_hasCustomIsolate) {
return _isolate.dispose();
}
Expand Down
11 changes: 10 additions & 1 deletion packages/postgrest/lib/src/postgrest_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:core';

import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:postgrest/postgrest.dart';
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
Expand Down Expand Up @@ -44,6 +45,7 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
final Client? _httpClient;
final YAJsonIsolate? _isolate;
final CountOption? _count;
final _log = Logger('supabase.postgrest');

PostgrestBuilder({
required Uri url,
Expand Down Expand Up @@ -132,6 +134,8 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
_headers['Content-Type'] = 'application/json';
}
final bodyStr = jsonEncode(_body);
_log.finer("Request: $uppercaseMethod $_url");

if (uppercaseMethod == METHOD_GET) {
response = await (_httpClient?.get ?? http.get)(
_url,
Expand Down Expand Up @@ -203,14 +207,17 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
// Workaround for https://github.com/supabase/supabase-flutter/issues/560
if (_maybeSingle && method.toUpperCase() == 'GET' && body is List) {
if (body.length > 1) {
throw PostgrestException(
final exception = PostgrestException(
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
code: '406',
details:
'Results contain ${body.length} rows, application/vnd.pgrst.object+json requires 1 row',
hint: null,
message: 'JSON object requested, multiple (or no) rows returned',
);

_log.fine('$exception for request $_url');
throw exception;
} else if (body.length == 1) {
body = body.first;
} else {
Expand Down Expand Up @@ -286,6 +293,8 @@ class PostgrestBuilder<T, S, R> implements Future<T> {
);
}

_log.fine('$error for request $_url');

throw error;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/postgrest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
http: '>=0.13.0 <2.0.0'
yet_another_json_isolate: 2.0.2
meta: ^1.9.1
logging: ^1.2.0

dev_dependencies:
collection: ^1.16.0
Expand Down

0 comments on commit 3575ac3

Please sign in to comment.