Git Product home page Git Product logo

errational's Introduction

errational - nice error handling

Build Status

Summary

This gem will create custom Exception classes for your rails app, rescue from them whenever they are raised, and display a nice message to the user.

Requirements

  • Ruby 1.9+
  • Rails 3+

Install

Add to your Gemfile:

gem 'errational'

Run Generators

$ rails generate errational:install <app_name>

This will generate three files, there is documentation within the generated files:

lib/errational/<app_name>_error.rb lib/errational/<app_name>_exception.rb config/initializers/errational_configuration.rb

Usage

This gem will generate exception classes based on the error constants defined in the generated <app_name>_error.rb file. It will also rescue from these custom exceptions and use the custom defined method of displaying the string of the constant to the user. It also rescues from all Exceptions and if the exception is not one of the custom exception classes, it will display the message from the ApplicationError::General::UNEXPECTED constant.

This module contains modules of error constants, from each constant an exception class will be generated. Each submodule will be created as a class; it will be used as an intermediate ancestor between the exception class and the application exception class. All error constants must be within a module.

The constant General::UNEXPECTED is special. This is the message that will be used for all exceptions that are caught that are not from the custom exception classes.

The module Loggable is a special case. All error constants defined under Loggable (they can be within submodules) will be logged using the Logger defined in the configuration.

For example, where Application is the name of the application:

 module ApplicationError

   module General
     UNEXPECTED = "An error has occurred in the application. Please try again."
   end

   module Loggable
     SERVICE_FAULT = "The response from the service cannot be processed. Please try again"
   end

   module Serious
     BAD_PROBLEM = "The server is on fire, please call the fire department"
   end
 end

 class FirstController < ApplicationController
    def index
      raise ApplicationException::ServiceFault # Will automatically be handled by the error handling gem with corresponding message.
    end

    def create
      raise RuntimeError  # Will automatically be handled by error handling gem with UNEXPECTED message.
    end

    def update
      raise ApplicationException::BadProblem
      rescue ApplicationException::Parent => e  # Will rescue any of the exceptions generated by the plugin.
        if(e.is_a? ApplicationException::Serious)  # Sub modules are created as ancestor classes of exceptions, useful for special casing.
          call_lead_developer
        end
     end
  end

The exception module generated into lib/errational/<app_name>_exception.rb is where you can override the default behavior of exception classes or add other exception classes. The default behavior of the exception classes is to create an exception with the message of the constant. Exception classes that override generated exception classes must be defined as children of their parent module's generated class.

Example:

 module ApplicationException
   include Errationalify

   class ServiceFault < Loggable
     def initialize(service_name)
       message = sprintf(ApplicationError::SERVICE_FAULT, service_name)
     end
   end
 end
 module ApplicationError
   module General
     UNEXPECTED = "An error has occurred in the application. Please try again."
   end
   module Loggable
     SERVICE_FAULT = "The response from %s cannot be processed. Please try again"
   end
 end

Configuration

In the file generated into config/initializers/errational_configuration.rb you will find several values that are configurable.

The error partial is the path to the partial that will be rendered as the result of xhr actions that result in an exception being thrown. The default error dialog requires jQuery >= 1.5 and jQuery UI >= 1.8.9. The error dialog will be created with an id of errational_error_dialog and a class of errational_class. These can be styled.

Example:

Errational.setup do |config|
  # Namespace for name of exception and error modules, based on the name given at install time.
  # Note, if changing, change the module names in lib/namespace_error.rb and lib/namespace_exception.rb,
  # , as well as the filenames
  config.namespace = "Application"

  # The response code given for exceptions that are handled.
  config.error_response_code = 203

  # The partial to be used for exceptions handled as the result of xhr actions
  config.error_partial = '/errational/error_dialog'

  # The logger to be used for NamespaceException::Loggable exceptions and unexpected exceptions
  config.logger = Rails.logger
end

Development

Please run rake appraisal:install once, then use rake appraisal to judge whether the build will pass.

errational's People

Contributors

olore avatar hahashraf avatar

Stargazers

Gary Lai avatar Wellington Torrejais da Silva avatar Katie Gengler avatar Faruk Celik avatar Michael Schiller avatar Ryan Taylor Long avatar Phil Gengler avatar  avatar  avatar Craig Wickesser avatar Ezekiel Templin avatar Matt Scilipoti avatar Mark Glossop avatar Allen Bargi avatar

Watchers

 avatar Katie Gengler avatar James Cloos avatar  avatar

Forkers

uversity olore

errational's Issues

Better response when exception is raised during full http request

Better response when exception is raised during full http request:

Have a better response when an exception is raised during a full http request. Currently it renders the text of the message with the error response code.

It should perhaps model after rails rescue_action_in_public. It should be configurable, whatever happens in this situation.

Exception classes should optionally take an exception

When rescuing one exception, then raising an errational exception because of it, it would be nice to have the trace available (for the log, etc) from the original exception

For example:

    def foo
    #do something
rescue Exception => e
  raise MyApp::SomeError(e)
end

Such that the class/message/trace from actual error are preserved

Allow %s in error string

If a user defined error string has %s in it, substitute the variable passed in and join it with commas

remove output from rake test

all the tmp/ dir stuff needs to go away

Started
.................cd /Users/brian/dev/errational
rm -rf /Users/brian/dev/errational/test/tmp
mkdir -p /Users/brian/dev/errational/test/tmp
cd /Users/brian/dev/errational
.
Finished in 0.267634 seconds.

Problems when generating the files

Hi, I was trying to use the gem but when I execute the generator errational:install I get the following error:

/home/cherta/.rvm/gems/ruby-1.9.2-p180/bundler/gems/errational-4a2f98bd984a/lib/errational/errationality.rb:46:in 'block in <top (required)>': undefined method `exception_base' for Errational:Module (NoMethodError)

Is strange because the "exception_base" method is there.

My environment is:

OpenSuse 11.4
ruby-1.9.2-p180

Please let me know if you need more info

Create helpers for testing purposes

Create helpers of testing purposes:

A helper to assert that no exception was raised, being that assert_raise and assert_nothing_raised do not seem to work with rescue_from. But whether it does or not should be confirmed with Rails 3.

A helper to assert the desired message was displayed to a user.

Add more configuration options

Add more configuration options:

The id used for the default error dialog, the class used for the default error dialog, the title for the default error dialog.

Whether or not Exception is rescued from. Whether or not Loggable is special.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.