Class: Twirp::Error
- Inherits:
-
Object
- Object
- Twirp::Error
- Defined in:
- lib/twirp/error.rb
Overview
Twirp::Error represents an error response from a Twirp service. Twirp::Error is not an Exception to be raised, but a value to be returned by service handlers and received by clients.
Instance Attribute Summary collapse
-
#cause ⇒ Object
used when wrapping another error, but this is not serialized.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#msg ⇒ Object
readonly
Returns the value of attribute msg.
Class Method Summary collapse
-
.internal_with(err) ⇒ Object
Wrap another error as a Twirp::Error :internal.
- .valid_code?(code) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(code, msg, meta = nil) ⇒ Error
constructor
Initialize a Twirp::Error The code must be one of the valid ERROR_CODES Symbols (e.g. :internal, :not_found, :permission_denied …).
- #inspect ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(code, msg, meta = nil) ⇒ Error
Initialize a Twirp::Error The code must be one of the valid ERROR_CODES Symbols (e.g. :internal, :not_found, :permission_denied …). The msg is a String with the error message. The meta is optional error metadata, if included it must be a Hash with String values.
64 65 66 67 68 |
# File 'lib/twirp/error.rb', line 64 def initialize(code, msg, =nil) @code = code.to_sym @msg = msg.to_s = () end |
Instance Attribute Details
#cause ⇒ Object
used when wrapping another error, but this is not serialized
58 59 60 |
# File 'lib/twirp/error.rb', line 58 def cause @cause end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
56 57 58 |
# File 'lib/twirp/error.rb', line 56 def code @code end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
56 57 58 |
# File 'lib/twirp/error.rb', line 56 def end |
#msg ⇒ Object (readonly)
Returns the value of attribute msg.
56 57 58 |
# File 'lib/twirp/error.rb', line 56 def msg @msg end |
Class Method Details
.internal_with(err) ⇒ Object
Wrap another error as a Twirp::Error :internal.
50 51 52 53 54 |
# File 'lib/twirp/error.rb', line 50 def self.internal_with(err) twerr = internal err., cause: err.class.name twerr.cause = err twerr end |
.valid_code?(code) ⇒ Boolean
35 36 37 |
# File 'lib/twirp/error.rb', line 35 def self.valid_code?(code) ERROR_CODES_TO_HTTP_STATUS.key? code # one of the valid symbols end |
Instance Method Details
#inspect ⇒ Object
83 84 85 |
# File 'lib/twirp/error.rb', line 83 def inspect to_s end |
#to_h ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/twirp/error.rb', line 70 def to_h h = { code: @code, msg: @msg, } h[:meta] = unless .empty? h end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/twirp/error.rb', line 79 def to_s "<Twirp::Error code:#{code} msg:#{msg.inspect} meta:#{meta.inspect}>" end |