Class: X::MissingResource

Inherits:
Objects::Error show all
Defined in:
x-objects/lib/x/objects/errors.rb

Overview

Raised when a resource that was asked for by identifier or name does not exist

The API answers a lookup of a resource that is not there with 200 OK and no data, so this is not the NotFound of a 404 response, which an endpoint that is not there raises, and which holds the response that named it.

It is raised as well when a request that creates a resource, such as X::Post.create, succeeds without returning it, as X::User.current! raises it when users/me returns no user, holding the problems the response reported.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, problems: []) ⇒ MissingResource

Initialize the error, adding the detail of the first problem to the message

Examples:

Raise the error

raise X::MissingResource.new("Could not find X::User nobody", problems: problems)

Parameters:

  • message (String, nil) (defaults to: nil) —

    the message

  • problems (Array<Problem>) (defaults to: []) —

    the problems the API reported



42
43
44
45
46
# File 'x-objects/lib/x/objects/errors.rb', line 42

def initialize(message = nil, problems: [])
  explanation = problems.first&.then { |problem| problem.detail || problem.title }
  super(([message, explanation].compact.join(": ") if message || explanation))
  @problems = problems.dup.freeze
end

Instance Attribute Details

#problems ⇒ Array<Problem> (readonly)

The problems the API reported about the resource

Examples:

Read why a user was not found

error.problems.first&.detail # => "Could not find user with username: [nobody]."

Returns:

  • (Array<Problem>) —

    the problems, empty if the API reported none



32
33
34
# File 'x-objects/lib/x/objects/errors.rb', line 32

def problems
  @problems
end