Class: WebFunction::DocumentedError

Inherits:
Object
  • Object
show all
Defined in:
lib/web_function/documented_error.rb

Overview

Represents an error definition as described in a Web Function package.

An error definition documents the possible errors that an endpoint might return, including a machine-readable error code and a human-readable description.

See the [error definition documentation] on the Web Function website for more details, including recognized keys and usage recommendations.

[0]: webfunction.org/package#error-definition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, docs: nil) ⇒ DocumentedError

Returns a new instance of DocumentedError.



15
16
17
18
# File 'lib/web_function/documented_error.rb', line 15

def initialize(code:, docs: nil)
  @code = code
  @docs = docs.to_s
end

Instance Attribute Details

#codeString (readonly)

The machine-readable code of the error.

Returns:

  • (String)


59
60
61
# File 'lib/web_function/documented_error.rb', line 59

def code
  @code
end

#docsString (readonly)

The documentation of the error.

Returns:

  • (String)


65
66
67
# File 'lib/web_function/documented_error.rb', line 65

def docs
  @docs
end

Class Method Details

.from_array(errors) ⇒ Array<DocumentedError>

Creates a new DocumentedError from an array of hashes. Uses from_hash under the hood.

Parameters:

  • errors (Array<Hash>)

    The error array of hashes

Returns:



48
49
50
51
52
# File 'lib/web_function/documented_error.rb', line 48

def from_array(errors)
  Utils.normalize_array errors do |error|
    from_hash(error)
  end
end

.from_hash(error) ⇒ DocumentedError

Creates a new DocumentedError from a hash.

Parameters:

  • error (Hash)

    The error hash

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/web_function/documented_error.rb', line 27

def from_hash(error)
  unless error.is_a?(Hash)
    return
  end

  unless error["code"]
    return
  end

  new(
    code: error["code"],
    docs: error["docs"],
  )
end