Skip to content

Commit 9daf08c

Browse files
authored
Better exception stacktraces (in some cases) using package:stack_trace. (#409)
1 parent 4243d95 commit 9daf08c

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.4.9
4+
5+
- Better exception stacktraces (in some cases) using `package:stack_trace`.
6+
37
## 3.4.8
48

59
- Fix: Don't ignore de pool resource `close` call when using foce. [#406](https://github.com/isoos/postgresql-dart/pull/406) by [davidmartos96](https://github.com/davidmartos96).

lib/src/v3/connection.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:async/async.dart' as async;
88
import 'package:charcode/ascii.dart';
99
import 'package:meta/meta.dart';
1010
import 'package:pool/pool.dart' as pool;
11+
import 'package:stack_trace/stack_trace.dart';
1112
import 'package:stream_channel/stream_channel.dart';
1213

1314
import '../../postgres.dart';
@@ -183,6 +184,7 @@ abstract class _PgSessionBase implements Session {
183184
}
184185

185186
Future<_PreparedStatement> _prepare(Object query) async {
187+
final trace = Trace.current();
186188
final conn = _connection;
187189
final name = 's/${conn._statementCounter++}';
188190
final description = InternalQueryDescription.wrap(
@@ -196,7 +198,7 @@ abstract class _PgSessionBase implements Session {
196198
typeOids: description.parameterTypes?.map((e) => e?.oid).toList(),
197199
));
198200

199-
return _PreparedStatement(description, name, this);
201+
return _PreparedStatement(description, name, this, trace);
200202
}
201203
}
202204

@@ -652,7 +654,9 @@ class _PreparedStatement extends Statement {
652654
/// See more in https://github.com/isoos/postgresql-dart/issues/390
653655
Queue<String>? _portalsToClose;
654656

655-
_PreparedStatement(this._description, this._name, this._session);
657+
final Trace _trace;
658+
659+
_PreparedStatement(this._description, this._name, this._session, this._trace);
656660

657661
@override
658662
ResultStream bind(Object? parameters) {
@@ -746,14 +750,16 @@ class _PgResultStreamSubscription
746750
PgConnectionImplementation get connection => session._connection;
747751

748752
late final _portalName = 'p/${connection._portalCounter++}';
749-
final StackTrace _trace;
753+
final Trace? _parentTrace;
754+
final Trace _callerTrace;
750755

751756
_PgResultStreamSubscription(
752757
_BoundStatement statement, this._controller, this._source)
753758
: session = statement.statement._session,
754759
ignoreRows = false,
755760
_boundStatement = statement,
756-
_trace = StackTrace.current {
761+
_parentTrace = statement.statement._trace,
762+
_callerTrace = Trace.current() {
757763
_scheduleStatement(() async {
758764
connection._pending = this;
759765

@@ -791,7 +797,8 @@ class _PgResultStreamSubscription
791797
this._source,
792798
this.ignoreRows, {
793799
void Function()? cleanup,
794-
}) : _trace = StackTrace.current {
800+
}) : _parentTrace = null,
801+
_callerTrace = Trace.current() {
795802
_scheduleStatement(() async {
796803
connection._pending = this;
797804

@@ -834,17 +841,23 @@ class _PgResultStreamSubscription
834841
await _controller.close();
835842
}
836843

844+
StackTrace _trace() => Chain([
845+
Trace.current(1),
846+
_callerTrace,
847+
if (_parentTrace != null) _parentTrace!,
848+
]);
849+
837850
@override
838851
void handleConnectionClosed(PgException? dueToException) {
839852
if (dueToException != null) {
840-
_controller.addError(dueToException, _trace);
853+
_controller.addError(dueToException, _trace());
841854
}
842855
_completeQuery();
843856
}
844857

845858
@override
846859
void handleError(PgException exception) {
847-
_controller.addError(exception, _trace);
860+
_controller.addError(exception, _trace());
848861
}
849862

850863
@override

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres
22
description: PostgreSQL database driver. Supports statement reuse and binary protocol and connection pooling.
3-
version: 3.4.8
3+
version: 3.4.9
44
homepage: https://github.com/isoos/postgresql-dart
55
topics:
66
- sql
@@ -16,6 +16,7 @@ dependencies:
1616
crypto: ^3.0.0
1717
collection: ^1.15.0
1818
sasl_scram: ^0.1.0
19+
stack_trace: ^1.12.1
1920
stream_channel: ^2.1.1
2021
async: ^2.10.0
2122
charcode: ^1.3.1

0 commit comments

Comments
 (0)