Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple error interceptor giving an empty response #152

Closed
konr opened this issue Jul 23, 2013 · 4 comments
Closed

Simple error interceptor giving an empty response #152

konr opened this issue Jul 23, 2013 · 4 comments

Comments

@konr
Copy link

konr commented Jul 23, 2013

Here's my first attempt of a catcher interceptor on Pedestal:

(definterceptorfn catcher []
   (interceptor
   :error (fn [context error]
           {:status 500 
            :body (->> error .toString (hash-map :error) json/write-str)
            :headers {"Content-type" "application/json"}})))

As I could test, by adding (/ 1 0) to my code, the function does get called, but the client gets an empty response with status 200, instead of the response in the map. I wonder why it is so.

There is nothing fancy in my routes variable:

(defroutes routes
  [[["/api"
     ^:interceptors [(body-params/body-params) (catcher) bootstrap/html-body]
     ...
@timewald
Copy link
Contributor

Konrad,

You are returning a response map when you need to return a context map. The
context map should have a :response key whose value is the map you are
returning now, like this:

{:response {:status 500 :body ... :headers ... }}

Tim-

On Tue, Jul 23, 2013 at 4:32 PM, Konrad Scorciapino <
notifications@github.com> wrote:

Here's my first attempt of a catcher interceptor on Pedestal:

(definterceptorfn catcher [](interceptor
:error %28fn [context error]
{:status 500
:body %28->> error .toString %28hash-map :error%29 json/write-str%29
:headers {"Content-type" "application/json"}})))

As I could test, by adding (/ 1 0) to my code, the function does get
called, but the client gets an empty response with status 200, instead of
the response in the map. I wonder why it is so.

There is nothing fancy in my routes variable:

(defroutes routes
[[["/api"
^:interceptors [(body-params/body-params) (catcher) bootstrap/html-body]
...


Reply to this email directly or view it on GitHubhttps://github.com//issues/152
.

@konr
Copy link
Author

konr commented Jul 23, 2013

Hi, Tim! Thanks for the response!

Is it enough to have only the :response key on this map, though? I'm still getting a blank response with {:response {:status ... :body ... :headers ...}}

@timewald
Copy link
Contributor

Konrad,

You should assoc your response into the context passed to the error fn.
Sorry, I should have been clearer about that.

Tim-

On Tue, Jul 23, 2013 at 6:08 PM, Konrad Scorciapino <
notifications@github.com> wrote:

Hi, Tim! Thanks for the response!

Is it enough to have only the :response key on this map, though? I'm still
getting a blank response with {:response }


Reply to this email directly or view it on GitHubhttps://github.com//issues/152#issuecomment-21449858
.

@konr
Copy link
Author

konr commented Jul 24, 2013

Thanks! This fixed the problem!!

@konr konr closed this as completed Jul 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants