Skip to content

Commit

Permalink
Use Stdlib.raise instead of Lwt.fail to capture backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterDA committed Jul 22, 2024
1 parent f0ddb83 commit 9b30a5d
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ struct
(* No implementation (can it be done?). What should the failure exception be? *)
exception Cohttp_lwt_xhr_callv_not_implemented

let callv ?ctx:_ _uri _reqs = Lwt.fail Cohttp_lwt_xhr_callv_not_implemented
let callv ?ctx:_ _uri _reqs = raise Cohttp_lwt_xhr_callv_not_implemented

(* ??? *)
end
Expand Down
4 changes: 2 additions & 2 deletions cohttp-lwt-unix/src/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ let wrap_read f ~if_closed =
https://github.com/ocsigen/lwt/pull/635 *)
Lwt.catch f (function
| Lwt_io.Channel_closed _ -> Lwt.return if_closed
| Unix.Unix_error _ as e -> Lwt.fail (IO_error e)
| Unix.Unix_error _ as e -> raise (IO_error e)
| exn -> raise exn)

let wrap_write f =
Lwt.catch f (function
| Unix.Unix_error _ as e -> Lwt.fail (IO_error e)
| Unix.Unix_error _ as e -> raise (IO_error e)
| exn -> raise exn)

let read_line ic =
Expand Down
4 changes: 2 additions & 2 deletions cohttp-lwt-unix/src/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let respond_file ?headers ~fname () =
(fun () ->
(* Check this isn't a directory first *)
( fname |> Lwt_unix.stat >>= fun s ->
if Unix.(s.st_kind <> S_REG) then Lwt.fail Isnt_a_file
else Lwt.return_unit )
if Unix.(s.st_kind <> S_REG) then raise Isnt_a_file else Lwt.return_unit
)
>>= fun () ->
let count = 16384 in
Lwt_io.open_file ~buffer:(Lwt_bytes.create count) ~mode:Lwt_io.input fname
Expand Down
6 changes: 3 additions & 3 deletions cohttp-lwt-unix/test/test_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ let methods (handler : Cohttp_lwt.S.call) uri =
handler `GET Uri.(with_path uri k) >>= fun (res, body) ->
match Response.status res with
| `OK | `No_content -> Body.to_string body
| _ -> Body.drain_body body >>= fun () -> Lwt.fail Not_found
| _ -> Body.drain_body body >>= fun () -> raise Not_found
and delete k =
handler `DELETE Uri.(with_path uri k) >>= fun (res, body) ->
Body.drain_body body >>= fun () ->
match Response.status res with
| `OK | `No_content -> Lwt.return_unit
| _ -> Lwt.fail Not_found
| _ -> raise Not_found
and mem k =
handler `HEAD Uri.(with_path uri k) >>= fun (res, body) ->
Body.drain_body body >|= fun () ->
Expand Down Expand Up @@ -171,7 +171,7 @@ let test_unknown uri =
connection := c;
match body with
(* Still, body may have been (partially) consumed and needs re-creation. *)
| Some (`Stream _) -> Lwt.fail Connection.Retry
| Some (`Stream _) -> raise Connection.Retry
| None | Some (`Empty | `String _ | `Strings _) ->
handler ?headers ?body meth uri)
| e -> Lwt.reraise e)
Expand Down
2 changes: 1 addition & 1 deletion cohttp-lwt/src/connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ module Make (Net : S.Net) : S.Connection with module Net = Net = struct
Queue.push { uri; meth; headers; body; res_r } connection.waiting;
Lwt_condition.broadcast connection.condition ();
res
| Closing _ | Half _ | Closed | Failed _ -> Lwt.fail Retry
| Closing _ | Half _ | Closed | Failed _ -> raise Retry

let rec writer connection =
match connection.state with
Expand Down
4 changes: 2 additions & 2 deletions cohttp-lwt/src/connection_cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ end = struct
(function
| Retry -> (
match body with
| Some (`Stream _) -> Lwt.fail Retry
| Some (`Stream _) -> raise Retry
| None | Some `Empty | Some (`String _) | Some (`Strings _) ->
if retry <= 0 then Lwt.fail Retry else request (retry - 1))
if retry <= 0 then raise Retry else request (retry - 1))
| e -> Lwt.reraise e)
in
request self.retry
Expand Down
2 changes: 1 addition & 1 deletion cohttp-mirage/src/input_channel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Make (Channel : Mirage_channel.S) = struct
Cstruct.blit_to_bytes v 0 buf pos len;
Lwt.return (`Ok len)
| Ok `Eof -> Lwt.return `Eof
| Error e -> Lwt.fail (Read_exn e)
| Error e -> raise (Read_exn e)

let create ?(buf_len = 0x4000) chan =
{ buf = Bytebuffer.create buf_len; chan }
Expand Down
2 changes: 1 addition & 1 deletion cohttp-mirage/src/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module Make (Channel : Mirage_channel.S) = struct
Channel.flush oc >>= function
| Ok () -> Lwt.return_unit
| Error `Closed -> failwith "Trying to write on closed channel"
| Error e -> Lwt.fail (Write_exn e)
| Error e -> raise (Write_exn e)

let flush _ =
(* NOOP since we flush in the normal writer functions above *)
Expand Down

0 comments on commit 9b30a5d

Please sign in to comment.