Using a Box / Failure and how to set HTTP status code

101 views
Skip to first unread message

bg

unread,
Oct 8, 2012, 8:09:30 AM10/8/12
to lif...@googlegroups.com

Hi all,

Three questions ->

1) Is this an appropriate way to use Box? 
2) My nested matches in userAllowed, are feeling alot like a bunch of if statements, so any more concise way to express all those checks?
3) How do I set the HTTP status of the response? I can see some examples around status but haven't been able to find exactly how I would respond with a specifc code (like a 401) for a specific condition.

val return_string = resourceAction.userAllowed("addConnection") match {
      case Full(true) => {
        UserConnectionHelper.addUserConnection(Session.currentUser.get.pk, badgeNumber)
        "this will return the JSON of the new connection to be rendered in the browser"
      }
      case Failure(msg, exception, failure) => msg
    }

    Full(JsonResponse(JString(return_string)))
}

userAllowed

//This is just checking that a bunch of criteria are met, and needs to return a specific message for the one that isn't

def userAllowed(action: String): Box[Boolean] = {
  
  action match {
      case "addConnection" => {

        Session.defaultEvent match {
          case Some(event) => event.name.get match {

            case "XYZ" => Session.currentUser match { //must be a XYZ event
              case Some(user) => { ////user must be logged in

                user.type.get match {
                  case "PQR" => Full(true) //must be logged in
                  case _ => Failure("addConnection not allowed for users of type " + user.avatar.get)
              }
              case None => Failure("addConnection only allowed for logged in users")
....
....

David Pollak

unread,
Oct 8, 2012, 5:49:55 PM10/8/12
to lif...@googlegroups.com
Please do not use pattern matching, please use for comprehensions.

Please see http://lift.la/scala-option-lift-box-and-how-to-make-your-co

Specifically, please use for comprehensions.



--
--
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


Naftoli Gugenheim

unread,
Oct 12, 2012, 12:14:53 AM10/12/12
to lif...@googlegroups.com

On Mon, Oct 8, 2012 at 5:49 PM, David Pollak <feeder.of...@gmail.com> wrote:
Please do not use pattern matching, please use for comprehensions.

Why? And, how do you handle the Failure case?

Maarten Koopmans

unread,
Oct 12, 2012, 5:09:14 AM10/12/12
to lif...@googlegroups.com
This returns "extracted" wrapped in a Box[SomeType] assuming that SomeValue is if Box[SomeType]:

for ( extracted <- SomeValue ?~ "param failure error message" ~> 401) yield extracted

Of course you can add processing logic in the yield using a helper function (so you get yield f(extracted))

Does this help?

--Maarten

bg

unread,
Oct 12, 2012, 10:15:04 AM10/12/12
to lif...@googlegroups.com
Hi guys,

How would I get the message in the "Failure" using a for comprehension.

In my first post - userAllowed functions does some checking, and has different messages for different scenarios?

for ( extracted <- SomeValue ?~ "param failure error message" # instead of writing this message, can I look at the Failure and get the message returned with it? # ~> 401) yield extracted

David Pollak

unread,
Oct 12, 2012, 11:59:47 AM10/12/12
to lif...@googlegroups.com
for {
  user <- User.find.filter(_.isTheRightUser) ?~ "Wrong user" ~> 407
} yield user.name
--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net

bg

unread,
Oct 13, 2012, 12:11:23 PM10/13/12
to lif...@googlegroups.com
Sorry guys - I'm still not quite understanding what I need to. 

David - as I read your example - the error message is still being created after a Failure is returned. I would like to have "userDoes" as a helper function which checks multiple conditions, and the appropriate error can be extracted from the Failure that is returned. 

I tried the below - but get a compilation error

for {
      allowed <- resourceAction.userDoes("addConnection") ?~ _.msg ~> 407
    } yield {
      ...
missing parameter type for expanded function ((x$4) => resourceAction.userDoes("addConnection").$qmark$tilde(x$4.msg).$tilde$greater(407))
[error]       allowed <- resourceAction.userDoes("addConnection") ?~ _.msg ~> 407

Naftoli Gugenheim

unread,
Oct 13, 2012, 11:03:41 PM10/13/12
to lif...@googlegroups.com
I see now there was some miscommunication. When DPP said to use for comprehensions, he was answering question #2. So the rule of thumb is: if all your pattern match cases return a Box, you can probably unify them with for comprehensions. However in the first snippet, you need the pattern match, since the expression has to evaluate to a string. openOr won't help since you need to extract the Failure's msg.


Naftoli Gugenheim

unread,
Oct 13, 2012, 11:05:13 PM10/13/12
to lif...@googlegroups.com
On Mon, Oct 8, 2012 at 8:09 AM, bg <brent...@gmail.com> wrote:
3) How do I set the HTTP status of the response? I can see some examples around status but haven't been able to find exactly how I would respond with a specifc code (like a 401) for a specific condition.


 Full(JsonResponse(JString(return_string), 401))

Reply all
Reply to author
Forward
0 new messages