-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcatch_errors.rb
More file actions
57 lines (53 loc) · 1.74 KB
/
catch_errors.rb
File metadata and controls
57 lines (53 loc) · 1.74 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
# frozen_string_literal: true
# Catches errors in a given block and returns them. This will return the
# output of the block if no errors are raised. Accepts an optional list of
# error kinds to catch.
#
# > **Note:** Not available in apply block
Puppet::Functions.create_function(:catch_errors) do
# @param error_types An array of error types to catch
# @param block The block of steps to catch errors on
# @return If an error is raised in the block then the error will be returned,
# otherwise the result will be returned
# @example Catch errors for a block
# catch_errors() || {
# run_command("whoami", $targets)
# run_command("adduser ryan", $targets)
# }
# @example Catch parse errors for a block of code
# catch_errors(['bolt/parse-error']) || {
# run_plan('canary', $targets)
# run_plan('other_plan)
# apply($targets) || {
# notify { "Hello": }
# }
# }
dispatch :catch_errors do
optional_param 'Array[String[1]]', :error_types
block_param 'Callable[0, 0]', :block
return_type 'Any'
end
def catch_errors(error_types = nil)
unless Puppet[:tasks]
raise Puppet::ParseErrorWithIssue
.from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING,
action: self.class.name)
end
executor = Puppet.lookup(:bolt_executor)
# Send Analytics Report
executor.report_function_call(self.class.name)
begin
yield
rescue Puppet::PreformattedError => e
if e.cause.is_a?(Bolt::Error)
if error_types.nil? || error_types.include?(e.cause.to_h['kind'])
e.cause.to_puppet_error
else
raise e
end
else
raise e
end
end
end
end