# encoding: utf-8 require_relative "messages" module Hexx module Helpers # @api hide # The module declares instance helper methods to provide validations module Exceptions extend ActiveSupport::Concern include ActiveModel::Validations include Messages # @api hide # Class helper methods module ClassMethods def raises(*names) fail ArgumentError if (types = names.flatten.map(&:to_s)) == [] types.each { |type| add_exception(type) } end private def add_exception(type) check type class_eval "class #{ type } < StandardError; end" end def check(type) return if type[/([A-Z][a-z]+)+/] fail TypeError.new( %(Wrong exception name "#{ type }". Use words in camel-case only.) ) end end def on_error(messages) messages.map(&:text).each { |text| errors.add :base, text } fail Hexx::ServiceInvalid.new(self) end def escape yield rescue Hexx::ServiceInvalid => err raise err rescue => err errors.add :base, err.message raise Hexx::ServiceInvalid.new(self) end end end end