diff --git a/CHANGELOG.md b/CHANGELOG.md index 86f5723..5baf2c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.4.9 + +- Better exception stacktraces (in some cases) using `package:stack_trace`. + ## 3.4.8 - 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). diff --git a/lib/src/v3/connection.dart b/lib/src/v3/connection.dart index 09479b3..b0c9a41 100644 --- a/lib/src/v3/connection.dart +++ b/lib/src/v3/connection.dart @@ -8,6 +8,7 @@ import 'package:async/async.dart' as async; import 'package:charcode/ascii.dart'; import 'package:meta/meta.dart'; import 'package:pool/pool.dart' as pool; +import 'package:stack_trace/stack_trace.dart'; import 'package:stream_channel/stream_channel.dart'; import '../../postgres.dart'; @@ -183,6 +184,7 @@ abstract class _PgSessionBase implements Session { } Future<_PreparedStatement> _prepare(Object query) async { + final trace = Trace.current(); final conn = _connection; final name = 's/${conn._statementCounter++}'; final description = InternalQueryDescription.wrap( @@ -196,7 +198,7 @@ abstract class _PgSessionBase implements Session { typeOids: description.parameterTypes?.map((e) => e?.oid).toList(), )); - return _PreparedStatement(description, name, this); + return _PreparedStatement(description, name, this, trace); } } @@ -652,7 +654,9 @@ class _PreparedStatement extends Statement { /// See more in https://github.com/isoos/postgresql-dart/issues/390 Queue? _portalsToClose; - _PreparedStatement(this._description, this._name, this._session); + final Trace _trace; + + _PreparedStatement(this._description, this._name, this._session, this._trace); @override ResultStream bind(Object? parameters) { @@ -746,14 +750,16 @@ class _PgResultStreamSubscription PgConnectionImplementation get connection => session._connection; late final _portalName = 'p/${connection._portalCounter++}'; - final StackTrace _trace; + final Trace? _parentTrace; + final Trace _callerTrace; _PgResultStreamSubscription( _BoundStatement statement, this._controller, this._source) : session = statement.statement._session, ignoreRows = false, _boundStatement = statement, - _trace = StackTrace.current { + _parentTrace = statement.statement._trace, + _callerTrace = Trace.current() { _scheduleStatement(() async { connection._pending = this; @@ -791,7 +797,8 @@ class _PgResultStreamSubscription this._source, this.ignoreRows, { void Function()? cleanup, - }) : _trace = StackTrace.current { + }) : _parentTrace = null, + _callerTrace = Trace.current() { _scheduleStatement(() async { connection._pending = this; @@ -834,17 +841,23 @@ class _PgResultStreamSubscription await _controller.close(); } + StackTrace _trace() => Chain([ + Trace.current(1), + _callerTrace, + if (_parentTrace != null) _parentTrace!, + ]); + @override void handleConnectionClosed(PgException? dueToException) { if (dueToException != null) { - _controller.addError(dueToException, _trace); + _controller.addError(dueToException, _trace()); } _completeQuery(); } @override void handleError(PgException exception) { - _controller.addError(exception, _trace); + _controller.addError(exception, _trace()); } @override diff --git a/pubspec.yaml b/pubspec.yaml index e8f4d38..e653252 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: postgres description: PostgreSQL database driver. Supports statement reuse and binary protocol and connection pooling. -version: 3.4.8 +version: 3.4.9 homepage: https://github.com/isoos/postgresql-dart topics: - sql @@ -16,6 +16,7 @@ dependencies: crypto: ^3.0.0 collection: ^1.15.0 sasl_scram: ^0.1.0 + stack_trace: ^1.12.1 stream_channel: ^2.1.1 async: ^2.10.0 charcode: ^1.3.1