Skip to content

Add label rendering to try/catch rendered errors#14477

Merged
fdncred merged 2 commits intonushell:mainfrom
132ikl:catch-err-labels
Nov 30, 2024
Merged

Add label rendering to try/catch rendered errors#14477
fdncred merged 2 commits intonushell:mainfrom
132ikl:catch-err-labels

Conversation

@132ikl
Copy link
Copy Markdown
Member

@132ikl 132ikl commented Nov 29, 2024

Description

Before this PR, you can access rendered error values that are raised in a try/catch block by accessing the rendered element of the catch error value:

$ try { ls nonexist.txt } catch {|e| print "my cool error:" $e.rendered }
my cool error:
nu::shell::directory_not_found

  × Directory not found
  help: /home/rose/nonexist.txt does not exist

However, the rendered errors don't include the labels present in the real rendered error, which would look like this:

$ ls nonexist.txt
Error: nu::shell::directory_not_found

  × Directory not found
   ╭─[entry #46:1:4]
 1 │ ls nonexist.txt
   ·    ──────┬─────
   ·          ╰── directory not found
   ╰────
  help: /home/rose/nonexist.txt does not exist

After this PR, the rendered error includes the labels:

$ try { ls nonexist.txt } catch {|e| print "my cool error:" $e.rendered }
my cool error:
Error: nu::shell::directory_not_found

  × Directory not found
   ╭─[entry #4:1:10]
 1 │ try { ls nonexist.txt } catch {|e| print "my cool error:" $e.rendered }
   ·          ──────┬─────
   ·                ╰── directory not found
   ╰────
  help: /home/rose/nonexist.txt does not exist

This change is accomplished by using the standard error formatting code to render an error. This respects the error theme as before without any extra scaffolding, but it means that e.g., the terminal size is also respected. I think this is fine because the way the error is rendered already changed based on config, and I think that a "rendered" error should give back exactly what would be shown to the user anyway.

@fdncred, let me know if you have any concerns with the way this is handled since you were the one who implemented this feature in the first place.

User-Facing Changes

The rendered element of the try/catch error record now includes labels in the error output.

Tests + Formatting

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

N/A

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Nov 30, 2024

great improvement! thanks!

@fdncred fdncred merged commit e1f74a6 into nushell:main Nov 30, 2024
@github-actions github-actions bot added this to the v0.101.0 milestone Nov 30, 2024
@fdncred fdncred added the deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way label Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants