Skip to content

Commit dbfb173

Browse files
authored
Fix new exceptions superclass (#418)
1 parent 31df6ab commit dbfb173

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

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

512
- New typed exceptions: `UniqueViolationException`, `ForeignKeyViolationException`. [#416](https://github.com/isoos/postgresql-dart/pull/416) by [hurrba](https://github.com/hurrba)

lib/src/exceptions.dart

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,27 @@ class ServerException extends PgException {
138138
this.routineName,
139139
});
140140

141+
ServerException._from(ServerException original)
142+
: this._(
143+
original.message,
144+
severity: original.severity,
145+
position: original.position,
146+
internalPosition: original.internalPosition,
147+
lineNumber: original.lineNumber,
148+
code: original.code,
149+
detail: original.detail,
150+
hint: original.hint,
151+
internalQuery: original.internalQuery,
152+
trace: original.trace,
153+
schemaName: original.schemaName,
154+
tableName: original.tableName,
155+
columnName: original.columnName,
156+
dataTypeName: original.dataTypeName,
157+
constraintName: original.constraintName,
158+
fileName: original.fileName,
159+
routineName: original.routineName,
160+
);
161+
141162
@override
142163
String toString() {
143164
final buff = StringBuffer('$severity $code: $message');
@@ -193,37 +214,28 @@ ServerException buildExceptionFromErrorFields(List<ErrorField> errorFields) {
193214
PgException transformServerException(ServerException ex) {
194215
// TODO: consider adding more exception from https://www.postgresql.org/docs/current/errcodes-appendix.html
195216
return switch (ex.code) {
196-
'23505' => UniqueViolationException(ex.message, severity: ex.severity),
197-
'23503' => ForeignKeyViolationException(ex.message, severity: ex.severity),
198-
'57014' => _PgQueryCancelledException(
199-
['${ex.code}:', ex.message, ex.trace].whereType<String>().join(' '),
200-
severity: ex.severity,
217+
'23505' => UniqueViolationException._(ex),
218+
'23503' => ForeignKeyViolationException._(ex),
219+
'57014' => _PgQueryCancelledException._(
220+
ex,
221+
// [ex.message, ex.trace].whereType<String>().join(' '),
201222
),
202223
_ => ex,
203224
};
204225
}
205226

206-
class _PgQueryCancelledException extends PgException
227+
class _PgQueryCancelledException extends ServerException
207228
implements TimeoutException {
208229
@override
209230
late final duration = null;
210231

211-
_PgQueryCancelledException(
212-
super.message, {
213-
required super.severity,
214-
});
232+
_PgQueryCancelledException._(super.original) : super._from();
215233
}
216234

217-
class UniqueViolationException extends PgException {
218-
UniqueViolationException(
219-
super.message, {
220-
required super.severity,
221-
});
235+
class UniqueViolationException extends ServerException {
236+
UniqueViolationException._(super.original) : super._from();
222237
}
223238

224-
class ForeignKeyViolationException extends PgException {
225-
ForeignKeyViolationException(
226-
super.message, {
227-
required super.severity,
228-
});
239+
class ForeignKeyViolationException extends ServerException {
240+
ForeignKeyViolationException._(super.original) : super._from();
229241
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres
2-
description: PostgreSQL database driver. Supports statement reuse and binary protocol and connection pooling.
3-
version: 3.5.3
2+
description: PostgreSQL database driver. Supports binary protocol, connection pooling and statement reuse.
3+
version: 3.5.4
44
homepage: https://github.com/isoos/postgresql-dart
55
topics:
66
- sql

0 commit comments

Comments
 (0)