trycatch

contributions requested
Try/Catch functions for Puppet

9,903 downloads

9,688 latest version

4.1 quality score

Support the Puppet Community by contributing to this module

You are welcome to contribute to this module by suggesting new features, currency updates, or fixes. Every contribution is valuable to help ensure that the module remains compatible with the latest Puppet versions and continues to meet community needs. Complete the following steps:

  1. Review the module’s contribution guidelines and any licenses. Ensure that your planned contribution aligns with the author’s standards and any legal requirements.
  2. Fork the repository on GitHub, make changes on a branch of your fork, and submit a pull request. The pull request must clearly document your proposed change.

For questions about updating the module, contact the module’s author.

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