Skip to content

Commit

Permalink
Include original stacktrace when query fails (#15)
Browse files Browse the repository at this point in the history
* StackTrace attached to the Query object

* transaction test

* Execute
  • Loading branch information
davidmartos96 committed Nov 5, 2021
1 parent a3fe383 commit de0a9d9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
13 changes: 9 additions & 4 deletions lib/src/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class _OidCache {
.toSet()
.where((oid) => oid > 0 && !_tableOIDNameMap.containsKey(oid))
.toList()
..sort();
..sort();

if (unresolvedTableOIDs.isNotEmpty) {
await _resolveTableOIDs(c, unresolvedTableOIDs);
Expand Down Expand Up @@ -441,7 +441,12 @@ abstract class _PostgreSQLExecutionContextMixin
}

final query = Query<List<List<dynamic>>>(
fmtString, substitutionValues, _connection, _transaction);
fmtString,
substitutionValues,
_connection,
_transaction,
StackTrace.current,
);
if (allowReuse) {
query.statementIdentifier = _connection._cache.identifierForQuery(query);
}
Expand Down Expand Up @@ -489,8 +494,8 @@ abstract class _PostgreSQLExecutionContextMixin
'Attempting to execute query, but connection is not open.');
}

final query = Query<void>(
fmtString, substitutionValues, _connection, _transaction,
final query = Query<void>(fmtString, substitutionValues, _connection,
_transaction, StackTrace.current,
onlyReturnAffectedRowCount: true);

final result = await _enqueue(query, timeoutInSeconds: timeoutInSeconds);
Expand Down
7 changes: 5 additions & 2 deletions lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Query<T> {
this.statement,
this.substitutionValues,
this.connection,
this.transaction, {
this.transaction,
this.queryStackTrace, {
this.onlyReturnAffectedRowCount = false,
});

Expand All @@ -43,6 +44,8 @@ class Query<T> {

List<FieldDescription>? get fieldDescriptions => _fieldDescriptions;

final StackTrace queryStackTrace;

set fieldDescriptions(List<FieldDescription>? fds) {
_fieldDescriptions = fds;
cache?.fieldDescriptions = fds;
Expand Down Expand Up @@ -166,7 +169,7 @@ class Query<T> {
return;
}

_onComplete.completeError(error, stackTrace);
_onComplete.completeError(error, stackTrace ?? queryStackTrace);
}

@override
Expand Down
5 changes: 3 additions & 2 deletions lib/src/transaction_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class _TransactionProxy extends Object
implements PostgreSQLExecutionContext {
_TransactionProxy(
this._connection, this.executionBlock, this.commitTimeoutInSeconds) {
_beginQuery = Query<int>('BEGIN', {}, _connection, this,
_beginQuery = Query<int>('BEGIN', {}, _connection, this, StackTrace.current,
onlyReturnAffectedRowCount: true);

_beginQuery.future
Expand Down Expand Up @@ -89,7 +89,8 @@ class _TransactionProxy extends Object
'that prevented this query from executing.');
_queue.cancel(err);

final rollback = Query<int>('ROLLBACK', {}, _connection, _transaction,
final rollback = Query<int>(
'ROLLBACK', {}, _connection, _transaction, StackTrace.current,
onlyReturnAffectedRowCount: true);
_queue.addEvenIfCancelled(rollback);

Expand Down
38 changes: 35 additions & 3 deletions test/error_handling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,48 @@ void main() {
usePostgresDocker();

test('Reports stacktrace correctly', () async {
final conn = PostgreSQLConnection('localhost', 5432, 'dart_test', username: 'dart', password: 'dart');
final conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
username: 'dart', password: 'dart');
await conn.open();
addTearDown(() async => conn.close());

// Root connection query
try {
await conn.query('SELECT hello');
fail('Should not reach');
} catch (e, st) {
// TODO: This expectation fails
//expect(st.toString(), isNotEmpty);
expect(e.toString(), contains('column "hello" does not exist'));
expect(
st.toString(),
contains('postgresql-dart/test/error_handling_test.dart'),
);
}

// Root connection execute
try {
await conn.execute('DELETE FROM hello');
fail('Should not reach');
} catch (e, st) {
print(e);
expect(e.toString(), contains('relation "hello" does not exist'));
expect(
st.toString(),
contains('postgresql-dart/test/error_handling_test.dart'),
);
}

// Inside transaction
try {
await conn.transaction((conn) async {
await conn.query('SELECT hello');
fail('Should not reach');
});
} catch (e, st) {
expect(e.toString(), contains('column "hello" does not exist'));
expect(
st.toString(),
contains('postgresql-dart/test/error_handling_test.dart'),
);
}
});
}

0 comments on commit de0a9d9

Please sign in to comment.