Class: Ksef::ProblemDetails

Inherits:
Data
  • Object
show all
Defined in:
lib/ksef/problem_details.rb,
lib/ksef/problem_details.rb

Overview

Parsing and presentation for ProblemDetails.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFAULTS =

Only status and raw are common to every envelope; everything else is absent in at least one of them, so the builders below name only what they actually have.

{
  status: nil, title: nil, detail: nil, instance: nil, timestamp: nil,
  trace_id: nil, entries: [].freeze, reason_code: nil, security: {}.freeze, raw: nil
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#detailObject (readonly)

Returns the value of attribute detail

Returns:

  • (Object)

    the current value of detail



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def detail
  @detail
end

#entriesObject (readonly)

Returns the value of attribute entries

Returns:

  • (Object)

    the current value of entries



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def entries
  @entries
end

#instanceObject (readonly)

Returns the value of attribute instance

Returns:

  • (Object)

    the current value of instance



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def instance
  @instance
end

#rawObject (readonly)

Returns the value of attribute raw

Returns:

  • (Object)

    the current value of raw



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def raw
  @raw
end

#reason_codeObject (readonly)

Returns the value of attribute reason_code

Returns:

  • (Object)

    the current value of reason_code



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def reason_code
  @reason_code
end

#securityObject (readonly)

Returns the value of attribute security

Returns:

  • (Object)

    the current value of security



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def security
  @security
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def status
  @status
end

#timestampObject (readonly)

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def timestamp
  @timestamp
end

#titleObject (readonly)

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def title
  @title
end

#trace_idObject (readonly)

Returns the value of attribute trace_id

Returns:

  • (Object)

    the current value of trace_id



12
13
14
# File 'lib/ksef/problem_details.rb', line 12

def trace_id
  @trace_id
end

Class Method Details

.parse(status:, body:) ⇒ Ksef::ProblemDetails

Parameters:

  • status (Integer)

    HTTP status

  • body (Hash, String, nil)

    decoded JSON body, or the raw string

Returns:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ksef/problem_details.rb', line 42

def parse(status:, body:)
  return build(status: status, raw: body) unless body.is_a?(Hash)

  if body["exception"].is_a?(Hash)
    legacy_exception(status, body)
  elsif body["status"].is_a?(Hash)
    legacy_status(status, body)
  else
    problem_json(status, body)
  end
end

Instance Method Details

#codeInteger?

Returns the first KSeF error code, when the body carried one.

Returns:

  • (Integer, nil)

    the first KSeF error code, when the body carried one



128
# File 'lib/ksef/problem_details.rb', line 128

def code = entries.first&.code

#detailsArray<String>

Returns every detail message, across all entries.

Returns:

  • (Array<String>)

    every detail message, across all entries



131
# File 'lib/ksef/problem_details.rb', line 131

def details = entries.flat_map(&:details)

#summaryString

A single line suitable for an exception message.

Returns:

  • (String)


136
137
138
139
140
141
# File 'lib/ksef/problem_details.rb', line 136

def summary
  lead = detail || title || entries.first&.description
  parts = [lead, *details.reject { |d| d == lead }].compact
  body = parts.empty? ? "HTTP #{status}" : parts.join(" ")
  code ? "[#{code}] #{body}" : body
end