Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

exceptor

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exceptor

  • 1.0.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Exceptor

Handle your exceptions with an elegant OO pattern.

Introduction

With Exceptor handle all of your exceptions in one place, Don't repeat yourself! define a handler for your exception and use it all over the program.

Installation

Add this line to your application's Gemfile:

gem 'exceptor', '~> 1.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install exceptor

Usage

Let's define a handler for exceptions

class ExceptionHandler < Exceptor::Base

  on(:default) do |context,error|
    # self is equal to instance which error raised on.
    # context is the closure binding
    # error contains error information such as message, exception and ...
    "#{@instance_var} #{context.local_var} raised #{error.class}"
  end

  on(StandardError) do |context,error|
    "#{@instance_var} #{context.local_var} raised StandardError"
  end

end

As you see, we can define a handler for all non-defined exceptions with :default argument. Also inside the block you can directly access to the instance which error raised on, and you can access block binding too(from context argument).

Ok, Let's use our ExceptionHandler:

class Foo
  include Exceptor::Safe
  default_exceptor ExceptionHandler

  def initialize
    @instance_var = "Foo"
  end

  def save
    local_var = "Bar"
    # Just put your dangerous code inside exceptor block, That's all
    exceptor do
      bar.save # For example bar.save is gonna raise StandardError
    end
  end

end

object = Foo.new.save
# => "Foo Bar raised StandardError"

As you see, you can define handler for your exceptions, and use it with exceptor closure! Also setting default_exceptor isn't required.

Also you can pass your handler like:

exceptor(MyCustomHandler) do
  # put your dangerous code here
end

Exceptor is very handy, you can use it in your daily programming.

Exceptor is about 70-80 lines of code(It's small), so there isn't any performance issue.

You can use specs as example.

Rails users

If you want to use Exceptor in a RoR application, Check this out

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/EhsanYousefi/Exceptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 07 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc