Module: Happy::Errors

Defined in:
lib/happy/errors.rb

Defined Under Namespace

Classes: Base, NotFound

Class Method Summary collapse

Class Method Details

.friendly_message_for(msg) ⇒ Object (protected)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/happy/errors.rb', line 42

def self.friendly_message_for(msg)
  case msg
  when /^undefined local variable or method `(.+)'/
    "You called a method called \"#{$1}\", and this method did not exist. This could simply be a typo. If it's not, please check that you're calling the method from within the correct scope."
  when /^undefined method `(.+)' for nil:NilClass$/
    "You called a method called <strong>\"#{$1}\"</strong> on <strong>nil</strong>. In most cases, this is due to simple typos; please check your variable names. Otherwise, make sure the receiving object has been initialized correctly."
  when /^undefined method `(.+)' for (.+)$/
    method = $1
    var = $2
    klass = case var
      when /^#<(.+):0x.+>$/ then $1
      when /^.+:(.+)$/ then $1
      else var
    end
    "You called a method called <strong>\"#{h method}\"</strong> on an instance of class <strong>#{h klass}</strong>, which doesn't know how to respond to that method. Please check for typos."
  end
end

.h(t) ⇒ Object (protected)


60
# File 'lib/happy/errors.rb', line 60

def self.h(t) ; Rack::Utils.escape_html(t) ; end

.html(exception, controller, options = {}) ⇒ Object

Render a HTML page for the given exception.

Options Hash (options):

  • :title (String)

    Title of error page

  • :message (String)

    Message to be displayed on error page, right underneath the title.

  • :friendly_message (String)

    Friendly error message to be displayed below title and message. If left blank, will be generated from the exception message.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/happy/errors.rb', line 21

def self.html(exception, controller, options = {})
  options = {
    :title => exception.class.to_s,
    :message => exception.message,
    :friendly_message => nil
  }.merge(options)

  # Load and cache error template.
  @html = begin
    File.read(File.expand_path(File.join(__FILE__, '../files/error.erb')))
  end

  # Generate friendly message from exception.
  options[:friendly_message] ||= friendly_message_for options[:message]

  # Render error page.
  ERB.new(@html).result(binding)
end