Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace process:exit in functions with Result + error handling #58

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nicmr
Copy link
Collaborator

@nicmr nicmr commented Jan 30, 2024

Description

Hi Rami, I hope you've had a good start into 2024 :) I'm back with another PR on error handling.

  • Currently, some methods simply call std::process::exit when encountering an error, which can cause problems
    1. It immediately exits the program and does not call any destructors (currently probably not a problem, but might in the future) like a panic would
    2. More importantly, it makes it hard to tell if a given function will cause the program to terminate, e.g. just reading the signature of a function pub fn selectable_list(input: Vec<String>) -> String does not tell you if it can cause the program to terminate, which can be very surprising to developers and requires you to always look at the implementation of a function.
  • They also immediately print an error message, which makes it hard to wrap the error message with additional information, for example why the function was called

I'm looking forward to your feedback :)

Fixes # -

Type of change

  • Add thiserror crate to make the error types less verbose
  • Add src/error.rs module for error types
  • Fallible functions in src/modes.rs now return Result<(), Error> (or Option<T> where appropriate)
  • Error messages are now defined on the error type
  • Errors are now wrapped with the context or have the error context as a field where appropriate

Changes from a user perspective:

Only error messages have changed.

  • Failing to make a selection with skim
    • old: No item selected
    • new (context): error: no item selected when prompted to select context
    • new (namespace): error: no item selected when prompted to select namespace
  • Trying to select a context that doesn't exist (kc foo)
    • old: error: no context exists with the name: foo
    • new: error: failed to set context: no context exists with the name foo

How Has This Been Tested?

  • Manual end-to-end testing

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@nicmr nicmr requested a review from Ramilito January 30, 2024 15:49
@Ramilito
Copy link
Owner

Ramilito commented Feb 7, 2024

Hey Nico! Super hectic start but hoping it will settle down soon 😓!
I take a look this weekend, super sorry for the delay 🙏

@@ -68,22 +71,32 @@ enum Mode {
}

impl Mode {
fn invoke(&self) {
fn invoke(&self) -> Result <(), Error> {
let args = Cli::parse();
Copy link
Owner

Choose a reason for hiding this comment

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

Nice!


use crate::{commands::{self}, config, Cli, DEST, KUBECONFIG};

fn selection(value: Option<String>, callback: fn() -> String) -> String {
Copy link
Owner

Choose a reason for hiding this comment

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

Thanking you for resolving this ugly hack 👍

@Ramilito
Copy link
Owner

More importantly, it makes it hard to tell if a given function will cause the program to terminate, e.g. just reading the signature of a function pub fn selectable_list(input: Vec) -> String does not tell you if it can cause the program to terminate, which can be very surprising to developers and requires you to always look at the implementation of a function.

This is such a good point! Thank you for pointing it out!

@nicmr
Copy link
Collaborator Author

nicmr commented Feb 20, 2024

Hey Nico! Super hectic start but hoping it will settle down soon 😓!

Keeping my fingers crossed for you! It's been quite hectic over here as well 😰

@nicmr
Copy link
Collaborator Author

nicmr commented Feb 20, 2024

Should I try and fix the issue with the CI before merging? I don't think it's related to my changes specifically

@Ramilito
Copy link
Owner

Ramilito commented Mar 7, 2024

I don't think so either, Il create a issue to make forks not need the phony secret from this repo (I'm guessing that it's the issue)

Merge away!

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.

None yet

2 participants