Session problem

12 views
Skip to first unread message

Oleg

unread,
Jun 30, 2010, 6:30:54 AM6/30/10
to Compojure, ring-c...@googlegroups.com
I've got this error message:

2010-06-30 14:28:02.808::WARN: EXCEPTION
java.lang.NullPointerException
at ring.middleware.session$wrap_session$fn__4514.invoke(session.clj:
47)
at ring.middleware.cookies$wrap_cookies$fn__4276.invoke(cookies.clj:
124)
at compojure.core$routes$fn__4435$fn__4436.invoke(core.clj:71)
at clojure.core$some.invoke(core.clj:2041)
at compojure.core$routes$fn__4435.invoke(core.clj:71)
at ring.middleware.params$wrap_params$fn__4180.invoke(params.clj:76)
at ring.middleware.cookies$wrap_cookies$fn__4276.invoke(cookies.clj:
124)
at clojure.lang.Var.invoke(Var.java:365)

It begins with wrapping my handlers with wrap-session middleware.

Here's little source snippet to reproduce error (some code was
truncated):

(ns aeon.server
(:require (aeon [config :as config]
[error :as error]
[layout :as layout]
[flexible :as flexible]
[middleware :as mw])
(ring.adapter [jetty :as jetty])
(ring.util [response :as response])
(ring.middleware session file-info))
(:use (compojure [core :only [defroutes GET POST ANY wrap!]])))

(defn- tmp-handler [id session]
(let [old-id (:old-id session)]
{:session {:old-id id}
:body
(str "Hello you have requested template #" id
"<br/> Last time you have asked for #" old-id)}))

(defroutes template-routes
(GET "/template/:id/" {{id "id"} :params session :session}
(tmp-handler id session)))

(wrap! template-routes
mw/wrap-layout
ring.middleware.session/wrap-session)

(defroutes all-routes
template-routes)

(defonce server (jetty/run-jetty #'all-routes {:port 8080 :host
"localhost" :join? false}))

(defn restart []
(.stop server)
(.start server))


- Oleg

James Reeves

unread,
Jun 30, 2010, 7:42:50 AM6/30/10
to ring-c...@googlegroups.com, Compojure
On 30 June 2010 11:30, Oleg <oleg.r...@gmail.com> wrote:
> (defroutes template-routes
>  (GET "/template/:id/" {{id "id"} :params session :session}
>       (tmp-handler id session)))
>
> (wrap! template-routes
>       mw/wrap-layout
>       ring.middleware.session/wrap-session)

Currently, if you're going to wrap a handler in the session
middleware, you need to ensure it does not return nil. I guess I'll
add something in to fix this in Ring, but in general, Ring middleware
expects a valid response map, whilst Compojure will return nil if no
route matches.

Because you haven't added a catch-all 404 route, you'll get that
NullPointerException every time someone accesses a resource other than
"/template/:id/". For instance, perhaps the browser is attempting to
retrieve the favicon.

- James

Reply all
Reply to author
Forward
0 new messages