Class: GraphQL::Client::Errors
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Enumerable
|
|
Inherits: | Object |
Defined in: | lib/graphql/client/errors.rb |
Overview
Public: Collection of errors associated with ::GraphQL
object type.
Inspired by ActiveModel::Errors
.
Class Method Summary
-
.new(errors = [], path = [], all = false) ⇒ Errors
constructor
Internal: Initialize from collection of errors.
-
.normalize_error_paths(data = nil, errors = [])
Internal: Normalize GraphQL
Error
“path” ensuring the path exists.
Instance Attribute Summary
-
#blank?
readonly
Alias for #empty?.
-
#empty? ⇒ Boolean
(also: #blank?)
readonly
Public: Check if there are no errors on object.
Instance Method Summary
-
#[](key)
Public: When passed a symbol or a name of a field, returns an array of errors for the method.
-
#all
Public: Return collection of all nested errors.
-
#count
Alias for #size.
-
#details
Public: Access Hash of error objects.
-
#each
Public: Iterates through each error key, value pair in the error messages hash.
-
#filter_by_path(field)
Internal: Return collection of errors for a given subfield.
-
#has_key?(field)
Alias for #include?.
-
#include?(field) ⇒ Boolean
(also: #has_key?, #key?)
Public: Check if there are any errors on a given field.
-
#inspect
Public: Display console friendly representation of errors collection.
-
#key?(field)
Alias for #include?.
-
#keys
Public: Returns all message keys.
-
#messages
Public: Access Hash of error messages.
-
#size
(also: #count)
Public: Count the number of errors on object.
-
#values
Public: Returns all message values.
Constructor Details
.new(errors = [], path = [], all = false) ⇒ Errors
Internal: Initialize from collection of errors.
errors - Array of ::GraphQL
Hash error objects path - Array of String|Integer fields to data all - Boolean flag if all nested errors should be available
Class Method Details
.normalize_error_paths(data = nil, errors = [])
Internal: Normalize GraphQL Error
“path” ensuring the path exists.
Records “normalizedPath” value to error object.
data - Hash of response data errors - Array of error Hashes
Returns nothing.
# File 'lib/graphql/client/errors.rb', line 20
def self.normalize_error_paths(data = nil, errors = []) errors.each do |error| path = ["data"] current = data error.fetch("path", []).each do |key| break unless current path << key current = current[key] end error["normalizedPath"] = path end errors end
Instance Attribute Details
#blank? (readonly)
Alias for #empty?.
# File 'lib/graphql/client/errors.rb', line 171
alias blank? empty?
#empty? ⇒ Boolean
(readonly)
Also known as: #blank?
Public: Check if there are no errors on object.
data.errors. # => {"node"=>["couldn't find node by id"]}
data.errors.empty? # => false
Returns true if no errors are found, otherwise false.
# File 'lib/graphql/client/errors.rb', line 168
def empty? size.zero? end
Instance Method Details
#[](key)
Public: When passed a symbol or a name of a field, returns an array of errors for the method.
data.errors[:node] # => ["couldn't find node by id"]
data.errors['node'] # => ["couldn't find node by id"]
Returns Array of errors.
# File 'lib/graphql/client/errors.rb', line 122
def [](key) .fetch(key, []) end
#all
Public: Return collection of all nested errors.
data.errors[:node]
data.errors.all[:node]
Returns Errors collection.
# File 'lib/graphql/client/errors.rb', line 51
def all if @all self else self.class.new(@raw_errors, @ast_path, true) end end
#count
Alias for #size.
# File 'lib/graphql/client/errors.rb', line 160
alias count size
#details
Public: Access Hash of error objects.
data.errors.details["node"]
data.errors.details[:node]
Returns HashWithIndifferentAccess.
# File 'lib/graphql/client/errors.rb', line 95
def details return @details if defined? @details details = {} @raw_errors.each do |error| path = error.fetch("normalizedPath", []) matched_path = @all ? path[0, @ast_path.length] : path[0...-1] next unless @ast_path == matched_path field = path[@ast_path.length] next unless field details[field] ||= [] details[field] << error end @details = HashWithIndifferentAccess.new(details) end
#each
Public: Iterates through each error key, value pair in the error messages hash. Yields the field and the error for that attribute. If the field has more than one error message, yields once for each error message.
# File 'lib/graphql/client/errors.rb', line 130
def each return enum_for(:each) unless block_given? .each_key do |field| [field].each { |error| yield field, error } end end
#filter_by_path(field)
Internal: Return collection of errors for a given subfield.
data.errors.filter_by_path("node")
Returns Errors collection.
# File 'lib/graphql/client/errors.rb', line 64
def filter_by_path(field) self.class.new(@raw_errors, @ast_path + [field], @all) end
#has_key?(field)
Alias for #include?.
# File 'lib/graphql/client/errors.rb', line 148
alias has_key? include?
#include?(field) ⇒ Boolean
Also known as: #has_key?, #key?
Public: Check if there are any errors on a given field.
data.errors. # => {"node"=>["couldn't find node by id", "unauthorized"]}
data.errors.include?("node") # => true
data.errors.include?("version") # => false
Returns true if the error messages include an error for the given field, otherwise false.
# File 'lib/graphql/client/errors.rb', line 145
def include?(field) self[field].any? end
#inspect
Public: Display console friendly representation of errors collection.
Returns String.
# File 'lib/graphql/client/errors.rb', line 196
def inspect "#<#{self.class} @messages=#{ .inspect} @details=#{details.inspect}>" end
#key?(field)
Alias for #include?.
# File 'lib/graphql/client/errors.rb', line 149
alias key? include?
#keys
Public: Returns all message keys.
data.errors. # => {"node"=>["couldn't find node by id"]}
data.errors.values # => ["node"]
Returns Array of String field names.
# File 'lib/graphql/client/errors.rb', line 179
def keys .keys end
#messages
Public: Access Hash of error messages.
data.errors. ["node"]
data.errors. [:node]
Returns HashWithIndifferentAccess.
# File 'lib/graphql/client/errors.rb', line 74
def return @messages if defined? @messages = {} details.each do |field, errors| [field] ||= [] errors.each do |error| [field] << error.fetch("message") end end @messages = HashWithIndifferentAccess.new( ) end
#size Also known as: #count
Public: Count the number of errors on object.
data.errors. # => {"node"=>["couldn't find node by id", "unauthorized"]}
data.errors.size # => 2
Returns the number of error messages.
# File 'lib/graphql/client/errors.rb', line 157
def size values.flatten.size end
#values
Public: Returns all message values.
data.errors. # => {"node"=>["couldn't find node by id"]}
data.errors.values # => [["couldn't find node by id"]]
Returns Array of Array String messages.
# File 'lib/graphql/client/errors.rb', line 189
def values .values end