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
9 changes: 8 additions & 1 deletion lib/postgres.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import 'src/v3/protocol.dart';
import 'src/v3/query_description.dart';

export 'src/exceptions.dart'
show BadCertificateException, Severity, PgException, ServerException;
show
BadCertificateException,
Severity,
PgException,
ServerException,
DuplicateKeyException,
ForeignKeyViolationException;
export 'src/pool/pool_api.dart';
export 'src/replication.dart';
export 'src/types.dart';
Expand Down Expand Up @@ -225,6 +231,7 @@ abstract class Connection implements Session, SessionExecutor {
}

ConnectionInfo get info;

Channels get channels;
}

Expand Down
30 changes: 23 additions & 7 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,15 @@ ServerException buildExceptionFromErrorFields(List<ErrorField> errorFields) {
}

PgException transformServerException(ServerException ex) {
if (ex.code == '57014') {
return _PgQueryCancelledException(
['${ex.code}:', ex.message, ex.trace].whereType<String>().join(' '),
severity: ex.severity,
);
}
return ex;
return switch (ex.code) {
'23505' => DuplicateKeyException(ex.message, severity: ex.severity),
'23503' => ForeignKeyViolationException(ex.message, severity: ex.severity),
'57014' => _PgQueryCancelledException(
['${ex.code}:', ex.message, ex.trace].whereType<String>().join(' '),
severity: ex.severity,
),
_ => ex,
};
}

class _PgQueryCancelledException extends PgException
Expand All @@ -210,3 +212,17 @@ class _PgQueryCancelledException extends PgException
required super.severity,
});
}

class DuplicateKeyException extends PgException {
DuplicateKeyException(
super.message, {
required super.severity,
});
}

class ForeignKeyViolationException extends PgException {
ForeignKeyViolationException(
super.message, {
required super.severity,
});
}
41 changes: 41 additions & 0 deletions test/error_handling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,46 @@ void main() {
),
);
});

test('DuplicateKeyException', () async {
final c = await server.newConnection();
await c.execute('CREATE TABLE test (id INT PRIMARY KEY);');
await c.execute('INSERT INTO test (id) VALUES (1);');
addTearDown(() async => c.execute('DROP TABLE test;'));

try {
await c.execute('INSERT INTO test (id) VALUES (1);');
} catch (e) {
expect(e, isA<DuplicateKeyException>());
expect(
e.toString(),
contains(
'duplicate key value violates unique constraint "test_pkey"'));
}
});

test('ForeignKeyViolationException', () async {
final c = await server.newConnection();
await c.execute('CREATE TABLE test (id INT PRIMARY KEY);');
await c.execute(
'CREATE TABLE test2 (id INT PRIMARY KEY, test_id INT REFERENCES test(id));');
await c.execute('INSERT INTO test (id) VALUES (1);');
addTearDown(() async {
await c.execute('DROP TABLE test2;');
await c.execute('DROP TABLE test;');
});

try {
await c.execute('INSERT INTO test2 (id, test_id) VALUES (1, 2);');
} catch (e) {
expect(e, isA<ForeignKeyViolationException>());
expect(
e.toString(),
contains(
'insert or update on table "test2" violates foreign key constraint "test2_test_id_fkey"',
),
);
}
});
});
}
2 changes: 1 addition & 1 deletion test/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ void main() {
// ignore
}
}),
throwsA(isA<ServerException>()),
throwsA(isA<DuplicateKeyException>()),
);
});
});
Expand Down