Skip to content

Commit 3f313b0

Browse files
committed
Also catch exceptions from read and write in PGX.
1 parent 37513ae commit 3f313b0

File tree

7 files changed

+33
-5
lines changed

7 files changed

+33
-5
lines changed

caqti-async/lib/caqti_async.ml

+9-4
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,19 @@ module System = struct
179179
type tcp_flow = Socket.t
180180
type tls_flow = Socket.t
181181

182+
let convert_io_exception exn =
183+
(match Async_kernel.Monitor.extract_exn exn with
184+
| Core_unix.Unix_error (err, func, arg) ->
185+
Some (Msg_unix (err, func, arg))
186+
| _ -> None)
187+
182188
let intercept_exceptions f =
183189
Async_kernel.Monitor.try_with f >|= function
184190
| Ok _ as r -> r
185191
| Error exn ->
186-
(match Async_kernel.Monitor.extract_exn exn with
187-
| Core_unix.Unix_error (err, func, arg) ->
188-
Error (Msg_unix (err, func, arg))
189-
| _ -> raise exn)
192+
(match convert_io_exception exn with
193+
| Some msg -> Error msg
194+
| None -> raise exn)
190195

191196
let connect_tcp ~sw:_ ~stdenv:() addr =
192197
intercept_exceptions @@ fun () ->

caqti-driver-pgx/lib/caqti_driver_pgx.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ module Connect_functor (System : Caqti_platform.System_sig.S) = struct
272272
| Failure msg -> (* Raised by our Pgx.Io implementation. *)
273273
Fiber.return (Error (h (Caqti_error.Msg msg)))
274274
| exn ->
275-
raise exn)
275+
(match Net.convert_io_exception exn with
276+
| Some msg -> Fiber.return (Error (h msg))
277+
| None -> raise exn))
276278

277279
let intercept_request_failed ~uri ~query =
278280
intercept (Caqti_error.request_failed ~uri ~query)

caqti-eio/lib/system.ml

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ module Net = struct
134134
with Eio.Exn.Io _ as exn ->
135135
Error (`Msg (Format.asprintf "%a" Eio.Exn.pp exn))
136136

137+
let convert_io_exception = function
138+
| Eio.Exn.Io (err, ctx) -> Some (Msg_io (err, ctx))
139+
| _ -> None
140+
137141
module Socket = struct
138142
type t = {
139143
flow: [Eio.Flow.two_way_ty | Eio.Resource.close_ty] Eio.Resource.t;

caqti-lwt/lib-unix/system.ml

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ module Net = struct
8181
(`Msg ("Cannot resolve host name: " ^ Unix.error_message code))
8282
| exn -> Lwt.fail exn)
8383

84+
let convert_io_exception = function
85+
| Unix.Unix_error (err, fn, arg) -> Some (Msg_unix (err, fn, arg))
86+
| _ -> None
87+
8488
type socket = {
8589
fd: Lwt_unix.file_descr option;
8690
ic: Lwt_io.input_channel;

caqti-mirage/lib/caqti_mirage.ml

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ struct
110110
| false, false ->
111111
Lwt.return (Error (`Msg "No IP address assigned to host.")))
112112

113+
let convert_io_exception = function
114+
| Failure msg -> Some (Caqti_error.Msg msg) (* Channel.S.error *)
115+
| _ -> None
116+
113117
module Make_stream_ops (Channel : Mirage_channel.S) = struct
114118
type t = Channel.t
115119

caqti/lib-blocking/caqti_blocking.ml

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ module System = struct
8888
include System_core
8989

9090
module Net = struct
91+
9192
module Sockaddr = struct
9293
type t = Unix.sockaddr
9394
let unix s = Unix.ADDR_UNIX s
@@ -105,6 +106,10 @@ module System = struct
105106
| Unix.Unix_error (code, _, _) ->
106107
Error (`Msg ("Cannot resolve host name: " ^ Unix.error_message code))
107108

109+
let convert_io_exception = function
110+
| Unix.Unix_error (err, fn, arg) -> Some (Msg_unix (err, fn, arg))
111+
| _ -> None
112+
108113
module Socket = struct
109114
type t = Tcp of in_channel * out_channel
110115

caqti/lib-platform/system_sig.ml

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ module type NET = sig
159159
error return indicates that an appropriate DNS server could not be
160160
queried. *)
161161

162+
val convert_io_exception : exn -> Caqti_error.msg option
163+
(** If the read and write operations in Socket raise exceptions other than
164+
{!End_of_file} and {!Failure}, this function is used to intercept them. *)
165+
162166
(** A socket with input and output channels and dedicated IO functions. This
163167
bundling is done to support the various APIs involved for networking and
164168
StartTLS. *)

0 commit comments

Comments
 (0)