Module: Apiwork::ErrorCode
- Defined in:
- lib/apiwork/error_code.rb,
lib/apiwork/error_code/registry.rb,
lib/apiwork/error_code/definition.rb
Defined Under Namespace
Classes: Definition, Registry
Constant Summary collapse
- DEFAULTS =
{ bad_gateway: { status: 502, }, bad_request: { status: 400, }, conflict: { status: 409, }, forbidden: { status: 403, }, gateway_timeout: { status: 504, }, gone: { status: 410, }, internal_server_error: { status: 500, }, locked: { status: 423, }, method_not_allowed: { status: 405, }, not_acceptable: { status: 406, }, not_found: { attach_path: true, status: 404, }, not_implemented: { status: 501, }, payment_required: { status: 402, }, precondition_failed: { status: 412, }, request_timeout: { status: 408, }, service_unavailable: { status: 503, }, too_many_requests: { status: 429, }, unauthorized: { status: 401, }, unprocessable_entity: { status: 422, }, unsupported_media_type: { status: 415, }, }.freeze
Class Method Summary collapse
-
.find(key) ⇒ ErrorCode::Definition?
Finds an error code by key.
-
.find!(key) ⇒ ErrorCode::Definition
Finds an error code by key.
- .key_for_status(status) ⇒ Object
-
.register(key, attach_path: false, status:) ⇒ ErrorCode::Definition
Registers a custom error code for use in API responses.
- .register_defaults! ⇒ Object
Class Method Details
.find(key) ⇒ ErrorCode::Definition?
Finds an error code by key.
114 115 116 117 118 119 120 121 |
# File 'lib/apiwork/error_code.rb', line 114 delegate :clear!, :exists?, :find, :find!, :keys, :register, :values, to: Registry |
.find!(key) ⇒ ErrorCode::Definition
Finds an error code by key.
114 115 116 117 118 119 120 121 |
# File 'lib/apiwork/error_code.rb', line 114 delegate :clear!, :exists?, :find, :find!, :keys, :register, :values, to: Registry |
.key_for_status(status) ⇒ Object
123 124 125 |
# File 'lib/apiwork/error_code.rb', line 123 def key_for_status(status) DEFAULTS.find { |_key, config| config[:status] == status }&.first end |
.register(key, attach_path: false, status:) ⇒ ErrorCode::Definition
Registers a custom error code for use in API responses.
Error codes are used with raises declarations and expose_error
in controllers. Built-in codes (400-504) are pre-registered.
114 115 116 117 118 119 120 121 |
# File 'lib/apiwork/error_code.rb', line 114 delegate :clear!, :exists?, :find, :find!, :keys, :register, :values, to: Registry |