Skip to content

Commit

Permalink
Replace ~debug flag by separate error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Dec 14, 2021
1 parent 6c726d5 commit 640a1b2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/dream.ml
Expand Up @@ -54,6 +54,8 @@ include Dream__middleware.Lowercase_headers
include Dream__middleware.Catch
include Dream__middleware.Site_prefix

let debug_error_handler =
Dream__http.Error_handler.debug_error_handler
let error_template =
Dream__http.Error_handler.customize

Expand Down
23 changes: 6 additions & 17 deletions src/dream.mli
Expand Up @@ -1818,7 +1818,6 @@ type error = {
response : response option;
client : string option;
severity : log_level;
debug : bool;
will_send_response : bool;
}
(** Detailed errors. Ignore this type if only using {!Dream.error_template}.
Expand Down Expand Up @@ -1887,17 +1886,6 @@ type error = {
[`Server] errors and [`Warning] for client errors.
}
{li
[debug] is [true] if {!Dream.run} was called with [~debug].
If so, the default error handler gathers various fields from the current
request, formats the error condition, and passes the resulting string to the
template as [debug_dump].
The default template shows this string in its repsonse, instead of returning
a response with no body.
}
{li
[will_send_response] is [true] in error contexts where Dream will still send
a response.
Expand All @@ -1921,7 +1909,7 @@ type error_handler = error -> response option promise
{!Dream.type-error}. *)

val error_template :
(error -> string option -> response -> response promise) -> error_handler
(error -> string -> response -> response promise) -> error_handler
(** Builds an {!error_handler} from a template. See example
{{:https://github.com/aantron/dream/tree/master/example/9-error#files}
[9-error]} \[{{:http://dream.as/9-error} playground}\].
Expand Down Expand Up @@ -1951,8 +1939,7 @@ val error_template :
the error was likely caused by the client, and [500 Internal Server Error]
if the error was likely caused by the server.
If [~debug] was passed to {!Dream.run}, [~debug_dump] will be [Some info],
where [info] is a multi-line string containing an error description, stack
[~debug_dump] is a multi-line string containing an error description, stack
trace, request state, and other information.
When an error occurs in a context where a response is not possible, the
Expand All @@ -1963,6 +1950,10 @@ val error_template :
If the template itself raises an exception or rejects, an empty [500
Internal Server Error] will be sent in contexts that require a response. *)

val debug_error_handler : error_handler
(** An {!error_handler} for showing extra information about requests and
exceptions, for use during development. *)



(** {1 Servers} *)
Expand All @@ -1971,7 +1962,6 @@ val run :
?interface:string ->
?port:int ->
?stop:unit promise ->
?debug:bool ->
?error_handler:error_handler ->
?https:bool ->
?certificate_file:string ->
Expand Down Expand Up @@ -2031,7 +2021,6 @@ val serve :
?interface:string ->
?port:int ->
?stop:unit promise ->
?debug:bool ->
?error_handler:error_handler ->
?https:bool ->
?certificate_file:string ->
Expand Down
38 changes: 15 additions & 23 deletions src/http/error_handler.ml
Expand Up @@ -157,11 +157,7 @@ let customize template (error : Dream.error) =
Lwt.return_none

else
let debug_dump =
match error.debug with
| false -> None
| true -> Some (dump error)
in
let debug_dump = dump error in

let response =
match error.condition with
Expand All @@ -187,24 +183,25 @@ let customize template (error : Dream.error) =



let default_template _error debug_dump response =
match debug_dump with
| None -> Lwt.return response
| Some debug_dump ->
let status = Dream.status response in
let code = Dream.status_to_int status
and reason = Dream.status_to_string status in
response
|> Dream.with_header "Content-Type" Dream_pure.Formats.text_html
|> Dream.with_body
(Dream__middleware.Error_template.render ~debug_dump ~code ~reason)
|> Lwt.return

let default_template _error _debug_dump response =
Lwt.return response

let debug_template _error debug_dump response =
let status = Dream.status response in
let code = Dream.status_to_int status
and reason = Dream.status_to_string status in
response
|> Dream.with_header "Content-Type" Dream_pure.Formats.text_html
|> Dream.with_body
(Dream__middleware.Error_template.render ~debug_dump ~code ~reason)
|> Lwt.return

let default =
customize default_template

let debug_error_handler =
customize debug_template



(* Error reporters (called in various places by the framework). *)
Expand Down Expand Up @@ -371,7 +368,6 @@ let httpaf
response = None;
client = Some (Adapt.address_to_string client_address);
severity;
debug = Dream.debug app;
will_send_response = true;
} in

Expand Down Expand Up @@ -430,7 +426,6 @@ let h2
response = None;
client = Some (Adapt.address_to_string client_address);
severity;
debug = Dream.debug app;
will_send_response = true;
} in

Expand Down Expand Up @@ -472,7 +467,6 @@ let tls
response = None;
client = Some (Adapt.address_to_string client_address);
severity = `Warning;
debug = Dream.debug app;
will_send_response = false;
} in

Expand Down Expand Up @@ -504,7 +498,6 @@ let websocket
response = Some response;
client = Some (Dream.client request);
severity = `Warning; (* Not sure what these errors are, yet. *)
debug = Dream.debug (Dream.app request);
will_send_response = false;
} in

Expand All @@ -527,7 +520,6 @@ let websocket_handshake
response = Some response;
client = Some (Dream.client request);
severity = `Warning;
debug = Dream.debug (Dream.app request);
will_send_response = true;
} in

Expand Down
3 changes: 2 additions & 1 deletion src/http/error_handler.mli
Expand Up @@ -13,8 +13,9 @@ module Dream = Dream_pure
templates and/or do logging. *)

val default : Dream.error_handler
val debug_error_handler : Dream.error_handler
val customize :
(Dream.error -> string option -> Dream.response -> Dream.response Lwt.t) ->
(Dream.error -> string -> Dream.response -> Dream.response Lwt.t) ->
Dream.error_handler


Expand Down
10 changes: 0 additions & 10 deletions src/http/http.ml
Expand Up @@ -749,7 +749,6 @@ let serve_with_maybe_https
~interface
~port
~stop
?debug
~error_handler
~https
?certificate_file ?key_file
Expand All @@ -760,11 +759,6 @@ let serve_with_maybe_https
let app = Dream.new_app (Error_handler.app error_handler) in

try%lwt
begin match debug with
| Some debug -> Dream.set_debug debug app
| None -> ()
end;

(* This check will at least catch secrets like "foo" when used on a public
interface. *)
(* if not (is_localhost interface) then
Expand Down Expand Up @@ -905,7 +899,6 @@ let serve
?(interface = default_interface)
?(port = default_port)
?(stop = never)
?debug
?(error_handler = Error_handler.default)
?(https = false)
?certificate_file
Expand All @@ -918,7 +911,6 @@ let serve
~interface
~port
~stop
?debug
~error_handler
~https:(if https then `OpenSSL else `No)
?certificate_file
Expand All @@ -934,7 +926,6 @@ let run
?(interface = default_interface)
?(port = default_port)
?(stop = never)
?debug
?(error_handler = Error_handler.default)
?(https = false)
?certificate_file
Expand Down Expand Up @@ -1012,7 +1003,6 @@ let run
~interface
~port
~stop
?debug
~error_handler
~https:(if https then `OpenSSL else `No)
?certificate_file ?key_file
Expand Down
2 changes: 0 additions & 2 deletions src/middleware/catch.ml
Expand Up @@ -40,7 +40,6 @@ let catch_errors next_handler request =
response = Some response;
client = Some (Dream.client request);
severity = severity;
debug = Dream.debug (Dream.app request);
will_send_response = true;
} in

Expand All @@ -64,7 +63,6 @@ let catch_errors next_handler request =
response = None;
client = Some (Dream.client request);
severity = `Error;
debug = Dream.debug (Dream.app request);
will_send_response = true;
} in

Expand Down
3 changes: 0 additions & 3 deletions src/pure/dream_pure.mli
Expand Up @@ -395,16 +395,13 @@ type error = {
response : response option;
client : string option;
severity : log_level;
debug : bool;
will_send_response : bool;
}

type error_handler = error -> response option promise

val new_app : (error -> response Lwt.t) -> app
val app : request -> app
val debug : app -> bool
val set_debug : bool -> app -> unit
val app_error_handler : app -> (error -> response promise)
val request_from_http :
app:app ->
Expand Down
9 changes: 0 additions & 9 deletions src/pure/inmost.ml
Expand Up @@ -71,7 +71,6 @@ and server = {
}

and app = {
mutable app_debug : bool;
error_handler : error -> response Lwt.t;
}

Expand Down Expand Up @@ -111,22 +110,14 @@ and error = {
| `Info
| `Debug
];
debug : bool;
will_send_response : bool;
}

let debug app =
app.app_debug

let set_debug value app =
app.app_debug <- value

(* TODO Remove. *)
let app_error_handler app =
app.error_handler

let new_app error_handler = {
app_debug = false;
error_handler;
}

Expand Down

0 comments on commit 640a1b2

Please sign in to comment.