I like using exceptions in my code, it keeps things simple and easier
to read. I'm less concerned about speed, and "Flow Control"
correctness. In Merb, I have to investigate my model in several
different ways, then raise an exception for each (permissions,
validation failures, database connection lost, etc...). For example,
here's one of my controller actions:

def create
  @article = Article.new(params[:article])
  raise Unauthorized unless requester.may_create?(@article)

  if @article.save
    head(Created, :location => uri(:article, :id => @article.id))
  else
    raise RecordInvalid.new(@article.errors)
  end
end

And this even fails in cases where there's something wrong saving the
record that has nothing to do with validations (a 500 server error,
rather than a 4xx client error). If datamapper were to do the
exceptions handling thing, that action could be reduced to two lines:

def create
  @article = Article.create(params[:article])
  head(Created, :location => uri(:article, :id => @article.id)
end

That would be my favorite solution. But Sam says exceptions are slow,
and I'll agree that there are applications for DM other than web apps.
I choose to raise exceptions, and let Merb's exception handler render
the right error page.

On Nov 28, 2:45 am, Alex Neth <[EMAIL PROTECTED]> wrote:
> ...
> The basic problem is that there is no guarantee of what "false" means
> when returned from save.  If you change the contract to be that false
> means there was a validation error, and state exceptions are thrown in
> any other instance of save failure, then I could write correct code.

I think this would be an acceptable middle ground, for speed concerns.
The most common error, a validation failure, takes the fast path of
save returning false. The other cases are rarer, and could be safely
handled by exceptions.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to