Вопрос

Lift code in Boot.scala:

Menu.i("Topic") / "topic" >> If(() => false, "myerror")

From the document of If:

/**
 * If the test returns True, the page can be accessed, otherwise,
 * the result of FailMsg will be sent as a response to the browser.
 * If the Loc cannot be accessed, it will not be displayed in menus.
 *
 * @param test -- the function that tests access to the page
 * @param failMsg -- what to return the the browser (e.g., 304, etc.) if
 * the page is accessed.
 */
case class If(test: () => Boolean, failMsg: FailMsg) extends AnyLocParam

It says: otherwise, the result of FailMsg will be sent as a response to the browser. So I expect it returns a http code of 5xx with error message myerror, but it not, it redirects to the index page / instead.

And with curl:

➜  ~  curl http://localhost:8080/topic -I
HTTP/1.1 302 Found
Set-Cookie: JSESSIONID=5gqkx8azu8gh1u3avyjds3wl;Path=/
Location: /
Expires: Tue, 16 Jul 2013 05:18:02 GMT
Content-Length: 0
Cache-Control: no-cache, private, no-store
Content-Type: text/plain
Pragma: no-cache
Date: Tue, 16 Jul 2013 05:18:02 GMT
X-Lift-Version: 2.5
Server: Jetty(8.1.7.v20120910)

Why it returns 302? And where is my error message myerror?

Это было полезно?

Решение

The reason you're seeing a redirect is that there's an implicit conversion from a String to a redirect to / (by default) with a Lift error notice set.

The string is used as the value of the error notice, which you can see displayed if you have Lift notices included on the page. It looks like the implicit is called strToFailMsg.

The recipe Access Restriction by HTTP Header in the Lift Cookbook touches on this briefly.

Другие советы

Menu.i("Topic") / "topic" >> If(() => false, RedirectResponse("/"))

What Lift version are you using?

In both Lift 2.4 and Lift 2.5, FailMsg is a alias of () ⇒ LiftResponse, so just simply provide it with NotFoundResponse("myerror") should work.

Menu.i("Topic") / "topic" >> If(() => false, () => net.liftweb.http.NotFoundResponse("myerror"))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top