Sha256: 870babac6f56bf33855e24b80d6823b292161e6d87e522222b08fc94efc94fa3

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# encoding: UTF-8

module Mojito
	
	class MojitoException < Exception
		
		def initialize(status = 500, message = STATUS[status].message)
			super(message)
			@status = Mojito::STATUS[status].code
		end
		
		attr_reader :status
		
		def to_response
			Rack::Response.new [], status, 'Content-Type' => 'application/octet-stream'
		end
		
	end
	
	module Helpers
	
		module ExceptionHandling
			
			def self.included(type)
				type.instance_exec do
					old_dispatch = instance_method(:dispatch)
					define_method :dispatch do
						begin
							old_dispatch.bind(self).call
						rescue Exception => e
							__handle_error e
						end
					end
				end
				type.extend ClassMethods
			end
			
			def raise(exception)
				backtrace = caller[1..-1]
				Kernel.raise case exception
				when Symbol, Integer
					Mojito::MojitoException.new(exception)
				when Exception
					exception
				else
					RuntimeError.new(exception)
				end.tap {|e| e.set_backtrace backtrace }
			end
			
			def __handle_error(exception)
				if handler = case exception
					when MojitoException
						response.status = exception.status
						self.class.error_handlers[exception.status]
					when Exception
						self.class.error_handlers[exception.class]
					end
					instance_exec &handler
				end
				raise exception
			end
			
			module ClassMethods
				
				def on_error(type, &block)
					case type
					when Symbol, Integer
						error_handlers[Mojito::STATUS[type].code] = block
					when Class
						error_handlers[type] = block
					end
				end
				
				def error_handlers
					@__error_handlers ||= {}
				end
				
			end
			
		end
		
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mojito-0.1.1 lib/mojito/helpers/exception_handling.rb