Exception: JsonHttpError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/json_http_error.rb,
lib/json_http_error/handler.rb,
lib/json_http_error/railtie.rb

Defined Under Namespace

Modules: Handler Classes: Railtie

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_code, error_opts = {}) ⇒ JsonHttpError

Returns a new instance of JsonHttpError.



4
5
6
7
# File 'lib/json_http_error.rb', line 4

def initialize(error_code, error_opts = {})
  @code = error_code
  @opts = error_opts.with_indifferent_access
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



2
3
4
# File 'lib/json_http_error.rb', line 2

def code
  @code
end

#optsObject

Returns the value of attribute opts.



2
3
4
# File 'lib/json_http_error.rb', line 2

def opts
  @opts
end

Instance Method Details

#replyObject



13
14
15
16
17
# File 'lib/json_http_error.rb', line 13

def reply
  reply = { status: 'ERROR', code: code, message: I18n.t("http_status.#{code}", opts) }
  reply = reply.merge(opts[:json]) if opts[:json]
  reply
end

#to_procObject



19
20
21
22
23
24
25
# File 'lib/json_http_error.rb', line 19

def to_proc
  binded_reply = reply
  binded_code  = code
  Proc.new do
    render status: binded_code, json: { error: binded_reply }
  end
end