Skip to content

Commit 39f18d9

Browse files
committed
Document the Error data type
Add a reference page for the Error data type, which describes an error condition as a value: a message plus an optional kind, issue_code, and details hash. Covers construction from a message, a kind, or a hash; reading the parts via the message/msg/kind/issue_code/details members; and the Error[kind, issue_code] type parameters for categorizing errors by kind (string, regexp, or abstract type). Error is RichData but not Data, Scalar, or ScalarData. Wire the page into the nav, the values-and-data-types index, and the data type syntax index. Part of #407 Signed-off-by: Michael Harp <mike@mikeharp.com>
1 parent df7ba32 commit 39f18d9

4 files changed

Lines changed: 129 additions & 0 deletions

File tree

_data/nav/openvox_8x.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@
199199
link: "lang_data_time.html"
200200
- text: SemVer and SemVerRange
201201
link: "lang_data_semver.html"
202+
- text: Error
203+
link: "lang_data_error.html"
202204
- text: Sensitive
203205
link: "lang_data_sensitive.html"
204206
- text: Undef

docs/_openvox_8x/lang_data.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Strings are the most common and useful data type, but you'll also have to work w
2727
* [Binary](./lang_data_binary.html)
2828
* [Timestamp and Timespan](./lang_data_time.html)
2929
* [SemVer and SemVerRange](./lang_data_semver.html)
30+
* [Error](./lang_data_error.html)
3031
* [Sensitive](./lang_data_sensitive.html)
3132
* [Undef](./lang_data_undef.html)
3233
* [Resource References](./lang_data_resource_reference.html)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
$handled = $err ? {
93+
Error['mymod/config'] => 'config problem',
94+
Error['mymod/network'] => 'network problem',
95+
default => 'unknown problem',
96+
}
97+
```
98+
99+
### Examples
100+
101+
- `Error` --- matches any error.
102+
- `Error['mymod/missing-file']` --- matches an error with that exact kind.
103+
- `Error[Pattern[/^mymod/]]` --- matches an error whose kind starts with `mymod`.
104+
- `Error[NotUndef]` --- matches any error that has a kind.
105+
- `Error['mymod/missing-file', 'E100']` --- matches on both kind and issue code.
106+
107+
### Related data types
108+
109+
`Error` is part of `RichData`, but it is not `Data`, `Scalar`, or `ScalarData`:
110+
111+
```puppet
112+
$e = Error('boom')
113+
114+
notice($e =~ RichData) # true
115+
notice($e =~ Data) # false
116+
notice($e =~ Scalar) # false
117+
```
118+
119+
That matters when a parameter or function expects `Data`, which covers only the JSON-compatible types. Use
120+
`RichData`, or `Error` itself, when you need to accept one.
121+
122+
You can also use [abstract types][abstract types] to match a value that might be an error. For example,
123+
`Optional[Error]` matches an `Error` or `undef`, and `Variant[Error, String]` matches an error or a plain
124+
string message.

docs/_openvox_8x/lang_data_type.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ title: "Language: Data types: Data type syntax"
2020
[binary]: ./lang_data_binary.html
2121
[time]: ./lang_data_time.html
2222
[semver]: ./lang_data_semver.html
23+
[error]: ./lang_data_error.html
2324
[undef]: ./lang_data_undef.html
2425
[default]: ./lang_data_default.html
2526
[resource_reference]: ./lang_data_resource_reference.html
@@ -156,6 +157,7 @@ These are the "real" data types, which make up the most common values you'll int
156157
* [`Binary`][binary]
157158
* [`Timestamp` and `Timespan`][time]
158159
* [`SemVer` and `SemVerRange`][semver]
160+
* [`Error`][error]
159161
* [`Undef`][undef]
160162
* [`Default`][default]
161163

0 commit comments

Comments
 (0)