Fork me on GitHub
#reitit
<
2022-09-01
>
cvetan21:09:30

how do I return custom json 404 response from my handler function? I am using reitit swagger support, and I wrote my custom 404 function like this:

(defn not-found
  "This function returns 404 Not Found response"
  [message]
  {:status 404
   :body {:message message}})
but when I try to call this I get this error:
{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$21241/status :spec$21241/message]), :type :map, :leaf? false})",
  "problems": [
    {
      "path": [],
      "pred": "(clojure.core/fn [%] (clojure.core/contains? % :status))",
      "val": {
        "message": "Not found"
      },
      "via": [],
      "in": []
    }
  ],
  "type": "reitit.coercion/response-coercion",
  "coercion": "spec",
  "value": {
    "message": "Not found"
  },
  "in": [
    "response",
    "body"
  ]
}

rolt21:09:24

why does your spec requires status to be present in the body ?

cvetan21:09:02

I am still in process of learning, so I am trying out things

cvetan21:09:19

I wrote this utility function to return 404 response with different messages

rolt21:09:11

do you have somewhere in your route definition :response {:message string? :status int?}} ? if so just remove status

cvetan21:09:11

yes I had it

cvetan21:09:17

thank you very much

cvetan21:09:43

how to fix this,what I am doing wrong?