Skip to content

Commit

Permalink
Fix new exceptions superclass (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Feb 21, 2025
1 parent 31df6ab commit dbfb173
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 3.5.4

**Warning**: This version fixes the inconsistency of `3.5.3` by:
- `UniqueViolationException` and `ForeignKeyViolationException` extends `ServerException` instead of `PgException`, and
- they no longer have public constructor (please open an issue if this is something you'd rely on).
- The exception from cancelling the statement is also extends `ServerException` instead of `PgException`.

## 3.5.3

- New typed exceptions: `UniqueViolationException`, `ForeignKeyViolationException`. [#416](https://github.com/isoos/postgresql-dart/pull/416) by [hurrba](https://github.com/hurrba)
Expand Down
52 changes: 32 additions & 20 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ class ServerException extends PgException {
this.routineName,
});

ServerException._from(ServerException original)
: this._(
original.message,
severity: original.severity,
position: original.position,
internalPosition: original.internalPosition,
lineNumber: original.lineNumber,
code: original.code,
detail: original.detail,
hint: original.hint,
internalQuery: original.internalQuery,
trace: original.trace,
schemaName: original.schemaName,
tableName: original.tableName,
columnName: original.columnName,
dataTypeName: original.dataTypeName,
constraintName: original.constraintName,
fileName: original.fileName,
routineName: original.routineName,
);

@override
String toString() {
final buff = StringBuffer('$severity $code: $message');
Expand Down Expand Up @@ -193,37 +214,28 @@ ServerException buildExceptionFromErrorFields(List<ErrorField> errorFields) {
PgException transformServerException(ServerException ex) {
// TODO: consider adding more exception from https://www.postgresql.org/docs/current/errcodes-appendix.html
return switch (ex.code) {
'23505' => UniqueViolationException(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,
'23505' => UniqueViolationException._(ex),
'23503' => ForeignKeyViolationException._(ex),
'57014' => _PgQueryCancelledException._(
ex,
// [ex.message, ex.trace].whereType<String>().join(' '),
),
_ => ex,
};
}

class _PgQueryCancelledException extends PgException
class _PgQueryCancelledException extends ServerException
implements TimeoutException {
@override
late final duration = null;

_PgQueryCancelledException(
super.message, {
required super.severity,
});
_PgQueryCancelledException._(super.original) : super._from();
}

class UniqueViolationException extends PgException {
UniqueViolationException(
super.message, {
required super.severity,
});
class UniqueViolationException extends ServerException {
UniqueViolationException._(super.original) : super._from();
}

class ForeignKeyViolationException extends PgException {
ForeignKeyViolationException(
super.message, {
required super.severity,
});
class ForeignKeyViolationException extends ServerException {
ForeignKeyViolationException._(super.original) : super._from();
}
4 changes: 2 additions & 2 deletions 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.5.3
description: PostgreSQL database driver. Supports binary protocol, connection pooling and statement reuse.
version: 3.5.4
homepage: https://github.com/isoos/postgresql-dart
topics:
- sql
Expand Down

0 comments on commit dbfb173

Please sign in to comment.