Class: SimpleAcp::Models::Error
- Inherits:
-
Object
- Object
- SimpleAcp::Models::Error
- Defined in:
- lib/simple_acp/models/errors.rb
Overview
Structured error response following the ACP specification.
Instance Attribute Summary collapse
-
#code ⇒ String
readonly
Error code (server_error, invalid_input, not_found).
-
#data ⇒ Object?
readonly
Additional error data.
-
#message ⇒ String
readonly
Human-readable error message.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Error
Create from a hash (JSON deserialization).
-
.invalid_input(message, data: nil) ⇒ Error
Create an invalid input error.
-
.not_found(message, data: nil) ⇒ Error
Create a not found error.
-
.server_error(message, data: nil) ⇒ Error
Create a server error.
Instance Method Summary collapse
-
#initialize(code:, message:, data: nil) ⇒ Error
constructor
Create a new error.
-
#to_h ⇒ Hash
Convert to hash for JSON serialization.
-
#to_json(*args) ⇒ String
Convert to JSON string.
Constructor Details
#initialize(code:, message:, data: nil) ⇒ Error
Create a new error.
36 37 38 39 40 |
# File 'lib/simple_acp/models/errors.rb', line 36 def initialize(code:, message:, data: nil) @code = code @message = @data = data end |
Instance Attribute Details
#code ⇒ String (readonly)
Returns error code (server_error, invalid_input, not_found).
23 24 25 |
# File 'lib/simple_acp/models/errors.rb', line 23 def code @code end |
#data ⇒ Object? (readonly)
Returns additional error data.
29 30 31 |
# File 'lib/simple_acp/models/errors.rb', line 29 def data @data end |
#message ⇒ String (readonly)
Returns human-readable error message.
26 27 28 |
# File 'lib/simple_acp/models/errors.rb', line 26 def @message end |
Class Method Details
.from_hash(hash) ⇒ Error
Create from a hash (JSON deserialization).
66 67 68 69 70 71 72 |
# File 'lib/simple_acp/models/errors.rb', line 66 def self.from_hash(hash) new( code: hash["code"] || hash[:code], message: hash["message"] || hash[:message], data: hash["data"] || hash[:data] ) end |
.invalid_input(message, data: nil) ⇒ Error
Create an invalid input error.
88 89 90 |
# File 'lib/simple_acp/models/errors.rb', line 88 def self.invalid_input(, data: nil) new(code: ErrorCode::INVALID_INPUT, message: , data: data) end |
.not_found(message, data: nil) ⇒ Error
Create a not found error.
97 98 99 |
# File 'lib/simple_acp/models/errors.rb', line 97 def self.not_found(, data: nil) new(code: ErrorCode::NOT_FOUND, message: , data: data) end |
.server_error(message, data: nil) ⇒ Error
Create a server error.
79 80 81 |
# File 'lib/simple_acp/models/errors.rb', line 79 def self.server_error(, data: nil) new(code: ErrorCode::SERVER_ERROR, message: , data: data) end |
Instance Method Details
#to_h ⇒ Hash
Convert to hash for JSON serialization.
45 46 47 48 49 50 51 52 |
# File 'lib/simple_acp/models/errors.rb', line 45 def to_h hash = { code: @code, message: @message } hash[:data] = @data if @data hash end |
#to_json(*args) ⇒ String
Convert to JSON string.
58 59 60 |
# File 'lib/simple_acp/models/errors.rb', line 58 def to_json(*args) to_h.to_json(*args) end |