Skip to content

Update PostgresError's DatabaseError conformance for new PostgresNIO behavior #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2023
Merged
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
53 changes: 39 additions & 14 deletions Sources/FluentPostgresDriver/PostgresError+Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
}
}