From ec340a660796524340bcd5be6a1a6ce7eb0371f3 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sat, 6 May 2023 23:20:26 -0500 Subject: [PATCH] Update PostgresError's DatabaseError conformance for new PostgresNIO behavior. Fixes broken tests. --- .../PostgresError+Database.swift | 53 ++++++++++++++----- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/Sources/FluentPostgresDriver/PostgresError+Database.swift b/Sources/FluentPostgresDriver/PostgresError+Database.swift index 60010d2..68cc3ee 100644 --- a/Sources/FluentPostgresDriver/PostgresError+Database.swift +++ b/Sources/FluentPostgresDriver/PostgresError+Database.swift @@ -3,9 +3,9 @@ import FluentSQL import PostgresKit import PostgresNIO -extension PostgresError: DatabaseError { - public var isSyntaxError: Bool { - switch self.code { +fileprivate extension PostgresError.Code { + var isSyntaxError: Bool { + switch self { case .syntaxErrorOrAccessRuleViolation, .syntaxError, .insufficientPrivilege, @@ -54,18 +54,9 @@ extension PostgresError: DatabaseError { return false } } - - public var isConnectionClosed: Bool { + + var isConstraintFailure: Bool { switch self { - case .connectionClosed: - return true - default: - return false - } - } - - public var isConstraintFailure: Bool { - switch self.code { case .integrityConstraintViolation, .restrictViolation, .notNullViolation, @@ -79,3 +70,37 @@ extension PostgresError: DatabaseError { } } } + +extension PostgresError: DatabaseError { + public var isSyntaxError: Bool { self.code.isSyntaxError } + public var isConnectionClosed: Bool { + switch self { + case .connectionClosed: return true + default: return false + } + } + public var isConstraintFailure: Bool { self.code.isConstraintFailure } +} + +extension PSQLError: DatabaseError { + public var isSyntaxError: Bool { + switch self.code { + case .server: return self.serverInfo?[.sqlState].map { PostgresError.Code(raw: $0).isSyntaxError } ?? false + default: return false + } + } + + public var isConnectionClosed: Bool { + switch self.code { + case .connectionClosed: return true + default: return false + } + } + + public var isConstraintFailure: Bool { + switch self.code { + case .server: return self.serverInfo?[.sqlState].map { PostgresError.Code(raw: $0).isConstraintFailure } ?? false + default: return false + } + } +}