Skip to content

give better error if required field of url join is invalid - #10589

Merged
amtoine merged 5 commits into
nushell:mainfrom
brunerm99:helpful-null-argument-message
Oct 10, 2023
Merged

amtoine merged 5 commits into
nushell:mainfrom
brunerm99:helpful-null-argument-message

Conversation

@brunerm99

@brunerm99 brunerm99 commented Oct 2, 2023 •

Copy link
Copy Markdown
Contributor

Description

Fix #10506 by adding ExpectedNonNull ShellError if required field is
entered as $nothing, null, " ", etc.

This adds a new ShellError, ExpectedNonNull, taking the expected
type and span.

User-Facing Changes

Will get a more helpful error in the case described by #10506. Examples:

> {scheme: hello, host: []} | url join
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry #1:1:1]
 1 │ {scheme: hello, host: []} | url join
   ·                       ─┬
   ·                        ╰── can't convert list<any> to string
   ╰────
> {scheme: hello, host: ""} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #2:1:1]
 1 │ {scheme: hello, host: ""} | url join
   ·                       ─┬
   ·                        ╰── expected non-empty string, got empty string
   ╰────
> {scheme: null, host: "github.com"} | url join
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry #3:1:1]
 1 │ {scheme: null, host: "github.com"} | url join
   ·          ──┬─
   ·            ╰── can't convert nothing to string
   ╰────

Tests + Formatting

All pass.

@brunerm99

Copy link
Copy Markdown
Contributor Author

@sholderbach from #10506

Has this been fully resolved by #10565?

No, but this should.

@sholderbach

Copy link
Copy Markdown
Member

Will look into the details tomorrow but in general we don't treat the empty string as null.

@brunerm99

brunerm99 commented Oct 2, 2023 •

Copy link
Copy Markdown
Contributor Author

Yeah probably shouldn't group it in with null. I could change it to say something like: expected non-empty string, found "".

That will have to be tomorrow though.

@amtoine

amtoine commented Oct 3, 2023

Copy link
Copy Markdown
Member

i agree with @sholderbach that the first error would be less confusing if it were to say

➜ {scheme: "", host: "github.com"} | url join
Error: nu::shell::unexpected_argument

  × Expected non-empty string.
   ╭─[entry #16:1:1]
 1 │ {scheme: "", host: "github.com"} | url join
   ·          ─┬
   ·           ╰── expected non-empty string
   ╰────

maybe creating another error type just for this is not the right way to go 🤔

the second error looks very nice 👌

@brunerm99

Copy link
Copy Markdown
Contributor Author

Updated examples:

➜ {scheme: "https", host: ""} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #1:1:1]
 1 │ {scheme: "https", host: ""} | url join
   ·                         ─┬
   ·                          ╰── expected non-empty string, got empty string
   ╰────
❯ {scheme: null, host: "github.com"} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #2:1:1]
 1 │ {scheme: null, host: "github.com"} | url join
   ·          ──┬─
   ·            ╰── expected non-empty string, got null or empty value
   ╰────
➜ {scheme: [], host: "github.com"} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #1:1:1]
 1 │ {scheme: [], host: "github.com"} | url join
   ·          ─┬
   ·           ╰── expected non-empty string, got null or empty value
   ╰────

I went for the got null or empty value on the second example because in the code currently the only two paths to a failure on matching the value is

  1. an empty string (example 1)
  2. anything else which is "empty" (examples 2,3)

and I didn't want to add much more logic for a simple error.

I also removed the new ShellError type.

@amtoine

amtoine commented Oct 4, 2023

Copy link
Copy Markdown
Member

mm now that we can see it, i'm personally not a huge fan of these... 🤔

if scheme and host and the rest can only be non-empty strings, then maybe url join should check the real Value and then if it's a Value::String, whether or not it's the empty one 😇

@brunerm99

Copy link
Copy Markdown
Contributor Author

@amtoine At this point in the add_component function the Value must be able to be converted to a string via Value::as_string() or else it continues onto the next input.

Are you saying check all values for being non-empty strings and error on any if they are empty (instead of just on host and scheme)? Or check within just within the host and scheme cases for emptiness of the string?

@amtoine

amtoine commented Oct 7, 2023

Copy link
Copy Markdown
Member

this might be a bit too far out of scope for this PR and the work you've done 🤔

but what i mean is that, to be as little confusing as possible for the user, i think we should have errors like

  • when there is a real missing config value (already like this right now)
❯ {host: "github.com"} | url join
Error: nu::shell::missing_parameter

  × Missing parameter: scheme.
   ╭─[entry #1:1:1]
 1 │ {host: "github.com"} | url join
   · ──────────┬─────────
   ·           ╰── missing parameter: scheme
   ╰────
  • when the type is incorrect, i.e. non-string (similar to what we have right now, even though more precise)
❯ {scheme: null, host: "github.com"} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #2:1:1]
 1 │ {scheme: null, host: "github.com"} | url join
   ·          ──┬─
   ·            ╰── expected string, got nothing
   ╰────
➜ {scheme: [], host: "github.com"} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #1:1:1]
 1 │ {scheme: [], host: "github.com"} | url join
   ·          ─┬
   ·           ╰── expected string, got list
   ╰────
  • when the type is correct but the string is empty
➜ {scheme: "https", host: ""} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #1:1:1]
 1 │ {scheme: "https", host: ""} | url join
   ·                         ─┬
   ·                          ╰── expected non-empty string
   ╰────

@brunerm99

brunerm99 commented Oct 7, 2023 •

Copy link
Copy Markdown
Contributor Author

@amtoine I looked into it and this would require some matching of the value after checking if it can be converted to a string using Value.as_string(). .as_string() already does this matching so I figured I would try just using it's error (see below). I think the message is clear about what it expects and it requires no more code to create error messages. I can also do it with the message you said but this seems simpler and doesn't add anything unnecessary. Thoughts?

❯ {scheme: hello, host: null} | url join
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry #3:1:1]
 1 │ {scheme: hello, host: null} | url join
   ·                       ──┬─
   ·                         ╰── can't convert nothing to string
   ╰────
❯ {scheme: hello, host: []} | url join
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry #5:1:1]
 1 │ {scheme: hello, host: []} | url join
   ·                       ─┬
   ·                        ╰── can't convert list<any> to string
   ╰────

Also: The other messages for missing required values and empty strings would look like what you suggested.

@amtoine

amtoine commented Oct 8, 2023

Copy link
Copy Markdown
Member

i think this looks quite good, because you'd see what the actual and expected types are 👌

let's go with this, if you have time to make the change, that'd be amazing 🙏

if the wording is not perfect in the end, this can be addressed in a follow-up PR 😉

@brunerm99

brunerm99 commented Oct 8, 2023 •

Copy link
Copy Markdown
Contributor Author

@amtoine Ok... I hope this commit finalizes this PR.

The updated error messages are as follows:

  1. Input incorrect type:
➜ {scheme: hello, host: []} | url join
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry #2:1:1]
 1 │ {scheme: hello, host: []} | url join
   ·                       ─┬
   ·                        ╰── can't convert list<any> to string
   ╰────
  1. Empty String passed for required input (host, scheme)
❯ {scheme: hello, host: ""} | url join
Error: nu::shell::unsupported_config_value

  × Unsupported config value
   ╭─[entry #3:1:1]
 1 │ {scheme: hello, host: ""} | url join
   ·                       ─┬
   ·                        ╰── expected non-empty string, got empty string
   ╰────
  1. Missing required input
❯ {scheme: hello} | url join
Error: nu::shell::missing_parameter

  × Missing parameter: host.
   ╭─[entry #4:1:1]
 1 │ {scheme: hello} | url join
   · ───────┬───────
   ·        ╰── missing parameter: host
   ╰────

@amtoine

amtoine commented Oct 9, 2023

Copy link
Copy Markdown
Member

@brunerm99
yeah i think this looks much better than what we have now 🙏
if you can fix the CI, then it looks like it's very close to land 😉

@brunerm99

Copy link
Copy Markdown
Contributor Author

Hey @amtoine, I fixed the problem but it looks like the CI failed due to a network error. Snip from failed test:

Error: nu::shell::network_failure

  × Network failure
   ╭─[source:1:1]
 1 │ http get --insecure https://expired.badssl.com/
   ·                     ─────────────┬─────────────
   ·                                  ╰── Cannot make request to https://expired.badssl.com/, there was an error establishing a connection.
   ╰────

Could this CI be re-run? Looks unrelated to my code, I think.

@brunerm99

Copy link
Copy Markdown
Contributor Author

@amtoine CI is now passing. Thanks @sholderbach for restarting it.

@amtoine

amtoine commented Oct 10, 2023

Copy link
Copy Markdown
Member

oh yeah i got the exact same failure in #10341 😮

@amtoine

amtoine commented Oct 10, 2023

Copy link
Copy Markdown
Member

let's land this PR, it looks much better than before 🙏

@amtoine amtoine changed the title Fix #10506 by adding ExpectedNonNull ShellError if required field is entered as $nothing, null, "", etc. give better error if required field of `url join Oct 10, 2023
@amtoine amtoine changed the title give better error if required field of `url join give better error if required field of url join is invalid Oct 10, 2023
@amtoine
amtoine merged commit 1402508 into nushell:main Oct 10, 2023
@brunerm99

Copy link
Copy Markdown
Contributor Author

@amtoine

oh yeah i got the exact same failure in #10341 😮

I was so confused for a minute 😆

Thanks for the help!

@amtoine

amtoine commented Oct 10, 2023

Copy link
Copy Markdown
Member

I was so confused for a minute 😆

same because it was so unrelated to the changes 😆

Thanks for the help!

anytime 😊

hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
…#10589)

# Description
Fix nushell#10506 by adding `ExpectedNonNull` `ShellError` if required field is
entered as `$nothing`, `null`, " ", etc.

This adds a new `ShellError`, `ExpectedNonNull`, taking the expected
type and span.

# User-Facing Changes
Will get a more helpful error in the case described by nushell#10506. Examples:
```nushell
➜ {scheme: "", host: "github.com"} | url join
Error: nu::shell::expected_non_null

  × Expected string found null.
   ╭─[entry nushell#16:1:1]
 1 │ {scheme: "", host: "github.com"} | url join
   ·          ─┬
   ·           ╰── expected string, found null
   ╰────
```

```nushell
❯ {scheme: "https", host: null} | url join
Error: nu:🐚:expected_non_null

  × Expected string found null.
   ╭─[entry nushell#19:1:1]
 1 │ {scheme: "https", host: null} | url join
   ·                         ──┬─
   ·                           ╰── expected string, found null
   ╰────
```

# Tests + Formatting
All pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing error message when variable is Nothing

3 participants