Use custom 404 page, halt on 404 instead of resp

This commit is contained in:
Zed 2019-10-07 16:47:53 +02:00
parent 8fcdfa744a
commit ebb89edef6
7 changed files with 12 additions and 9 deletions

View File

@ -38,6 +38,9 @@ routes:
get "/help": get "/help":
redirect("/about") redirect("/about")
error Http404:
resp showError("Page not found", cfg.title)
extend unsupported, "" extend unsupported, ""
extend preferences, "" extend preferences, ""
extend rss, "" extend rss, ""

View File

@ -8,7 +8,7 @@ import ../views/[general, timeline, list]
template respList*(list, timeline: typed) = template respList*(list, timeline: typed) =
if list.minId.len == 0: if list.minId.len == 0:
resp Http404, showError("List \"" & @"list" & "\" not found", cfg.title) halt Http404, showError("List \"" & @"list" & "\" not found", cfg.title)
let html = renderList(timeline, list.query, @"name", @"list") let html = renderList(timeline, list.query, @"name", @"list")
let rss = "/$1/lists/$2/rss" % [@"name", @"list"] let rss = "/$1/lists/$2/rss" % [@"name", @"list"]
resp renderMain(html, request, cfg.title, rss=rss) resp renderMain(html, request, cfg.title, rss=rss)

View File

@ -33,7 +33,7 @@ proc createMediaRouter*(cfg: Config) =
discard discard
if not existsFile(filename): if not existsFile(filename):
resp Http404 halt Http404
let file = openAsync(filename) let file = openAsync(filename)
let buf = await readAll(file) let buf = await readAll(file)
@ -58,7 +58,7 @@ proc createMediaRouter*(cfg: Config) =
discard discard
if content.len == 0: if content.len == 0:
resp Http404 halt Http404
resp content, mimetype(url) resp content, mimetype(url)

View File

@ -15,7 +15,7 @@ proc showRss*(name: string; query: Query): Future[string] {.async.} =
template respRss*(rss: typed) = template respRss*(rss: typed) =
if rss.len == 0: if rss.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title) halt Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
resp rss, "application/rss+xml;charset=utf-8" resp rss, "application/rss+xml;charset=utf-8"
proc createRssRouter*(cfg: Config) = proc createRssRouter*(cfg: Config) =

View File

@ -29,7 +29,7 @@ proc createSearchRouter*(cfg: Config) =
resp renderMain(renderTweetSearch(tweets, prefs, getPath()), request, resp renderMain(renderTweetSearch(tweets, prefs, getPath()), request,
cfg.title, rss=rss) cfg.title, rss=rss)
else: else:
resp Http404, showError("Invalid search.", cfg.title) halt Http404, showError("Invalid search", cfg.title)
get "/hashtag/@hash": get "/hashtag/@hash":
redirect("/search?q=" & encodeUrl("#" & @"hash")) redirect("/search?q=" & encodeUrl("#" & @"hash"))

View File

@ -19,10 +19,10 @@ proc createStatusRouter*(cfg: Config) =
let conversation = await getTweet(@"name", @"id", @"after", getAgent()) let conversation = await getTweet(@"name", @"id", @"after", getAgent())
if conversation == nil or conversation.tweet.id.len == 0: if conversation == nil or conversation.tweet.id.len == 0:
var error = "Tweet not found"
if conversation != nil and conversation.tweet.tombstone.len > 0: if conversation != nil and conversation.tweet.tombstone.len > 0:
resp Http404, showError(conversation.tweet.tombstone, cfg.title) error = conversation.tweet.tombstone
else: halt Http404, showError(error, cfg.title)
resp Http404, showError("Tweet not found", cfg.title)
let let
title = pageTitle(conversation.tweet.profile) title = pageTitle(conversation.tweet.profile)

View File

@ -72,7 +72,7 @@ proc showTimeline*(request: Request; query: Query; title, rss: string): Future[s
template respTimeline*(timeline: typed) = template respTimeline*(timeline: typed) =
if timeline.len == 0: if timeline.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title) halt Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
resp timeline resp timeline
proc createTimelineRouter*(cfg: Config) = proc createTimelineRouter*(cfg: Config) =