error make for core
Signature
> error make {flags} (error_struct)
Flags
--unspanned, -u: remove the labels from the error
Parameters
error_struct: The error to create.
Input/output types:
| input | output |
|---|---|
| nothing | error |
| string | error |
| record | error |
Examples
Create a simple, default error
> error makeCreate a simple error from a string
> error make 'my error message'Create a simple error from an error_struct record
> error make {msg: 'my error message'}A complex error utilizing spans and inners
> def foo [x: int, y: int] {
let z = $x + $y
error make {
msg: "an error for foo just occurred"
labels: [
{text: "one" span: (metadata $x).span}
{text: "two" span: (metadata $y).span}
]
help: "some help for the user"
inner: [
{msg: "an inner error" labels: [{text: "" span: (metadata $y).span}]}
]
}
}Chain errors using a pipeline
> try {error make "foo"} catch {error make "bar"}Chain errors using arguments (note the extra command in catch)
> try {
error make "foo"
} catch {|err|
print 'We got an error that will be chained!'
error make {msg: "bar" inner: [$err]}
}Notes
Use either as a command with an error_struct or string as an input. The error_struct is detailed below:
msg: string(required)code: stringlabels: table<error_label>help: stringurl: stringinner: table<error_struct>src: src_record
The error_label should contain the following keys:
text: stringspan: record<start: int end: int>
External errors (referencing external sources, not the default nu spans) are created using the src column with the src_record record. This only changes where the labels are placed. For this, the code key is ignored, and will always be nu::shell::outside. Errors cannot use labels that reference both inside and outside sources, to do that use an inner error.
name: string- name of the sourcetext: string- the raw text to place the spans inpath: string- a file path to place the spans in
Errors can be chained together using the inner key, and multiple spans can be specified to give more detailed error outputs.
If a string is passed it will be the msg part of the error_struct.
Errors can also be chained using try {} catch {}, allowing for related errors to be printed out more easily. The code block for catch passes a record of the try block's error into the catch block, which can be used in error make either as the input or as an argument. These will be added as inner errors to the most recent error make.