Class: X::MissingResource

Inherits:
Resources::Error show all
Defined in:
x-resources/lib/x/resources/errors.rb

Overview

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

A lookup of one resource that is not there raises it whether the API answers 200 OK with no data or a 404 that reports the resource as not found, so code that rescues it need not know which. It is not the NotFound of x-core, which holds the response, and which every other 404 raises, such as one from a client pointed at the wrong host or API version; the NotFound of a 404 that reports the resource is the cause of this.

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



44
45
46
47
48
# File 'x-resources/lib/x/resources/errors.rb', line 44

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



34
35
36
# File 'x-resources/lib/x/resources/errors.rb', line 34

def problems
  @problems
end