On Fri, Jul 10, 2009 at 1:39 PM, marius d. <[email protected]> wrote:

>
>
>
> On Jul 10, 11:11 pm, Jeppe Nejsum Madsen <[email protected]> wrote:
> > Hi,
> >
> > I understand (I think :-) the use of Option/Box to avoid the common
> > NPE. Also that map/for gives a nice way to conditionally do stuff with
> > something that can be Empty.
> >
> > But sometimes the Box just have to be full and if it isn't, this is a
> > logic error that is better handled than silently ignored.
>
> val myBox: Box[SomeType] = ...
>
> myBox or (alternative-logic)


A couple of common patterns I user are:

myBox openOr defaultValue


So:

User.currentUser.map(_.firstName.is) openOr "Unknown"


If you want a particular exception to be thrown when the logic cannot be
applied:

User.currentUser.map(_.firstName.is) openOr throw new FooException("I got no
user here... what's up?")



Hope this helps


>
> The or function returns the Box content if the Box is full or the
> result of the alternative-logic expression if the Box is Empty or
> Failure for that matter.
>
> You can also pattern match such as
>
> myBox match {
>  case Full(x) =>
>  case Failure(msg,ex,chain) => handle the Failure if you need to
>  case _ => handle Empty here
> }
>
> >
> > So what is a good pattern for doing this?
> >
> > On a related note: Is there a way to (globally) catch exceptions/errors
> that shows up in a Lift app?
> >
> > I foundhttp://wiki.liftweb.net/index.php/HowTo_use_error_pagesbut this
> > doesn't seem up to date....
>
> Please see LiftRules.exceptionHandler ... yeah I need to update that
> wiki.
>
> >
> > /Jeppe
> >
>


-- 
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to