Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

error make for core

Create an error.

Signature

> error make {flags} (error_struct)

Flags

  • --unspanned, -u: remove the origin label from the error

Parameters

  • error_struct: The error to create.

Input/output types:

inputoutput
nothingerror

Examples

Create a simple custom error

> error make {msg: "my custom error message"}

Create a complex error for a custom command that shows a full error_struct

> def foo [x] {
        error make {
            msg: "this is fishy"
            code: "my::error"  # optional error type to use
            labels: {  # optional, a table or single record
                text: "fish right here"  # Required if $.labels exists
                # use (metadata $var).span to get the {start: x end: y} of the variable
                span: (metadata $x).span  # optional
            }
            help: "something to tell the user as help"  # optional
            url: "https://nushell.sh"  # optional
        }
    }

Create a nested error from a try/catch statement with multiple labels

> try {
        error make {msg: "foo" labels: [{text: one} {text: two}]}
    } catch {|err|
        error make {msg: "bar", inner: [($err.json | from json)]}
    }

Notes

Errors are defined by an error_record, which is a record with a specific structure. (*) indicates a required key:

  • msg: string (*)
  • code: string
  • labels: oneof<table, record>
  • help: string
  • url: string
  • inner: table

The labels key allow for placing arrows to points in the code, optionally using span to choose where it points (see metadata). label can be a table (list of records) or a single record. There is an example of both in the examples. Each record has the following structure:

  • text: string (*)
  • span: record<start: int end: int>

The inner table takes a list of error_struct records, and can be used to have detail the errors that happened in a previous try {} catch {} statement or can be manually created. To use them from a catch statement, see the example below.