Harry
Harry

Reputation: 3

RIng with Coercion url parameter returning :status 500

This is my app:

(def routes
  [["/api"
    ping-routes
    submissions-routes]])

(def app
  (ring/ring-handler
    (ring/router routes
      {:data {:coercion reitit.coercion.schema/coercion
              :muuntaja m/instance
              :middleware [[wrap-cors
                            :access-control-allow-origin [#"http://localhost:4200"]
                            :access-control-allow-methods [:get :post]]
                           parameters-middleware
                           format-negotiate-middleware
                           format-response-middleware
                           exception-middleware
                           format-request-middleware
                           coerce-exceptions-middleware
                           coerce-request-middleware
                           coerce-response-middleware]}})
    (ring/routes
      (ring/redirect-trailing-slash-handler {:method :strip})
      (ring/create-default-handler
       {:not-found (constantly {:status 404 :body "Route not found"})}))))

(def ping-routes
  ["/ping" {:get (fn [req]
                   {:status 200
                    :body {:ping "pong"}})}])


(def submissions-routes
  ["/submissions"
   ["" {:get get-submissions}]
   ["/:id" {:get {:parameters {:path {:id s/Int}}
                      :handler get-submission-by-id}}]])


(comment 
  (get-submission-by-id {:parameters {:path 20}}))

When I try access "submissions/1 I get "{"type":"exception","class":"clojure.lang.ExceptionInfo"}" in the browser or within the repl:

{:status 500,
 :body
 #object[java.io.ByteArrayInputStream 0x3db2e139 "java.io.ByteArrayInputStream@3db2e139"],
 :headers {"Content-Type" "application/json; charset=utf-8"}}

If I run my comment code then the get-submission-by-id handler works fine on it's own. Thanks in advance.

Upvotes: 0

Views: 185

Answers (3)

cfrick
cfrick

Reputation: 37008

I think (and this is partially due to you not providing a proper MVCE), that you are mixing up the name spaces for Schema (schema.core) and the schema coercion from reitit (reitit.ring.coercion).

If you require:

  • reitit.coercion.schema :as rcs
  • schema.core :as s

And use :coercion rcs/coercion in your router, this problem should be solved.

Next, you should never have to guess what the problem is. Make sure, that you know, what exceptions are thrown. E.g. you can print exceptions, if you replace exception-middleware with

(rrme/create-exception-middleware
 (merge
  rrme/default-handlers
  {::rrme/wrap (fn [handler ex req]
          (prn ex)
          (handler ex req))}))

with reitit.ring.middleware.exception :as rrme in your requires.

Upvotes: 1

guest
guest

Reputation: 16

Try to define your routes more precisely with names and strict structuring, like this:

(def submissions-routes
      [["/submissions" {:name ::experiment}
        ["" {:name ::root-submission-route
             :get  {:handler (fn [_] {:status 200
                                      :body   {:resp "empty get"}})}}]
        ["/:id" {:name ::root-submission-route-id
                 :get  {:parameters {:path {:id Long}}
                        :handler    (fn [r] {:status 200 :body {:resp (-> r :path-params :id)}})}}]]])

Upvotes: 0

guest
guest

Reputation: 1

s/Int to be replaced by Long By default numbers in Java are longs.

Upvotes: 0

Related Questions