Forge Home

trycatch

Try/Catch functions for Puppet

9,461 downloads

9,274 latest version

4.1 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Version information

  • 0.2.0 (latest)
  • 0.1.0
released Oct 5th 2015

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'dalen-trycatch', '0.2.0'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add dalen-trycatch
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install dalen-trycatch --version 0.2.0

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

dalen/trycatch — version 0.2.0 Oct 5th 2015

trycatch

This is a simple Puppet module that adds the functions try and catch. They can be used to catch an exception raised in the block passed to try.

This module uses the V4 function API, so you need Puppet 4.x to use it or Puppet 3.7.x with the future parser.

Example

$ret = try() || {
  assert_type('String', 1)
  notice('This code is never reached')
  'return value from try'
}.catch |$exception| {
  notice($exception['class'])
  notice($exception['message'])
  'return value from catch'
}
notice($ret)

Output:

Notice: Scope(Class[main]): Puppet::PreformattedError
Notice: Scope(Class[main]): Evaluation Error: Error while evaluating a Function Call, assert_type(): Expected type String does not match actual: Integer at /Users/dalen/src/puppet-trycatch/test.pp:2:3
Notice: Scope(Class[main]): return value from catch
Notice: Compiled catalog for river.local in environment production in 0.28 seconds
Notice: Applied catalog in 0.01 seconds

Only catch certain exceptions

try() || {
  assert_type('String', 1)
}.catch('ArgumentError', 'RuntimeError') |$exception| {
  notice("Caught ${$exception['class']}")
}

Output:

Error: Evaluation Error: Error while evaluating a Function Call, assert_type(): Expected type String does not match actual: Integer at /Users/dalen/src/puppet-trycatch/test.pp:2:3 on node river.local

Note that this doesn't take exception subclasses into account. It only checks if the class names match exactly.

Note that this is a hack, use accordingly