open_exception gem: now with better emacs support!

I just released v0.3.1 of the open_exception gem. This release does a better job of handling exceptions that have been munged by Rails, and now provides a tempfile containing the full stack trace to the open command.

If you are using emacs, you can use the :emacs_with_trace open command, and get the stack trace as a navigable compilation buffer (next to || below) the source file in a split frame. For this to work, you’ll need to add the following function to your emacs init:

See github for the code and README.

open_exception gem: auto open exceptions in your editor

open_exception opens an exception in your favorite editor when developing locally. It works by parsing the backtrace, and opening the offending file at the offending line in your favorite editor (assuming your favorite editor supports remote open commands).

You can add filters that allow you to ignore some exceptions, and filters that allow you to scope the backtrace search. The backtrace scoping is useful for opening the last call in your application code when the exception occurs in a framework or lib.

If you are on MacOSX and have the growl gem installed, you will get a growl notification with the exception message when the file is opened.


Out if the box, the gem supports three editors (with the following open commands):

:emacs => '/usr/bin/emacsclient -n +{line} {file}',
:textmate => '/usr/local/bin/mate -a -d -l {line} {file}',
:macvim => '/usr/local/bin/mvim +{line} {file}'

Note: if using emacs, you will need to be running emacsserver. To start the server: M-x server-start

or add (server-start) to your init.


To configure, pass a block to the configure method:

OpenException.configure do |oe|
    # open_with can be one of the built in editors (:emacs, :macvim, :textmate)
    # or a command to execute to open the file, where {file} and {line} will be replaced
    # with the file path and line number, respectively. See 'Editors' above for an example.
    # The default editor is :emacs.
    oe.open_with = :emacs
    # you can add exclusion filters to ignore exceptions. A filter can be an exception class to 
    # ignore, or a proc that is passed the exception, and should evaluate to true if the exception 
    # should be ignored. Be careful with using a class - it uses is_a?, so any subclasses of the
    # passed class will be ignored as well. The list of filters is [] by default.
    oe.exclusion_filters << SomeErrorClass
    oe.exclusion_filters << lambda { |exception| true if exception_should_be_excluded }
    # you can scope the search for the file:line to open with a filter as well. A filter can be a 
    # regular expression that is matched against the line, or a proc that is passed the line and 
    # should evaluate to true if the line should be used. The first line that any filter passes for 
    # will be the file:line that is opened. This is useful for opening the point in the stack just
    # before control passes out of your app code when the exception occurs in an external 
    # lib/framework. The list of filters is [] by default. 
    oe.backtrace_line_filters << %r{/app/root/(app|lib)} 
    oe.backtrace_line_filters << lambda { |backtrace_line| true if line_should_be_used }

Rails Integration

The gem also alias chains in to rails’ ActionController#rescue_action_locally method to automatically open exceptions in development mode. The gem also adds the following filter to the :backtrace_line_filters to scope the opened files to the app:


To replace or remove this filter, you will need to reset the :backtrace_line_filters in your configure block:

OpenException.configure do |oe|
    oe.backtrace_line_filters = []
    oe.backtrace_line_filters << my_new_filter

This has been tested with rails v2.3.5, but should work fine with 2.1 <= rails < 3. It may work with rails 3 as well, I just haven’t yet looked at rails 3.

Standalone/Other Frameworks

# To manually open an exception, or wire it up in another framework, you call:
# You can override the default (or configured) options by passing a hash as the second arg:, {:open_with => :textmate, :backtrace_line_filters => [filter, another_filter])

The source is on github, and the gem is on rubygems.

Generate a migration and open it in a buffer (in Emacs)

Requires rinari

Commenting out blocks of ruby in emacs

I’ve been an emacs user for 12+ years, and am ashamed to admit that I’ve never learned emacs lisp (yes, my last post was about emacs lisp code, but it was a copy and paste with a few changes, and I certainly didn’t understand what it was doing). Until now, that is. I started reading the emacs lisp intro, but only got about fifty pages in to it before I got the itch to write some code.

So here it is. Its a package that allows you to comment out a block of ruby code from a keybinding (I’m sure there is code out there to do this already, but it was a fun learning challenge).

The main function is comment-ruby, which I have bound to C-:. If there is a region active, it comments out the region. If that region is less than five lines, each line is commented with ‘# ‘, otherwise the entire region is wrapped in =begin...=end. If the region is not active, the current line is commented out with ‘# ‘.

After a region has been commented out, the point (cursor) is set to the line above or below the region, depending on whether the point was at the beginning or end of the region when called.

Here is the code – it probably isn’t idiomatic lisp. If you have any suggestions on improving the code or making it more idiomatic, please let me know!

Emacs tip: running ack in the project root w/rinari

This post covers how to combine the awesomeness of ack with rinari in emacs to run ack in the project root by default. (If you aren’t using ack, definitely check it out for searching your codebase. If you aren’t using emacs, this post isn’t going to convince you to switch).

I’ve been using Kim van Wyk’s ack.el for a few months now, but was annoyed that I had to tell it the directory where to run ack, when in most cases I wanted it to run in the root of my Rails app. So I copied the ack function from ack.el and made one that only asks for the search pattern, then runs in the root of the app (provided by rinari’s rinari-root function). I then bound that function to C-c f a.

Here is the code:

Note: you will need rinari and ack.el loaded, and ack will need to be in your path.