Exception: Noise::PublicError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/noise/public_error.rb

Overview

Base class for all api level errors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(code, message) ⇒ PublicError #new(code, options) ⇒ PublicError

Returns a new instance of PublicError.

Overloads:

  • #new(code, message) ⇒ PublicError

    Instantiate error with given code and message

    Parameters:

    • code (Symbol)
    • message_or_options (String)
  • #new(code, options) ⇒ PublicError

    Instantiate error with given code and options. Options would be passed to I18n key

    Examples:

    Given the following I18n key exists:
      noise:
        public_error:
          unknown_fields: "Server does not know how to recognize these fields: %{fields}"
    
    To render error with this message:
      PublicError.new(:unknown_fields, fields: 'nickname, phone')

    Parameters:

    • code (Symbol)
    • message_or_options (Hash{Symbol => any})


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/noise/public_error.rb', line 34

def initialize(code, message_or_options = nil)
  @code = code.to_sym
  case message_or_options
  when Hash
    @options = message_or_options
    @message = nil
  else
    @options = {}
    @message = message_or_options
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



13
14
15
# File 'lib/noise/public_error.rb', line 13

def code
  @code
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/noise/public_error.rb', line 14

def options
  @options
end

Class Method Details

.register_as(status, severity:) ⇒ Object

Examples:

GoneError.register_as(:gone, :info)

Parameters:



71
72
73
74
# File 'lib/noise/public_error.rb', line 71

def register_as(status, severity:)
  Noise::ExceptionResponder.register(name, status: status)
  Noise::Notification.register(name, severity: severity)
end

Instance Method Details

#inspectString

Returns:

  • (String)


52
53
54
# File 'lib/noise/public_error.rb', line 52

def inspect
  "#<#{self.class}: #{message}>"
end

#messageString

Returns:

  • (String)


47
48
49
# File 'lib/noise/public_error.rb', line 47

def message
  @message.presence || I18n.t("noise.#{self.class.name.demodulize.underscore}.#{@code}", @options)
end

#responder_classExceptionResponder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



58
59
60
# File 'lib/noise/public_error.rb', line 58

def responder_class
  ExceptionResponder
end