Builder-web: Implemented better page-not-found

This commit is contained in:
rand00 2022-02-09 16:23:05 +01:00 committed by Robur
parent 338fa9dea3
commit 550dd59a19
3 changed files with 42 additions and 4 deletions

View file

@ -97,7 +97,7 @@ let setup_app level influx port host datadir =
@@ Dream.sql_pool ("sqlite3:" ^ dbpath) @@ Dream.sql_pool ("sqlite3:" ^ dbpath)
@@ Http_status_metrics.handle @@ Http_status_metrics.handle
@@ Builder_web.add_routes datadir @@ Builder_web.add_routes datadir
@@ Dream.not_found @@ Builder_web.not_found
open Cmdliner open Cmdliner

View file

@ -59,10 +59,22 @@ let mime_lookup path =
then "application/octet-stream" then "application/octet-stream"
else Magic_mime.lookup (Fpath.to_string path) else Magic_mime.lookup (Fpath.to_string path)
let string_of_html =
Format.asprintf "%a" (Tyxml.Html.pp ())
let not_found req =
let path = "/" ^ String.concat "/" (Dream.path req) in
let referer = Dream.header "referer" req in
Views.page_not_found ~path ~referer
|> string_of_html |> Dream.html ~status:`Not_Found
let or_error_response r = let or_error_response r =
let* r = r in let* r = r in
match r with match r with
| Ok response -> Lwt.return response | Ok response -> Lwt.return response
| Error (text, `Not_Found) ->
Views.resource_not_found ~text
|> string_of_html |> Dream.html ~status:`Not_Found
| Error (text, status) -> Dream.respond ~status text | Error (text, status) -> Dream.respond ~status text
let default_log_warn ~status e = let default_log_warn ~status e =
@ -81,9 +93,6 @@ let if_error
Lwt_result.fail (message, status) Lwt_result.fail (message, status)
| Ok _ as r -> Lwt.return r | Ok _ as r -> Lwt.return r
let string_of_html =
Format.asprintf "%a" (Tyxml.Html.pp ())
let get_uuid s = let get_uuid s =
Lwt.return Lwt.return
(if String.length s = 36 then (if String.length s = 36 then

View file

@ -209,6 +209,35 @@ let artifact
txtf " (%a)" Fmt.byte_size size; txtf " (%a)" Fmt.byte_size size;
] ]
let resource_not_found ~text =
[
H.h2 ~a:[ H.a_style "padding-top: 33vh" ]
[ txtf "This resource does not exist" ];
H.p [
H.txt @@ Fmt.str "Error: '%s'" text
];
]
|> layout ~title:"Resource not found"
let page_not_found ~path ~referer =
[
H.h2 ~a:[ H.a_style "padding-top: 33vh" ]
[ txtf "This page does not exist" ];
H.p [
H.txt @@ Fmt.str "You requested the page %s" path
];
] @ (
match referer with
| None -> []
| Some prev_url -> [
H.p [
H.txt "Go back to ";
H.a ~a:H.[ a_href prev_url ] [ H.txt prev_url ];
];
]
)
|> layout ~title:"Page not found"
module Builds = struct module Builds = struct
let make_header = let make_header =