Skip to content

Commit c71ff76

Browse files
committed
Normalize string_of_error formatting
1 parent ab46e13 commit c71ff76

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
- Updated asynchronous and notification examples to use `socket_descr` instead
1313
of coercing `socket` with `Obj.magic`.
1414

15+
### Fixed
16+
17+
- `string_of_error` now normalizes trailing newlines in backend-provided error
18+
messages and avoids odd punctuation for empty `Unexpected_status` details.
19+
1520
## [5.3.2] - 2025-09-27
1621

1722
### Fixed

lib/postgresql.ml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,31 @@ type error =
311311
| Unexpected_status of result_status * string * result_status list
312312
| Cancel_failure of string
313313

314+
let trim_trailing_newlines s =
315+
let rec find_end i =
316+
if i = 0 then 0
317+
else match s.[i - 1] with '\n' | '\r' -> find_end (i - 1) | _ -> i
318+
in
319+
String.sub s 0 (find_end (String.length s))
320+
321+
let format_error_with_detail prefix detail =
322+
let detail = trim_trailing_newlines detail in
323+
if detail = "" then prefix else prefix ^ ": " ^ detail
324+
314325
let string_of_error = function
315326
| Field_out_of_range (i, n) ->
316327
sprintf "Field number %i is out of range [0..%i]" i (n - 1)
317328
| Tuple_out_of_range (i, n) ->
318329
sprintf "Tuple number %i is out of range [0..%i]" i (n - 1)
319330
| Binary -> sprintf "This function does not accept binary tuples"
320-
| Connection_failure s -> "Connection failure: " ^ s
331+
| Connection_failure s -> format_error_with_detail "Connection failure" s
321332
| Unexpected_status (s, msg, sl) ->
322-
sprintf "Result status %s unexpected (expected status:%s); %s"
323-
(result_status s)
324-
(String.concat "," (List.map result_status sl))
333+
format_error_with_detail
334+
(sprintf "Result status %s unexpected (expected status: %s)"
335+
(result_status s)
336+
(String.concat "," (List.map result_status sl)))
325337
msg
326-
| Cancel_failure s -> "Cancel failure: " ^ s
338+
| Cancel_failure s -> format_error_with_detail "Cancel failure" s
327339

328340
exception Error of error
329341

0 commit comments

Comments
 (0)