Class: DataModel::Error

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/data_model/error.rb

Overview

Error is a class that holds errors. Errors are a tuple of [name, ctx]

- name is a symbol that identifies the error
- ctx is contextual information about the error which can be used to build an error message

The error object is a structured way to store, modify, and add errors in that intermediary format.
To turn an error into a human readable message, use #to_messages, which delegates to a registry

Base errors are errors that are related to the object as a whole, and not to any specific child
Child errors are errors that are related to a specific child of the object, which may or may not apply depending on the type

Instance Method Summary collapse

Methods included from Errors

#blank_error, #blank_error_message, #coerce_error, #coerce_error_message, #earliest_error, #early_error_message, #error_messages, #exclusion_error, #exclusion_error_message, #extra_keys_error, #extra_keys_error_message, #format_error, #format_error_message, #inclusion_error, #inclusion_error_message, #late_error_message, #latest_error, #max_error, #max_error_message, #min_error, #min_error_message, #missing_error, #missing_error_message, #type_error, #type_error_message

Constructor Details

#initializeError

Create a new error Object



16
17
18
19
# File 'lib/data_model/error.rb', line 16

def initialize
	@base = []
	@children = {}
end

Instance Method Details

#add(err, child: nil) ⇒ void

This method returns an undefined value.

Add an error to the error list.

Parameters:

  • err (Array(Symbol, untyped))

    the error to add

  • child (Symbol, Array(Symbol)) (defaults to: nil)

    the child to add the error to. child can be an array of symbols to specify a path if nested



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/data_model/error.rb', line 71

def add(err, child: nil)
	if child.is_a?(Array)
		child = child.join(".").to_sym
	end

	if child == :base
		raise "child errors may not be named :base"
	end

	errs = child ? @children[child] ||= [] : @base
	errs.push(err)
end

#allHash{Symbol => Array<Array(Symbol, untyped)>} Also known as: to_h

all errors

Returns:

  • (Hash{Symbol => Array<Array(Symbol, untyped)>})

    all errors



35
36
37
# File 'lib/data_model/error.rb', line 35

def all
	return children.merge(base:)
end

#any?(&blk) ⇒ Boolean

Returns true if any errors are present.

Parameters:

  • blk (Proc)

    an optional block to filter errors, takes an Array(Symbol, untyped) and returns boolean

Returns:

  • (Boolean)

    true if any errors are present



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/data_model/error.rb', line 44

def any?(&blk)
	if !blk
		return !@base.empty? || !@children.empty?
	end

	any = T.let(false, T::Boolean)

	for error_list in all.values
		any = error_list.any?(&blk)
		if any
			break
		end
	end

	return any
end

#baseArray<Array(Symbol, untyped)>

errors related to the object as a whole

Returns:

  • (Array<Array(Symbol, untyped)>)

    the base errors



23
24
25
# File 'lib/data_model/error.rb', line 23

def base
	return @base
end

#childrenHash{Symbol => Array<Array(Symbol, untyped)>}

errors related children

Returns:

  • (Hash{Symbol => Array<Array(Symbol, untyped)>})

    the child errors



29
30
31
# File 'lib/data_model/error.rb', line 29

def children
	return @children
end

#empty?Boolean

Returns true if no errors are present.

Returns:

  • (Boolean)

    true if no errors are present



63
64
65
# File 'lib/data_model/error.rb', line 63

def empty?
	!any?
end

#merge_child(name, child) ⇒ void

This method returns an undefined value.

Merge another error object into this one for child Errors

Parameters:

  • name (Symbol)

    the name of the child

  • child (Error)

    the child error object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/data_model/error.rb', line 88

def merge_child(name, child)
	if !child.any?
		return
	end

	for (key, error_list) in child.all
		for error in error_list
			add(error, child: [name, key])
		end
	end
end

#to_messages(registry: Registry.instance) ⇒ Hash{Symbol => Array[String]}

Get human readable error messages from error tuples

Parameters:

  • registry (Registry) (defaults to: Registry.instance)

    the registry to use to get error messages

Returns:

  • (Hash{Symbol => Array[String]})

    the error messages



103
104
105
# File 'lib/data_model/error.rb', line 103

def to_messages(registry: Registry.instance)
	return registry.error_messages(self)
end