How to return a Failure from a RestHelper block?

Showing 1-6 of 6 messages
How to return a Failure from a RestHelper block? Dan Gravell 10/9/12 3:33 AM
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
Re: [Lift] How to return a Failure from a RestHelper block? David Pollak 10/9/12 7:25 AM
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


Re: [Lift] How to return a Failure from a RestHelper block? Dan Gravell 10/10/12 2:08 AM
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
Re: [Lift] How to return a Failure from a RestHelper block? David Pollak 10/10/12 9:18 AM
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
 
 
 



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


This message has been hidden because it was flagged for abuse.
Re: [Lift] How to return a Failure from a RestHelper block? Dan Gravell 10/12/12 1:51 AM
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.