Error
| Description: | The data type generated by the error make command and other internal commands when an error condition occurs. |
| Annotation: | error |
| Literal syntax: | None |
| Casts: | error make (see below) |
Additional Language Notes
While
errormay be used as a type annotation, there is currently no known use-case for doing so, since anerrorvalue can never be assigned to a variable or custom command parameter. Any error condition results in termination of the current command/expression and will also terminate any assignment expression or custom command. For example:let e: error = (error make --unspanned { msg: "This is an error" }) # => Error: nu::shell::error # => # => × This is an error # => $e # => Error: nu::shell::variable_not_found # => # => × Variable not found # => ╭─[repl_entry #2:1:1] # => 1 │ $e # => · ─┬ # => · ╰── variable not found # => ╰────The
errortype is also returned from internal Nushell commands to indicate an error condition, but as with assignment, there is no way to use this result.Surrounding code that might potentially throw an
errorwith atry/catch {|e|}block will result in an$evariable that is arecord, not anerrortype.The record has the fields
msg,debug,raw,renderedanddetails.detailsis a record with themsg,labels,code,url,helpandinnerof the error:try { 1 / 0 } catch {|e| $e.msg } # => Division by zero. try { 1 / 0 } catch {|e| $e.details.code } # => nu::shell::division_by_zeroThe
rawfield holds the originalerrorvalue. Evaluating it raises the error again, so it can be used to re-throw an error after handling it:try { 1 / 0 } catch {|e| print "cleaning up"; $e.raw } # => cleaning up # => Error: nu::shell::division_by_zero # => # => × Division by zero. # => ╭─[repl_entry #1:1:9] # => 1 │ try { 1 / 0 } catch {|e| print "cleaning up"; $e.raw } # => · ┬ # => · ╰── division by zero # => ╰────See
try/catchfor more about handling errors.