-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy patherrors.rb
More file actions
152 lines (128 loc) · 4.56 KB
/
errors.rb
File metadata and controls
152 lines (128 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true
require "dry/system/cycle_visualization"
module Dry
module System
# Error raised when import is called on an already finalized container
#
# @api public
ContainerAlreadyFinalizedError = Class.new(StandardError)
# Error raised when a component dir is added to configuration more than once
#
# @api public
ComponentDirAlreadyAddedError = Class.new(StandardError) do
def initialize(dir)
super("Component directory #{dir.inspect} already added")
end
end
# Error raised when a configured component directory could not be found
#
# @api public
ComponentDirNotFoundError = Class.new(StandardError) do
def initialize(dir)
super("Component dir '#{dir}' not found")
end
end
# Error raised when a namespace for a component dir is added to configuration more
# than once
#
# @api public
NamespaceAlreadyAddedError = Class.new(StandardError) do
def initialize(path)
path_label = path ? "path #{path.inspect}" : "root path"
super("Namespace for #{path_label} already added")
end
end
# Error raised when attempting to register provider using a name that has already been
# registered
#
# @api public
ProviderAlreadyRegisteredError = Class.new(ArgumentError) do
def initialize(provider_name)
super("Provider #{provider_name.inspect} has already been registered")
end
end
# Error raised when a named provider could not be found
#
# @api public
ProviderNotFoundError = Class.new(ArgumentError) do
def initialize(name)
super("Provider #{name.inspect} not found")
end
end
# Error raised when a named provider source could not be found
#
# @api public
ProviderSourceNotFoundError = Class.new(StandardError) do
def initialize(name:, group:, keys:)
msg = "Provider source not found: #{name.inspect}, group: #{group.inspect}"
key_list = keys.map { |key| "- #{key[:name].inspect}, group: #{key[:group].inspect}" }
msg += "Available provider sources:\n\n#{key_list}"
super(msg)
end
end
# Error raised when trying to use a plugin that does not exist.
#
# @api public
PluginNotFoundError = Class.new(StandardError) do
def initialize(plugin_name)
super("Plugin #{plugin_name.inspect} does not exist")
end
end
# Exception raise when a plugin dependency failed to load
#
# @api public
PluginDependencyMissing = Class.new(StandardError) do
# @api private
def initialize(plugin, message, gem = nil)
details = gem ? "#{message} - add #{gem} to your Gemfile" : message
super("dry-system plugin #{plugin.inspect} failed to load its dependencies: #{details}")
end
end
# Exception raised when auto-registerable component is not loadable
#
# @api public
ComponentNotLoadableError = Class.new(NameError) do
# @api private
def initialize(component, error,
corrections: DidYouMean::ClassNameChecker.new(error).corrections)
full_class_name = [error.receiver, error.name].join("::")
message = [
"Component '#{component.key}' is not loadable.",
"Looking for #{full_class_name}."
]
if corrections.any?
case_correction = corrections.find { |correction| correction.casecmp?(full_class_name) }
if case_correction
acronyms_needed = case_correction.split("::").difference(full_class_name.split("::"))
stringified_acronyms_needed = acronyms_needed.map { |acronym|
"'#{acronym}'"
} .join(", ")
message <<
<<~ERROR_MESSAGE
You likely need to add:
acronym(#{stringified_acronyms_needed})
to your container's inflector, since we found a #{case_correction} class.
ERROR_MESSAGE
else
message << DidYouMean.formatter.message_for(corrections)
end
end
super(message.join("\n"))
end
end
# Error raised when components have cyclic dependencies
#
# @api public
CyclicDependencyError = Class.new(StandardError) do
# @api private
def initialize(cycle)
cycle_visualization = CycleVisualization.generate(cycle)
super(<<~ERROR_MESSAGE)
These dependencies form a cycle:
#{cycle_visualization}
You must break this cycle in order to use any of them.
ERROR_MESSAGE
end
end
end
end