Class: Pragma::Operation::Response::UnprocessableEntity

Inherits:
Pragma::Operation::Response show all
Defined in:
lib/pragma/operation/response/unprocessable_entity.rb

Overview

Represents the 422 Unprocessable Entity HTTP response.

Constant Summary

Constants inherited from Pragma::Operation::Response

STATUSES

Instance Attribute Summary

Attributes inherited from Pragma::Operation::Response

#entity, #headers, #status

Instance Method Summary collapse

Methods inherited from Pragma::Operation::Response

#decorate_with, #failure?, #success?

Constructor Details

#initialize(entity: nil, headers: {}, errors: nil) ⇒ UnprocessableEntity

Initializes the response.

You can provide either entity or errors, but not both. If you provide entity, the standard response’s entity will be replaced with yours. If you provide errors, the standard entity will be used and your errors will be added to the meta.

Parameters:

  • entity (Object) (defaults to: nil)

    the response’s entity

  • headers (Hash) (defaults to: {})

    the response’s headers

  • errors (Hash) (defaults to: nil)

    the response’s errors

Raises:

  • (ArgumentError)

    if both entity and errors are provided



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pragma/operation/response/unprocessable_entity.rb', line 19

def initialize(entity: nil, headers: {}, errors: nil)
  fail ArgumentError, 'You cannot provide both :entity and :errors!' if entity && errors

  entity ||= Error.new(
    error_type: :unprocessable_entity,
    error_message: 'The provided resource is in an unexpected format.',
    meta: {
      errors: errors || {}
    }
  )

  super(status: 422, entity: entity, headers: headers)
end