Skip to content

Add warning to url join when input key is not supported (#10506) - #10565

Merged
fdncred merged 3 commits into
nushell:mainfrom
brunerm99:url-join-incorrect-key-warning
Oct 2, 2023
Merged

fdncred merged 3 commits into
nushell:mainfrom
brunerm99:url-join-incorrect-key-warning

Conversation

@brunerm99

@brunerm99 brunerm99 commented Oct 1, 2023 •

Copy link
Copy Markdown
Contributor

Description

Adds warning to url join when input key is not supported as suggested by @amtoine in #10506.

It just adds a println! statement but it seems like that is all that is done for other warnings, e.g.,

println!("warning: get non-utf8 filename {path:?}, ignored.");

Tests + Formatting

All pass.

@fdncred

fdncred commented Oct 1, 2023

Copy link
Copy Markdown
Contributor

Seems like we'd want a ShellError here versus a println! right? Which other warnings are you talking about?

@brunerm99

Copy link
Copy Markdown
Contributor Author

@fdncred I thought this would make more sense as a warning than an error since, if the required fields are present, it will still work, just not include that one field (as in @amtoine's example below). I can change it to an error if you think that would make more sense.

> {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
foo://bar

To your second question: The only other place I found a warning message was what I linked (below), but I may be missing something. I agree, a simple println! may not be the best but I didn't see other examples of things like this.

println!("warning: get non-utf8 filename {path:?}, ignored.");

Thanks

@fdncred

fdncred commented Oct 1, 2023

Copy link
Copy Markdown
Contributor

We try to stay away from println!'s and we don't have a ShellWarning, only ShellError. I'm not sure what the right solution is here. @amtoine any ideas?

@sholderbach

Copy link
Copy Markdown
Member

For the deprecations we use nu_protocol::report_error_new() that allows you to print a ShellError with span info (the _new variant allows you to do this with just an EngineState)

nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError(
"Deprecated command".into(),
"`random decimal` is deprecated and will be removed in 0.86.".into(),
Some(call.head),
Some("Use `random float instead".into()),
vec![],
),
);

@brunerm99

Copy link
Copy Markdown
Contributor Author

Oh interesting... I'll look into that @sholderbach

@amtoine

amtoine commented Oct 1, 2023

Copy link
Copy Markdown
Member

yup, the report_error_new appears to be the way to go now 😋

@brunerm99

brunerm99 commented Oct 1, 2023 •

Copy link
Copy Markdown
Contributor Author

I'm working on adding this new error but I'm having trouble attaching the error message to the col of the input record instead of the value (example below)

➜ {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
Error:   × 'im_not_a_url_field' is not a valid URL field
   ╭─[entry #1:1:1]
 1 │ {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
   ·                                                  ──┬─
   ·                                                    ╰── Remove 'im_not_a_url_field' col from input record
   ╰────


foo://bar

The problem I'm having is that the cols in Record are Vec<String> so they don't have span. Does anyone know of a way to point the error to the column name?

I could also just do this:

➜ {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
Error:   × 'im_not_a_url_field' is not a valid URL field
   ╭─[entry #1:1:1]
 1 │ {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
   · ───────────────────────────┬──────────────────────────
   ·                            ╰── Remove 'im_not_a_url_field' col from input record
   ╰────


foo://bar

@amtoine

amtoine commented Oct 1, 2023

Copy link
Copy Markdown
Member

@brunerm99
i do not know a way to do that per column when you don't already have the span 🤔

i could go with either one 👍

.into_iter()
.try_fold(UrlComponents::new(), |url, (k, v)| {
url.add_component(k, v, span)
url.add_component(k, v, span, engine_state)

@brunerm99 brunerm99 Oct 1, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added engine_state to url.add_component() for report_error_new. I think this should be fine, let me know if not.

@brunerm99

Copy link
Copy Markdown
Contributor Author

I went with this:

➜ {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
Error:   × 'im_not_a_url_field' is not a valid URL field
   ╭─[entry #1:1:1]
 1 │ {scheme: "foo", host: "bar", im_not_a_url_field: "hi"} | url join
   · ───────────────────────────┬──────────────────────────
   ·                            ╰── Remove 'im_not_a_url_field' col from input record
   ╰────


foo://bar

Makes more sense to me.

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good if the design should be that this is only a warning. Haven't follow the potential debate why this should not lead to a hard error.

But tweaking that at a later date would now be trivial thanks to you figuring out the ShellError with spans for this.

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if a warning versus and error is appropriate but I'm willing to move forward with this PR since, I think it's better than before with either solution.

@fdncred
fdncred merged commit 679879f into nushell:main Oct 2, 2023
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
) (nushell#10565)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Adds warning to `url join` when input key is not supported as suggested
by @amtoine in nushell#10506.

It just adds a `println!` statement but it seems like that is all that
is done for other warnings, e.g.,
https://github.com/nushell/nushell/blob/20aaaaf90c24ea5495cba3396d3977ab10923488/crates/nu-glob/src/lib.rs#L434

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
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.

4 participants