nushell.github.io icon indicating copy to clipboard operation
nushell.github.io copied to clipboard

Add a chapter about errors

Open Hofer-Julian opened this issue 2 years ago • 5 comments

One concept that I still find quite hard to grasp are Nushell's errors. As far as I can tell, they behave like raised exceptions in a language like Python. They bubble up, but can be caught with try or do -i. However, I haven't seen that spelled out yet.

I see that nushell errors are also created if external commands exit with a non-zero exit code. But also that isn't mentioned in the book.

I would be happy to add a chapter about errors in Nushell if someone could me guide through the things I am unsure about.

Topics to cover:

  • behavior of errors
  • try
  • do
  • error make
  • complete

Hofer-Julian avatar Feb 08 '23 07:02 Hofer-Julian

I think this would be a great add. You may want to mention error make and complete as well. It may also be helpful to know that do has undergone some changes recently. So, I'd love some thorough documentation on it. I really don't understand why now you can just use do without the i sometimes.

fdncred avatar Feb 08 '23 12:02 fdncred

You may want to mention error make and complete as well

Good point, added it to a list of topics to cover

It may also be helpful to know that do has undergone some changes recently. So, I'd love some thorough documentation on it. I really don't understand why now you can just use do without the i sometimes.

Not sure what you mean there, at the moment, do takes a closure and can optionally accept command line flags according to the docs.

Regarding exceptions. Is there any relevant difference to exceptions in other languages which should be pointed out?

Hofer-Julian avatar Feb 08 '23 13:02 Hofer-Julian

Not sure what you mean there, at the moment,

There used to be a difference between these two commands (not in a directory with a git repo). Now there doesn't appear to be.

do -i { git branch --show-current }

and

do { git branch --show-current }

and with capturing the output.

do -i { git branch --show-current } | complete

and

do { git branch --show-current } | complete

fdncred avatar Feb 08 '23 14:02 fdncred

@fdncred I can see the following difference

# test1.nu
do { git branch --show-current }
echo Hello
$ nu test1.nu
fatal: not a git repository (or any of the parent directories): .git
# test2.nu
do -i { git branch --show-current }
echo Hello
$ test2.nu
fatal: not a git repository (or any of the parent directories): .git
Hello

Hofer-Julian avatar Feb 08 '23 14:02 Hofer-Julian

That's great that you've found a difference. Whatever all the differences are in those do commands should be documented.

fdncred avatar Feb 08 '23 15:02 fdncred