Skip to content
This repository was archived by the owner on Feb 7, 2020. It is now read-only.

Errors Handling

botanicus edited this page Sep 13, 2010 · 24 revisions

My opinion on opinionated software is you can have your opinion on everything you want. But I care shit about it.

General introduction

There is a lot of ways how you can handle exceptions in MVC pattern. In Merb you have special controller, Rails prefer to keep it in one controller, or you can require more complex setup … so Rango doesn’t care about exceptions handling a lot.

Default Exceptions Handling

class Posts < Rango::Controller
  def index
    @post = Post.get(params[:id])
    raise NotFound, "Post with given ID doesn't exist"
    render "posts.html"
  end
end

Rails-like Exceptions Handling

Well, not exactly the same syntax, but definitely the same concept:

class Application < Rango::Controller
  def rescue_http_error(exception)
    self.send(exception.class.name.snake_case)
  end
end

Merb-like Exceptions Handling

class Application < Rango::Controller
  def rescue_http_error(exception)
    method_name = exception.name.class.snake_case
    Exceptions.route_to(method_name)
  end
end

class Posts < Application
  def index
    @post = Post.get(params[:id])
    raise NotFound
    render "posts.html"
  end
end
class Exceptions < Rango::Controller
  def not_found
  end

  def rescue_http_error(exception)
   # TODO: what it should do?
    method_name = exception.name.class.snake_case
    Exceptions.route_to(method_name)
  end
end

Clone this wiki locally