Skip to content

Removes the panic, throws a warning & returns an empty list, which should be expected behavior#73

Merged
max-niederman merged 2 commits intomax-niederman:mainfrom
joao-vitor-sr:fix_list_languages
Sep 29, 2023
Merged

Removes the panic, throws a warning & returns an empty list, which should be expected behavior#73
max-niederman merged 2 commits intomax-niederman:mainfrom
joao-vitor-sr:fix_list_languages

Conversation

@joao-vitor-sr
Copy link
Copy Markdown
Contributor

@joao-vitor-sr joao-vitor-sr commented Feb 20, 2023

fix: #55

Copy link
Copy Markdown

@Sntx626 Sntx626 left a comment

Choose a reason for hiding this comment

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

Does improve the error message and provide a possible fix, however I think the panic should be avoided.

src/main.rs Outdated
if opt.list_languages {
opt.languages()
.expect("Couldn't get installed languages under config directory. Make sure the config directory exists.")
.expect("Couldn't get installed languages under the config directory. Make sure the config and language directory exist.")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This improves the error message.
However I agree with @omarkohl, the message should be printed as a warning and return an empty list with .or_else(vec![]).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since there's no logging this could look like the following:

    if opt.list_languages {
        opt.languages()
            .or_else({
                println!("Warning: Couldn't get installed languages under config directory. Make sure the config directory exists.");
                vec![]
            })
            .iter()
            .for_each(|name| println!("{}", name.to_str().expect("Ill-formatted language name.")));
        return Ok(());
    }

@joao-vitor-sr joao-vitor-sr changed the title refactor(errors): better message when languaage directory doesn't exist refactor(errors): better handling of the languages directory Apr 8, 2023
if opt.list_languages {
opt.languages()
.expect("Couldn't get installed languages under config directory. Make sure the config directory exists.")
match opt.languages() {
Copy link
Copy Markdown
Contributor Author

@joao-vitor-sr joao-vitor-sr Apr 8, 2023

Choose a reason for hiding this comment

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

Since or_else methods on results don't return a default value, an if or a match is necessary

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.

(improving the explanation) the or_else will return another Result which has to be handled.

Copy link
Copy Markdown

@Sntx626 Sntx626 left a comment

Choose a reason for hiding this comment

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

This looks good!
Removes the panic, throws a warning & returns an empty list, which should be expected behavior.
-> Approved

@joao-vitor-sr joao-vitor-sr changed the title refactor(errors): better handling of the languages directory Removes the panic, throws a warning & returns an empty list, which should be expected behavior Apr 12, 2023
Copy link
Copy Markdown
Owner

@max-niederman max-niederman left a comment

Choose a reason for hiding this comment

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

LGTM

@max-niederman max-niederman merged commit 237223e into max-niederman:main Sep 29, 2023
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.

Error when .config/ttyper/language directory does not exist

3 participants