|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "Language: Data types: Error" |
| 4 | +--- |
| 5 | + |
| 6 | +[data type]: lang_data_type.html |
| 7 | +[abstract types]: lang_data_abstract.html |
| 8 | +[hash]: lang_data_hash.html |
| 9 | +[enum]: lang_data_abstract.html#enum |
| 10 | +[pattern]: lang_data_abstract.html#pattern |
| 11 | +[notundef]: lang_data_abstract.html#notundef |
| 12 | +[case statements]: lang_conditional.html#case-statements |
| 13 | + |
| 14 | +An `Error` value describes an error condition as data. It carries a message, and optionally a `kind` that |
| 15 | +names the category of error, an `issue_code`, and a hash of `details`. This lets code that can fail report an |
| 16 | +error as a value you can inspect and categorize, rather than only aborting the run. |
| 17 | + |
| 18 | +## Creating an Error |
| 19 | + |
| 20 | +The short form takes a message, and optionally a kind: |
| 21 | + |
| 22 | +```puppet |
| 23 | +$e = Error('The config file is missing') |
| 24 | +
|
| 25 | +notice($e.message) # The config file is missing |
| 26 | +``` |
| 27 | + |
| 28 | +```puppet |
| 29 | +$e = Error('The config file is missing', 'mymod/missing-file') |
| 30 | +
|
| 31 | +notice($e.kind) # mymod/missing-file |
| 32 | +``` |
| 33 | + |
| 34 | +To set an issue code or details as well, use the hash form, which names each part. Only `msg` is required: |
| 35 | + |
| 36 | +```puppet |
| 37 | +$e = Error({ |
| 38 | + 'msg' => 'The config file is missing', |
| 39 | + 'kind' => 'mymod/missing-file', |
| 40 | + 'issue_code' => 'E100', |
| 41 | + 'details' => {'path' => '/etc/myapp.conf'}, |
| 42 | +}) |
| 43 | +
|
| 44 | +notice($e.issue_code) # E100 |
| 45 | +notice($e.details['path']) # /etc/myapp.conf |
| 46 | +``` |
| 47 | + |
| 48 | +## Reading an error's parts |
| 49 | + |
| 50 | +An `Error` exposes its parts as members. `message` is the same as `msg`. The parts you did not set are |
| 51 | +`undef`: |
| 52 | + |
| 53 | +| Member | Description | |
| 54 | +| ------------ | ----------- | |
| 55 | +| `message` | The error message. Same as `msg`. | |
| 56 | +| `msg` | The error message. | |
| 57 | +| `kind` | The category of error, such as `mymod/missing-file`, or `undef`. | |
| 58 | +| `issue_code` | A code identifying the specific issue, which can be used to translate the message into other locales, or `undef`. | |
| 59 | +| `details` | A [hash][hash] of extra information, typed as `Hash[String[1], Data]`, or `undef`. | |
| 60 | + |
| 61 | +```puppet |
| 62 | +$e = Error('Something failed') |
| 63 | +
|
| 64 | +notice($e.message) # Something failed |
| 65 | +notice($e.kind =~ Undef) # true, no kind was set |
| 66 | +``` |
| 67 | + |
| 68 | +## The `Error` data type |
| 69 | + |
| 70 | +The [data type][data type] of an error value is `Error`. On its own it matches any error. |
| 71 | + |
| 72 | +### Parameters |
| 73 | + |
| 74 | +`Error` takes an optional `kind`, and an optional `issue_code` after it. Each restricts the type to errors |
| 75 | +whose `kind` or `issue_code` matches. A parameter can be a string for an exact match, a regular expression, |
| 76 | +or an abstract type such as [`Enum`][enum], [`Pattern`][pattern], or [`NotUndef`][notundef]. |
| 77 | + |
| 78 | +```puppet |
| 79 | +$e = Error('Bad value', 'mymod/type-mismatch') |
| 80 | +
|
| 81 | +notice($e =~ Error) # true |
| 82 | +notice($e =~ Error['mymod/type-mismatch']) # true, kind matches |
| 83 | +notice($e =~ Error['mymod/other']) # false |
| 84 | +notice($e =~ Error[Pattern[/^mymod/]]) # true, kind matches |
| 85 | +notice($e =~ Error[NotUndef]) # true, has a kind |
| 86 | +``` |
| 87 | + |
| 88 | +This makes `Error` useful for sorting errors by category, for example in a [case statement][case statements] |
| 89 | +that handles each `kind` differently: |
| 90 | + |
| 91 | +```puppet |
| 92 | +$err = Error('Config file missing', 'mymod/config') |
| 93 | +
|
| 94 | +$handled = $err ? { |
| 95 | + Error['mymod/config'] => 'config problem', |
| 96 | + Error['mymod/network'] => 'network problem', |
| 97 | + default => 'unknown problem', |
| 98 | +} |
| 99 | +
|
| 100 | +notice($handled) # config problem |
| 101 | +``` |
| 102 | + |
| 103 | +### Examples |
| 104 | + |
| 105 | +- `Error` --- matches any error. |
| 106 | +- `Error['mymod/missing-file']` --- matches an error with that exact kind. |
| 107 | +- `Error[Pattern[/^mymod/]]` --- matches an error whose kind starts with `mymod`. |
| 108 | +- `Error[NotUndef]` --- matches any error that has a kind. |
| 109 | +- `Error['mymod/missing-file', 'E100']` --- matches on both kind and issue code. |
| 110 | + |
| 111 | +### Related data types |
| 112 | + |
| 113 | +`Error` is part of `RichData`, but it is not `Data`, `Scalar`, or `ScalarData`: |
| 114 | + |
| 115 | +```puppet |
| 116 | +$e = Error('boom') |
| 117 | +
|
| 118 | +notice($e =~ RichData) # true |
| 119 | +notice($e =~ Data) # false |
| 120 | +notice($e =~ Scalar) # false |
| 121 | +notice($e =~ ScalarData) # false |
| 122 | +``` |
| 123 | + |
| 124 | +That matters when a parameter or function expects `Data`, which covers only the JSON-compatible types. Use |
| 125 | +`RichData`, or `Error` itself, when you need to accept one. |
| 126 | + |
| 127 | +You can also use [abstract types][abstract types] to match a value that might be an error. For example, |
| 128 | +`Optional[Error]` matches an `Error` or `undef`, and `Variant[Error, String]` matches an error or a plain |
| 129 | +string message. |
0 commit comments