0

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.

Harry
  • 3
  • 2

3 Answers3

1

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.

cfrick
  • 35,203
  • 6
  • 56
  • 68
0

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

guest
  • 1
  • Still getting the same error unfortunately – Harry Apr 24 '23 at 08:50
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '23 at 13:48
0

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)}})}}]]])
guest
  • 16
  • Thanks! This helped a bunch turns out I wasn't getting the parameters out of the arguments correctly. – Harry Apr 25 '23 at 01:23