Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better exception stacktraces (in some cases) using package:stack_trace. #409

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
27 changes: 20 additions & 7 deletions lib/src/v3/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -652,7 +654,9 @@ class _PreparedStatement extends Statement {
/// See more in https://github.com/isoos/postgresql-dart/issues/390
Queue<String>? _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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Loading