How to return a Failure from a RestHelper block?

112 views
Skip to first unread message

Dan Gravell

unread,
Oct 9, 2012, 6:33:35 AM10/9/12
to lif...@googlegroups.com
I've managed to follow the REST docs so far, but now I want to add a piece of validation that rejects unrecognised Req params. I couldn't see anything that did this in the docs, only the enforcement of manadatory params.

When I try to return a:

ParamFailure("Unrecognized parameters: " + illegalParams, 400)

I get:

[error] /home/gravelld/scala/lift/d6y-lift_24_sbt-0d46974/src/main/scala/com/onemusicapi/api/Api.scala:37: ambiguous implicit values:
[error]  both method conforms in object Predef of type [A]=> <:<[A,A]
[error]  and lazy value convertJsonXmlAble in trait RestHelper of type => PartialFunction[(net.liftweb.http.rest.JsonXmlSelect, net.liftweb.http.rest.JsonXmlAble, net.liftweb.http.Req),net.liftweb.http.LiftResponse]
[error]  match expected type Nothing => net.liftweb.http.LiftResponse
[error]            case _ =>  ParamFailure("Unrecognized parameters: " + illegalParams, 400)
[error]                                   ^

Wider context:

        val illegalParams = S.request.map(_.params.keySet).getOrElse(Set.empty).diff(PARAMS)
        illegalParams toSeq match {
          case Seq() => {
              /* serve request */ : JValue
          } 
          case _ =>  ParamFailure("Unrecognized parameters: " + illegalParams, 400)
        }

I've tried different incantations of Boxes, Seqs and the like but no good.

Dan

David Pollak

unread,
Oct 9, 2012, 10:25:01 AM10/9/12
to lif...@googlegroups.com
You can't mix JValue and Failure. If you're going to return a Failure, the return type must be Box[JValue].

In your Seq() block, return a Full(JValue) and the compiler will choose the correct implicit conversion.

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 



--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net


Dan Gravell

unread,
Oct 10, 2012, 5:08:53 AM10/10/12
to lif...@googlegroups.com
Thanks David. Strange though, I still get the same thing. I've simplified the code right down:

        val illegalParams = S.request.map(_.params.keySet).getOrElse(Set.empty).diff(PARAMS)
        illegalParams toSeq match {
          case Seq() => {
       Full(JString("hi"))
          } 
          case _ =>  ParamFailure("Unrecognized parameters: " + illegalParams, 400)
        }

[error] /home/gravelld/scala/lift/d6y-lift_24_sbt-0d46974/src/main/scala/com/onemusicapi/api/Api.scala:30: ambiguous implicit values:
[error]  both method conforms in object Predef of type [A]=> <:<[A,A]
[error]  and lazy value convertJsonXmlAble in trait RestHelper of type => PartialFunction[(net.liftweb.http.rest.JsonXmlSelect, net.liftweb.http.rest.JsonXmlAble, net.liftweb.http.Req),net.liftweb.http.LiftResponse]
[error]  match expected type Nothing => net.liftweb.http.LiftResponse
[error]           case _ =>  ParamFailure("Unrecognized parameters: " + illegalParams, 400)
[error]                                  ^
[error] one error found

Maybe I'm missing an import? Apologies if I'm doing something daft.

Dan

David Pollak

unread,
Oct 10, 2012, 12:18:35 PM10/10/12
to lif...@googlegroups.com
Sometimes the Scala type inferencer is so weak, it's unbelievable. This code works:

import net.liftweb.http.rest._
import net.liftweb.json._
import net.liftweb.common._

object MyRestThing extends RestHelper {
    serve {
        case "foo" :: x Get _ =>
          (x match {
              case Nil => Full(JString("Hi"))
              case _ => ParamFailure("Hmmm... "+x, 400)
          }) : Box[JValue]
    }
}

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
 
 

Naftoli Gugenheim

unread,
Oct 12, 2012, 1:05:57 AM10/12/12
to lif...@googlegroups.com
Can you file a bug?

Dan Gravell

unread,
Oct 12, 2012, 4:51:23 AM10/12/12
to lif...@googlegroups.com
It's a bug? I thought it was something I was doing wrong with how I declared the types so the wrong implicit was being picked up... or something.
Reply all
Reply to author
Forward
0 new messages