package sihl
Source file web_error.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146(* This implementation is based on https://github.com/rgrinberg/opium/blob/master/opium/src/middlewares/middleware_debugger.ml but it removes the detailed error message to prevent leaking information. *) let log_src = Logs.Src.create "sihl.middleware.error" module Logs = (val Logs.src_log log_src : Logs.LOG) let page request_id = Format.asprintf {| <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>Internal Server Error</title> <style> %s </style> </head> <body class="antialiased"> <div class="py-4 sm:py-12"> <div class="max-w-8xl mx-auto px-4 sm:px-6 lg:px-8"> <h2 class="text-2xl leading-8 font-semibold font-display text-gray-900 sm:text-3xl sm:leading-9"> Internal Server Error </h2> <div class="mt-0 mb-4 text-gray-600"> An error has been caught while handling the request. </div> <p> Our administrators have been notified. Please note your request ID <b>%s</b> when contacting us. </p> </div> </div> </body> </html> |} Web.style request_id ;; let site_error_handler req = let request_id = Web_id.find req |> Option.value ~default:"-" in let site = page request_id in Opium.Response.of_plain_text site |> Opium.Response.set_content_type "text/html; charset=utf-8" |> Lwt.return ;; let json_error_handler req = let request_id = Web_id.find req |> Option.value ~default:"-" in let msg = Format.sprintf "Something went wrong, our administrators have been notified." in let body = Format.sprintf {|"{"errors": ["%s"], "request_id": "%s"}"|} msg request_id in Opium.Response.of_plain_text body |> Opium.Response.set_content_type "application/json; charset=utf-8" |> Opium.Response.set_status `Internal_server_error |> Lwt.return ;; let exn_to_string exn req = let msg = Printexc.to_string exn and stack = Printexc.get_backtrace () in let request_id = Web_id.find req |> Option.value ~default:"-" in let req_str = Format.asprintf "%a" Opium.Request.pp_hum req in Format.asprintf "Request id %s: %s\nError: %s\nStacktrace: %s" request_id req_str msg stack ;; let create_error_email (sender, recipient) error = Contract_email.create ~sender ~recipient ~subject:"Exception caught" error ;; let middleware ?email_config ?(reporter = fun _ _ -> Lwt.return ()) ?error_handler () = let filter handler req = Lwt.catch (fun () -> handler req) (fun exn -> (* Make sure to Lwt.catch everything that might go wrong. *) (* Log the error *) let error = exn_to_string exn req in Logs.err (fun m -> m "%s" error); (* Report error via email, don't wait for it.*) let _ = match email_config with | Some (sender, recipient, send_fn) -> let email = create_error_email (sender, recipient) error in Lwt.catch (fun () -> send_fn email) (fun exn -> let msg = Printexc.to_string exn in Logs.err (fun m -> m "Failed to report error per email: %s" msg); Lwt.return ()) | _ -> Lwt.return () in (* Use custom reporter to catch error, don't wait for it. *) let _ = Lwt.catch (fun () -> reporter req error) (fun exn -> let msg = Printexc.to_string exn in Logs.err (fun m -> m "Failed to run custom error reporter: %s" msg); Lwt.return ()) in let content_type = try req |> Opium.Request.header "Content-Type" |> Option.map (String.split_on_char ';') |> Option.map List.hd with | _ -> None in match error_handler with | Some error_handler -> error_handler req | None -> (match content_type with | Some "application/json" -> json_error_handler req (* Default to text/html *) | _ -> site_error_handler req)) in (* In a production setting we don't want to use the built in debugger middleware of opium. It is useful for development but it exposed too much information. *) if Core_configuration.is_production () then Rock.Middleware.create ~name:"error" ~filter else Opium.Middleware.debugger ;;