This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-16
Channels
- # adventofcode (24)
- # announcements (3)
- # aws (3)
- # babashka (16)
- # beginners (88)
- # biff (5)
- # calva (27)
- # cider (15)
- # cljs-dev (70)
- # clojure (87)
- # clojure-austin (3)
- # clojure-belgium (6)
- # clojure-europe (59)
- # clojure-nl (1)
- # clojure-norway (14)
- # clojure-uk (3)
- # clojurescript (37)
- # data-science (2)
- # datalevin (40)
- # datomic (1)
- # emacs (23)
- # events (2)
- # graalvm (13)
- # graphql (7)
- # gratitude (1)
- # holy-lambda (193)
- # inf-clojure (15)
- # lsp (27)
- # malli (9)
- # off-topic (20)
- # polylith (6)
- # reitit (29)
- # releases (2)
- # scittle (13)
- # shadow-cljs (51)
- # transit (15)
- # xtdb (29)
Up till now i was using compojure and handlers were responsible for too many things • input validation • generating 200 response • 400 response And i had middleware in case exceptions happen to return 500. Having switched to reitit i am unable to cover the status 400.
{:summary "Gives you link for specific match highlights"
:description "Once post-match has been processed and is shown in Dashboard you may fetch best moments of the game"
:parameters {:query (v/get-schema :match-highlights)}
:responses {200 {:headers any?
:body {:link string?}}}
:handler (fn [{{data :query} :parameters}]
(handlers/match-highlights data))}
If exception is thrown in handler -> i get 500. Which is expected
If handler returns status 400 bad request -> response is again 500 (default exception handler)
I couldn't find examples online how to do it, do you have any advice?based on the tests, it seems that there is a way for the 400 to be preserved https://github.com/metosin/reitit/blob/198cfda00d20093f3d7b3069e5e902835c396698/test/clj/reitit/ring/middleware/exception_test.clj#L100
the answer will be somewhere in one of these search results 😄 https://github.com/metosin/reitit/search?q=406
use the source, Luke -lightsaber emoji-
alright so my default ring handler picked this up and that's where i got my response.
Thanks for that.
with regards to my original question it appears that i need the only think i need to use is exception/exception-middleware
and it will work as i expect it to work which is great.
The only problem that remains is that i would like to show only 'humanized' from the response maybe others but for sure not schema
{
"schema": "[:map {:closed true} [:match-id [:fn {:swagger/description \"Match id\", :swagger/default \"21c0d56a-4360-45d7-a919-0ae89a35e062\", :swagger/type \"string\", :swagger/format \"uuid\", :decode/string #function[highlights.validation/fn--63082], :decode/json #function[highlights.validation/fn--63084], :error/message \"Invalid match-id\"} #function[clojure.core/uuid?]]] [:date [:fn {:swagger/description \"Date when the match happened\", :swagger/default \"2022-11-28\", :swagger/type \"string\", :swagger/format \"date\", :swagger/pattern #\"^\\d{4}\\-(0[1-9]|1[012])\\-(0[1-9]|[12][0-9]|3[01])$\", :error/message \"Invalid date\"} #function[highlights.validation/valid-date-as-str?]]]]",
"errors": [
{
"path": [
"date"
],
"in": [
"date"
],
"schema": "[:fn {:swagger/description \"Date when the match happened\", :swagger/default \"2022-11-28\", :swagger/type \"string\", :swagger/format \"date\", :swagger/pattern #\"^\\d{4}\\-(0[1-9]|1[012])\\-(0[1-9]|[12][0-9]|3[01])$\", :error/message \"Invalid date\"} #function[highlights.validation/valid-date-as-str?]]",
"value": "2022-11-281",
"message": "Invalid date"
}
],
"value": {
"date": "2022-11-281",
"matchId": "21c0d56a-4360-45d7-a919-0ae89a35e062"
},
"coercion": "malli",
"in": [
"request",
"query-params"
],
"humanized": {
"date": [
"Invalid date"
]
}
}
alright future reference for someone How to select only part (keys you are interested in) of the coercion response which is responsible for returning 404 in case schema validation doesn't pass. Essential how to modify 404 response 1. check source code and try to understand it 2. use this dirty version of code
(fn [a b]
(let [res ((exception/create-coercion-handler 400) a b)]
(timbre/error res)
(update res :body #(select-keys % [:in :humanized]))))
3. clean it up
4. have fun
Thanks for the time and help @U0509NKGK 🙇welcome! enjoy!
Actually proper way to do this is with
:data :coersion
(reitit.coercion.malli/create
{ ;; set of keys to include in error messages
:error-keys #{#_:type #_:coercion :in #_:schema #_:value #_:errors :humanized #_:transformed}
;; schema identity function (default: close all map schemas)
:compile mu/closed-schema
;; strip-extra-keys (effects only predefined transformers)
:strip-extra-keys true
;; add/set default values
:default-values true
;; malli options
:options nil})
just pick and choose which keys you want in responseIf you're interested, i have an example of using my own custome exception middleware to return different responses
thanks @U11EL3P9U this is super useful
i wish i found this earlier 😄 i couldn't find any real world example of reitit kinda like https://github.com/polymeris/re-frame-realword-example-app
We’re happy to take PR’s of helpful examples! Currently we have the examples
folder in the repo and a list of external resources in the README. Updates to either or both will be much appreciated 🙏
https://github.com/metosin/reitit#more-examples
https://github.com/metosin/reitit#external-resources
@U45SLGVHV I've added in basic and token authorization! Enjoy!
@U11EL3P9U i can't seem to find it https://github.com/dharrigan/startrek/tree/master/src/startrek/api/middleware Also there are no commits related to auth 😕 Am i looking at the wrong place?
bear in mind, that is just my one way of doings things, others may do it differently and perhaps you'll discover your own (better!!!) way 🙂
Thanks @U11EL3P9U i shall check it out!
We’re happy to take PR’s of helpful examples! Currently we have the examples
folder in the repo and a list of external resources in the README. Updates to either or both will be much appreciated 🙏
https://github.com/metosin/reitit#more-examples
https://github.com/metosin/reitit#external-resources