Skip to content

Commit 1be5882

Browse files
committed
#5: Check why a connection fails
1 parent 4cc5a2d commit 1be5882

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

Sources/PostgresConnectionPool/PostgresConnectionPool.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,31 @@ public actor PostgresConnectionPool {
433433
}
434434
catch {
435435
let connectionRuntime = fabs(connectionStartTimestamp.timeIntervalSinceNow)
436-
logger.debug("[\(poolName)] Connection \(poolConnection.id) failed after \(connectionRuntime.rounded(toPlaces: 2))s: \(error)")
437436
poolConnection.state = .closed
438437

439-
// TODO: Don't just open a new connection, check first if this is a permanent error like wrong password etc.
438+
let logMessage: Logger.Message = "[\(poolName)] Connection \(poolConnection.id) failed after \(connectionRuntime.rounded(toPlaces: 2))s: \(error)"
439+
440+
// Don't just open a new connection, check first if this is a permanent error like wrong password etc.
441+
if let pslError = error as? PSQLError,
442+
let serverInfo = pslError.serverInfo,
443+
let code = serverInfo[.sqlState]
444+
{
445+
// TODO: List of hard errors
446+
switch PostgresError.Code(raw: code) {
447+
case .adminShutdown,
448+
.cannotConnectNow,
449+
.invalidAuthorizationSpecification,
450+
.invalidName,
451+
.invalidPassword:
452+
logger.error(logMessage)
453+
return
454+
455+
default:
456+
break
457+
}
458+
}
459+
460+
logger.warning(logMessage)
440461

441462
Task.detached { [weak self] in
442463
await self?.openConnection()

0 commit comments

Comments
 (0)