Skip to content
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
33 changes: 33 additions & 0 deletions lib/core/database/database_error_mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ QueryaDatabaseException mapDatabaseError(
originalError: error,
stackTrace: stackTrace,
);
} else if (_columnMatch(raw) case final column?) {
// Before the table patterns: `column "x" of relation "t" does not exist`
// contains `relation "t" does not exist` and is not a missing table.
mapped = ColumnNotFoundException(
column.table == null
? 'Column "${column.name}" does not exist'
: 'Column "${column.name}" does not exist in "${column.table}"',
columnName: column.name,
tableName: column.table,
remediationHint:
'Check the column name, or refresh the object tree if it was added '
'or renamed',
originalError: error,
stackTrace: stackTrace,
);
} else if (_firstMatch(raw, [
RegExp(r'relation "([^"]+)" does not exist', caseSensitive: false),
RegExp(r"table '([^']+)' doesn't exist", caseSensitive: false),
Expand Down Expand Up @@ -195,6 +210,24 @@ String _innermost(String raw) {
bool _hasAny(String lower, List<String> needles) =>
needles.any(lower.contains);

/// A missing column in the words of PostgreSQL, MySQL or SQLite.
({String name, String? table})? _columnMatch(String raw) {
final pg = RegExp(
r'column "([^"]+)" of relation "([^"]+)" does not exist',
caseSensitive: false,
).firstMatch(raw);
if (pg != null) return (name: pg.group(1)!, table: pg.group(2));
for (final p in [
RegExp(r'column "([^"]+)" does not exist', caseSensitive: false),
RegExp(r"unknown column '([^']+)'", caseSensitive: false),
RegExp(r'no such column: ([\w."]+)', caseSensitive: false),
]) {
final m = p.firstMatch(raw);
if (m != null) return (name: m.group(1)!, table: null);
}
return null;
}

String? _firstMatch(String text, List<RegExp> patterns) {
for (final p in patterns) {
final m = p.firstMatch(text);
Expand Down
16 changes: 16 additions & 0 deletions lib/core/database/querya_database_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ final class TableNotFoundException extends QueryaDatabaseException {
final String? tableName;
}

/// A column referenced by a statement does not exist (#1310).
final class ColumnNotFoundException extends QueryaDatabaseException {
const ColumnNotFoundException(
super.message, {
this.columnName,
this.tableName,
super.detailedExplanation,
super.remediationHint,
super.originalError,
super.stackTrace,
});

final String? columnName;
final String? tableName;
}

/// A lock could not be acquired in time (busy / locked / deadlock).
final class LockTimeoutException extends QueryaDatabaseException {
const LockTimeoutException(
Expand Down
39 changes: 39 additions & 0 deletions test/core/database/database_error_mapper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@ import 'package:querya_desktop/core/database/redis_connection.dart';
import 'package:querya_desktop/core/database/sqlite_connection.dart';

void main() {
group('missing columns (#1310)', () {
test('a column of an existing table is not a missing table', () {
final e = mapDatabaseError(
Exception('ERROR: column "nickname" of relation "users" does not exist'),
driver: DatabaseDriver.postgres,
);
expect(e, isA<ColumnNotFoundException>());
expect((e as ColumnNotFoundException).columnName, 'nickname');
expect(e.tableName, 'users');
expect(e.message, 'Column "nickname" does not exist in "users"');
expect(e, isNot(isA<TableNotFoundException>()));
});

test('PostgreSQL, MySQL and SQLite wordings without a table', () {
for (final (text, driver, name) in [
('column "price" does not exist', DatabaseDriver.postgres, 'price'),
(
"Unknown column 'price' in 'field list'",
DatabaseDriver.mysql,
'price'
),
('no such column: orders.price', DatabaseDriver.sqlite, 'orders.price'),
]) {
final e = mapDatabaseError(Exception(text), driver: driver);
expect(e, isA<ColumnNotFoundException>(), reason: text);
expect((e as ColumnNotFoundException).columnName, name);
}
});

test('a missing relation is still a missing table', () {
final e = mapDatabaseError(
Exception('ERROR: relation "orders" does not exist'),
driver: DatabaseDriver.postgres,
);
expect(e, isA<TableNotFoundException>());
expect((e as TableNotFoundException).tableName, 'orders');
});
});

group('mapDatabaseError PostgreSQL', () {
test('wrong password is AuthFailedException', () {
final e = mapDatabaseError(
Expand Down
Loading