Make HTTP handler return feed and catch basic errors

This commit is contained in:
Felisp 2023-12-24 00:20:52 +01:00
parent 2173d6f7fa
commit f36f5be5d0

View file

@ -99,9 +99,47 @@
rss-thread-watch.watcher.chod-threads-cache
rss-thread-watch.core.CONFIG"
[rqst]
(try (let [{{chod :chod :or {chod 60}
:as prms} :params
uri :uri} rqst
queries (if (vector? (prms "q")) (prms "q") [(prms "q")]) ; to always return vector
repeat? (prms "repeat")
real-chod (if (or (vector? chod)
(< chod 60))
94 chod) ;Never accept chod lower that 60 TODO: don't hardcode this
cache @watcher/chod-threads-cache
]
;; ====== Errors =====
;; Something other than feed.xml requested
(when-not (s/ends-with? uri "feed.xml")
(throw (ex-info "404" {:status 404
:header {"Content-Type" "text/plain"}
:body "404 This server has nothing but /feed.xml"})))
;; No querry specified - don't know what to search for
(when-not (prms "q")
(throw (ex-info "400" {:status 400
:header {"Content-Type" "text/plain"}
:body (str "400 You MUST specify query with one OR more'q=searchTerm' url parameter(s)\n\n\n"
"Exmple: '/feed.xml?q=pony&q=IWTCIRD' will show in your feed all threads with 'pony' or 'IWTCIRD'"
" in their title that are about to die.")})))
(when (empty? cache)
(throw (ex-info "503" {:status 503
:header {"Content-Type" "text/plain"}
:body (str "503 Service Unavailable\n"
"Cache is empty, cannot generate feed. Try again later, it may work.")})))
;; ==== Everything good ====
;; (println "RCVD: " rqst)
{:status 200
;; There shouldn't be any problems with this mime type but if there are
;; replace with "text/xml", or even better, get RSS reader that is not utter shit
:header {"Content-Type" "application/rss+xml"}
:body "All pony here ^:)"})
:body (generate-feed queries real-chod repeat? cache)})
(catch Exception e
;; Ex-info has been crafted to match HTTP response body so we can send it
(if-let [caught (ex-data e)] ;Tam bude ale vždycky ex-data myslím, to chce čekovat jestli t obsahuje nějaký klíč (body? at nemusí být nějaký extra)
caught ;We have custom crafted error
{:status 500 ;Something else fucked up, we print what happened
:header {"Content-Type" "text/plain"}
:body (str "500 - Something fucked up while generating feed, If you decide to report it, please include url adress you used:\n"
(ex-cause e) "\n"
e)}))))