Skip to content

Commit bd7a395

Browse files
committed
Correct error handling in establishing SSL/TLS connection (Issue #17).
1 parent 8a129e8 commit bd7a395

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

Docs/API/Enums/PostgresError.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,33 @@ <h4>Declaration</h4>
301301
</section>
302302
</div>
303303
</li>
304+
<li class="item">
305+
<div>
306+
<code>
307+
<a name="/s:17PostgresClientKit0A5ErrorO03sslD0yACs0D0_p_tcACmF"></a>
308+
<a name="//apple_ref/swift/Element/sslError(cause:)" class="dashAnchor"></a>
309+
<a class="token" href="#/s:17PostgresClientKit0A5ErrorO03sslD0yACs0D0_p_tcACmF">sslError(cause:)</a>
310+
</code>
311+
</div>
312+
<div class="height-container">
313+
<div class="pointer-container"></div>
314+
<section class="section">
315+
<div class="pointer"></div>
316+
<div class="abstract">
317+
<p>An error occurred in establishing SSL/TLS encryption.</p>
318+
319+
</div>
320+
<div class="declaration">
321+
<h4>Declaration</h4>
322+
<div class="language">
323+
<p class="aside-title">Swift</p>
324+
<pre class="highlight swift"><code><span class="k">case</span> <span class="nf">sslError</span><span class="p">(</span><span class="nv">cause</span><span class="p">:</span> <span class="kt">Error</span><span class="p">)</span></code></pre>
325+
326+
</div>
327+
</div>
328+
</section>
329+
</div>
330+
</li>
304331
<li class="item">
305332
<div>
306333
<code>

Sources/PostgresClientKit/Connection.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,16 @@ public class Connection: CustomStringConvertible {
135135
throw PostgresError.sslNotSupported
136136
}
137137

138-
let sslConfig = configuration.sslServiceConfiguration
139-
let sslService = try SSLService(usingConfiguration: sslConfig)!
140-
socket.delegate = sslService
141-
try sslService.initialize(asServer: false)
142-
try sslService.onConnect(socket: socket)
138+
do {
139+
let sslConfig = configuration.sslServiceConfiguration
140+
let sslService = try SSLService(usingConfiguration: sslConfig)!
141+
socket.delegate = sslService
142+
try sslService.initialize(asServer: false)
143+
try sslService.onConnect(socket: socket)
144+
} catch {
145+
log(.severe, "Unable to establish SSL/TLS encryption: \(error)")
146+
throw PostgresError.sslError(cause: error)
147+
}
143148

144149
log(.fine, "Successfully negotiated SSL/TLS encryption")
145150
}

Sources/PostgresClientKit/PostgresError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public enum PostgresError: Error {
4747
/// The Postgres server reported a SQL error.
4848
case sqlError(notice: Notice)
4949

50+
/// An error occurred in establishing SSL/TLS encryption.
51+
case sslError(cause: Error)
52+
5053
/// The Postgres server does not support SSL/TLS.
5154
case sslNotSupported
5255

0 commit comments

Comments
 (0)