How to return a Failure from a RestHelper block?

112 zobrazení
Přeskočit na první nepřečtenou zprávu

Dan Gravell

nepřečteno,
9. 10. 2012 6:33:3509.10.12
komu: 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

nepřečteno,
9. 10. 2012 10:25:0109.10.12
komu: 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

nepřečteno,
10. 10. 2012 5:08:5310.10.12
komu: 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

nepřečteno,
10. 10. 2012 12:18:3510.10.12
komu: 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

nepřečteno,
12. 10. 2012 1:05:5712.10.12
komu: lif...@googlegroups.com
Can you file a bug?

Dan Gravell

nepřečteno,
12. 10. 2012 4:51:2312.10.12
komu: 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.
Odpovědět všem
Odpověď autorovi
Přeposlat
0 nových zpráv