Sha256: 9493a8b978f347af9663db1e4215961f18d0ad5f64e733af1c24db8c18276cdd

Contents?: true

Size: 789 Bytes

Versions: 2

Compression:

Stored size: 789 Bytes

Contents

require 'business_error/config'

module BusinessError
  class Error < StandardError
    attr_accessor :name, :msg, :code, :http_status

    def initialize(name, msg, code, http_status = 200)
      msg = name.to_s.humanize if msg.blank?
      @name, @msg, @code, @http_status = name, msg, code, http_status
    end

    def info
      @info ||= { code: @code, msg: @msg, http: @http_status }
    end

    def format!(template, **addition_content)
      content = Config.formats[template].each_with_index.map { |k, i| [k, info.values[i]] }.to_h
      @info = { only: content.merge(addition_content) }
      raise self
    end

    alias render! format!

    def with!(**addition_content)
      info.merge!(addition_content)
      raise self
    end

    def message; info.to_s end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
business_error-1.0.1 lib/business_error/error.rb
business_error-1.0.0 lib/business_error/error.rb