refactor extract error page into dedicated views/error.ml module

Moves the error page rendering out of layout.ml into its own Error module. The public API (Views.error_page) is unchanged.

Commit
da2dd19816aa33be749fd48a33f99eca38733690
Author
Marius Peter <dev@marius-peter.com>
Author date
Committer
Marius Peter <dev@marius-peter.com>
Committer date
Changed files
lib/views.ml
index d7be46f9..3c185c9a 100644..100644
@@ -2,7 +2,7 @@
2 2
3 3 (** View layer — re-exports layout and page modules. *)
4 4
5 Removed: let error_page = Layout.error_page
5 Added: let error_page = Error.render
6 6 let root = Root.render
7 7
8 8 module Repo = struct
lib/views/error.ml
index 00000000..7622444e 000000..100644
@@ -0,0 +1,58 @@
1 Added: (* -*- mode: tuareg; -*- *)
2 Added:
3 Added: open Dream_html
4 Added:
5 Added: let hint_of_status status =
6 Added: match Dream.status_to_int status with
7 Added: | 400 ->
8 Added: "The request could not be understood. Check the URL for typos or invalid \
9 Added: characters."
10 Added: | 404 ->
11 Added: "The page or resource you are looking for does not exist. It may have \
12 Added: been moved or deleted."
13 Added: | 500 ->
14 Added: "Something went wrong on the server. This is not your fault — try again \
15 Added: later."
16 Added: | code when code >= 400 && code < 500 ->
17 Added: "The request could not be completed. Check the URL and try again."
18 Added: | code when code >= 500 ->
19 Added: "The server encountered an unexpected condition. Try again later."
20 Added: | _ -> ""
21 Added:
22 Added: let render ?(title = "Request failed") ?(status = `Internal_Server_Error)
23 Added: message =
24 Added: let page_title = title in
25 Added: let status_code = Dream.status_to_int status |> string_of_int in
26 Added: let hint = hint_of_status status in
27 Added: let open HTML in
28 Added: respond ~status
29 Added: @@ html []
30 Added: [
31 Added: head []
32 Added: [
33 Added: HTML.title [] "%s" page_title;
34 Added: meta
35 Added: [
36 Added: name "viewport"; content "width=device-width, initial-scale=1";
37 Added: ];
38 Added: link [ rel "stylesheet"; href "/static/styles.css" ];
39 Added: ];
40 Added: body []
41 Added: [
42 Added: header
43 Added: [ id "error-header" ]
44 Added: [
45 Added: span [ class_ "error-code" ] [ txt "%s" status_code ];
46 Added: h1 [] [ txt "%s" page_title ];
47 Added: ];
48 Added: div
49 Added: [ id "main" ]
50 Added: [
51 Added: p [ class_ "error-hint" ] [ txt "%s" hint ];
52 Added: p [ class_ "error-detail" ] [ txt "%s" message ];
53 Added: p
54 Added: [ class_ "error-nav" ]
55 Added: [ a [ href "/" ] [ txt "Return to the repository list" ] ];
56 Added: ];
57 Added: ];
58 Added: ]
lib/views/layout.ml
index 5775c302..d933697d 100644..100644
@@ -132,59 +132,3 @@
132 132 HTML.html
133 133 [ HTML.lang "en" ]
134 134 [ head page_title; body ~user ~root_title body_data ]
135 Removed:
136 Removed: let error_page ?(title = "Request failed") ?(status = `Internal_Server_Error)
137 Removed: message =
138 Removed: let page_title = title in
139 Removed: let status_code = Dream.status_to_int status |> string_of_int in
140 Removed: let hint =
141 Removed: match Dream.status_to_int status with
142 Removed: | 400 ->
143 Removed: "The request could not be understood. Check the URL for typos or \
144 Removed: invalid characters."
145 Removed: | 404 ->
146 Removed: "The page or resource you are looking for does not exist. It may have \
147 Removed: been moved or deleted."
148 Removed: | 500 ->
149 Removed: "Something went wrong on the server. This is not your fault — try \
150 Removed: again later."
151 Removed: | code when code >= 400 && code < 500 ->
152 Removed: "The request could not be completed. Check the URL and try again."
153 Removed: | code when code >= 500 ->
154 Removed: "The server encountered an unexpected condition. Try again later."
155 Removed: | _ -> ""
156 Removed: in
157 Removed: let open HTML in
158 Removed: respond ~status
159 Removed: @@ html []
160 Removed: [
161 Removed: head []
162 Removed: [
163 Removed: HTML.title [] "%s" page_title;
164 Removed: meta
165 Removed: [
166 Removed: name "viewport"; content "width=device-width, initial-scale=1";
167 Removed: ];
168 Removed: link [ rel "stylesheet"; href "/static/styles.css" ];
169 Removed: ];
170 Removed: body []
171 Removed: [
172 Removed: header
173 Removed: [ id "error-header" ]
174 Removed: [
175 Removed: span [ class_ "error-code" ] [ txt "%s" status_code ];
176 Removed: h1 [] [ txt "%s" page_title ];
177 Removed: ];
178 Removed: div
179 Removed: [ id "main" ]
180 Removed: ([
181 Removed: p [ class_ "error-hint" ] [ txt "%s" hint ];
182 Removed: p [ class_ "error-detail" ] [ txt "%s" message ];
183 Removed: ]
184 Removed: @ [
185 Removed: p
186 Removed: [ class_ "error-nav" ]
187 Removed: [ a [ href "/" ] [ txt "Return to the repository list" ] ];
188 Removed: ]);
189 Removed: ];
190 Removed: ]