forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQLError.swift
148 lines (121 loc) · 4.27 KB
/
PSQLError.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import NIOCore
struct PSQLError: Error {
enum Base {
case sslUnsupported
case failedToAddSSLHandler(underlying: Error)
case server(PSQLBackendMessage.ErrorResponse)
case decoding(PSQLDecodingError)
case unexpectedBackendMessage(PSQLBackendMessage)
case unsupportedAuthMechanism(PSQLAuthScheme)
case authMechanismRequiresPassword
case saslError(underlyingError: Error)
case tooManyParameters
case connectionQuiescing
case connectionClosed
case connectionError(underlying: Error)
case uncleanShutdown
case casting(PSQLCastingError)
}
internal var base: Base
private init(_ base: Base) {
self.base = base
}
static var sslUnsupported: PSQLError {
Self.init(.sslUnsupported)
}
static func failedToAddSSLHandler(underlying error: Error) -> PSQLError {
Self.init(.failedToAddSSLHandler(underlying: error))
}
static func server(_ message: PSQLBackendMessage.ErrorResponse) -> PSQLError {
Self.init(.server(message))
}
static func decoding(_ error: PSQLDecodingError) -> PSQLError {
Self.init(.decoding(error))
}
static func unexpectedBackendMessage(_ message: PSQLBackendMessage) -> PSQLError {
Self.init(.unexpectedBackendMessage(message))
}
static func unsupportedAuthMechanism(_ authScheme: PSQLAuthScheme) -> PSQLError {
Self.init(.unsupportedAuthMechanism(authScheme))
}
static var authMechanismRequiresPassword: PSQLError {
Self.init(.authMechanismRequiresPassword)
}
static func sasl(underlying: Error) -> PSQLError {
Self.init(.saslError(underlyingError: underlying))
}
static var tooManyParameters: PSQLError {
Self.init(.tooManyParameters)
}
static var connectionQuiescing: PSQLError {
Self.init(.connectionQuiescing)
}
static var connectionClosed: PSQLError {
Self.init(.connectionClosed)
}
static func channel(underlying: Error) -> PSQLError {
Self.init(.connectionError(underlying: underlying))
}
static var uncleanShutdown: PSQLError {
Self.init(.uncleanShutdown)
}
}
struct PSQLCastingError: Error {
let columnName: String
let columnIndex: Int
let file: String
let line: Int
let targetType: PSQLDecodable.Type
let postgresType: PSQLDataType
let postgresData: ByteBuffer?
let description: String
let underlying: Error?
static func missingData(targetType: PSQLDecodable.Type, type: PSQLDataType, context: PSQLDecodingContext) -> Self {
PSQLCastingError(
columnName: context.columnName,
columnIndex: context.columnIndex,
file: context.file,
line: context.line,
targetType: targetType,
postgresType: type,
postgresData: nil,
description: """
Failed to cast Postgres data type \(type.description) to Swift type \(targetType) \
because of missing data in \(context.file) line \(context.line).
""",
underlying: nil
)
}
static func failure(targetType: PSQLDecodable.Type,
type: PSQLDataType,
postgresData: ByteBuffer,
description: String? = nil,
underlying: Error? = nil,
context: PSQLDecodingContext) -> Self
{
PSQLCastingError(
columnName: context.columnName,
columnIndex: context.columnIndex,
file: context.file,
line: context.line,
targetType: targetType,
postgresType: type,
postgresData: postgresData,
description: description ?? """
Failed to cast Postgres data type \(type.description) to Swift type \(targetType) \
in \(context.file) line \(context.line)."
""",
underlying: underlying
)
}
}
enum PSQLAuthScheme {
case none
case kerberosV5
case md5
case plaintext
case scmCredential
case gss
case sspi
case sasl(mechanisms: [String])
}