Class: EasyTalk::ErrorFormatter::Rfc7807

Inherits:
Base
  • Object
show all
Defined in:
lib/easy_talk/error_formatter/rfc7807.rb

Overview

Formats validation errors according to RFC 7807 (Problem Details for HTTP APIs).

RFC 7807 defines a standard format for describing errors in HTTP APIs. This formatter produces a Problem Details object with validation errors in an extended "errors" array.

Examples:

Output

{
  "type" => "https://example.com/validation-error",
  "title" => "Validation Failed",
  "status" => 422,
  "detail" => "The request contains invalid parameters",
  "errors" => [
    { "pointer" => "/properties/name", "detail" => "can't be blank", "code" => "blank" }
  ]
}

See Also:

Constant Summary collapse

DEFAULT_TITLE =

Default values for RFC 7807 fields

'Validation Failed'
DEFAULT_STATUS =
422
DEFAULT_DETAIL =
'The request contains invalid parameters'

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from EasyTalk::ErrorFormatter::Base

Instance Method Details

#formatHash

Format the errors as an RFC 7807 Problem Details object.

Returns:

  • (Hash)

    The Problem Details object



33
34
35
36
37
38
39
40
41
# File 'lib/easy_talk/error_formatter/rfc7807.rb', line 33

def format
  {
    'type' => error_type_uri,
    'title' => options.fetch(:title, DEFAULT_TITLE),
    'status' => options.fetch(:status, DEFAULT_STATUS),
    'detail' => options.fetch(:detail, DEFAULT_DETAIL),
    'errors' => build_errors_array
  }
end