-
Notifications
You must be signed in to change notification settings - Fork 163
Improve the error description when no name is provided in func delete
#3054
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
Conversation
Hi @RayyanSeliya. Thanks for your PR. I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3054 +/- ##
==========================================
+ Coverage 55.66% 59.06% +3.39%
==========================================
Files 134 134
Lines 17315 17350 +35
==========================================
+ Hits 9638 10247 +609
+ Misses 6773 6136 -637
- Partials 904 967 +63
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
cmd/delete.go
Outdated
return runDelete(cmd, args, newClient) | ||
// Layer 2: Catch technical errors and provide CLI-specific user-friendly messages | ||
err := runDelete(cmd, args, newClient) | ||
if err != nil && errors.Is(err, fn.ErrNameRequiredForDelete) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good! I would just use a general fn.ErrNameRequired
as the error's name. The fact it's "for delete" is implied by its context here (so you can reuse ErrNameRequired in other places where... a name is required)
cmd/delete.go
Outdated
// Layer 2: Catch technical errors and provide CLI-specific user-friendly messages | ||
err := runDelete(cmd, args, newClient) | ||
if err != nil && errors.Is(err, fn.ErrNameRequiredForDelete) { | ||
return fmt.Errorf(`%v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly what I was thinking as well: Print the error message as it was returned from the client, and then append "cli specific" text to help out more.
Since this text is a bit long, you might want to consider breaking this out into either a separate function dedicated to error formatting, or create an "cli specific" version of the error with the full text, and the layer 1 error as a member:
return ErrDeleteNameRequired(err)
where err is a fn.ErrNameRequired and ErrDeleteNameRequired returns the CLI-specific error you have here.
Experiment with different patterns/structures to see what flows well.
Good start
hey @lkingland @gauron99 i just wanted to tell that in this case as i have created an explicit function for the typed error coz its a bit lengthy so i think in future we will need to shift this type of implementation coz it will affect the code readability as david sir you mentioned like we can create an explicit file in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Location:
I think keeping this error in the command's file (delete.go
) at the bottom of the file is fine because it is specific to a given command. If you end up creating shared errors for the CLI, then a cmd/errors.go
file would be a good place for those. The core errors (like fn.ErrNameRequired
) are shared, so putting them in a dedicated file (pkg/functions/errors.go
) is appropriate.
If you end up with more than a thousand lines of error code (approximately), it might be worth creating a dedicated file for them, cmd/delete_errors.go
Basic Implementation Suggestion:
Here's a suggestion on how to use a custom error types to help make this a bit cleaner:
// ErrDeleteNameRequired wraps core library errors with CLI-specific context
// for delete operations that require a function name or path.
type ErrDeleteNameRequired struct {
// Underlying error from the core library (e.g., fn.ErrNameRequired)
Err error
}
// NewErrDeleteNameRequired creates a new ErrDeleteNameRequired wrapping the given error
func NewErrDeleteNameRequired(err error) ErrDeleteNameRequired {
return ErrDeleteNameRequired{Err: err}
}
// Error implements the error interface with CLI-specific help text
func (e ErrDeleteNameRequired) Error() string {
return fmt.Sprintf(`%v
Function name is required for deletion (or --path not specified).
You can delete functions in two ways:
1. By name:
func delete myfunction Delete function by name
func delete myfunction --namespace apps Delete from specific namespace
2. By path:
func delete --path /path/to/function Delete function at specific path
Examples:
func delete myfunction Delete 'myfunction' from cluster
func delete myfunction --namespace prod Delete from 'prod' namespace
func delete --path ./myfunction Delete function at path
For more options, run 'func delete --help'`, e.Err)
}
The naming convention here would be "Err{Command Name}{Core Error Name}"
Complex Errors Implementation Suggestion
Now also consider how this can help with the creation of more complex help text strings by allowing you to add more context to the type as needed:
// NewErrDeleteNamespaceNotFound creates a new ErrDeleteNamespaceNotFound with the given error and namespace
func NewErrDeleteNamespaceNotFound(err error, namespace string) *ErrDeleteNamespaceNotFound {
return &ErrDeleteNamespaceNotFound{
Err: err,
Namespace: namespace,
}
}
// Error implements the error interface with CLI-specific help text
func (e *ErrDeleteNamespaceNotFound) Error() string {
return fmt.Sprintf(`%v
Namespace '%s' does not exist or is not accessible.
To resolve this issue:
{in-depth help text here}
For more options, run 'func delete --help'`,
e.Err,
e.Namespace)
}
Future Flexibility and Justification
This may seem like it's no different than the formatting function you suggested already, but consider how it allows us to do interesting things like this in the future if we need to:
// Unwrap returns the underlying error for use with errors.Is/As
func (e *ErrDeleteNameRequired) Unwrap() error {
return e.Err
}
// Is allows this error to match the underlying error type
// This enables errors.Is(err, fn.ErrNameRequired) to work through the wrapper
func (e *ErrDeleteNameRequired) Is(target error) bool {
return errors.Is(e.Err, target)
}
(No need to implement those methods now, however; that's just to give you an example of the flexibility afforded when using types.
thanks @lkingland for the clarification i will follow your suggestion and keep the error in the command file |
/retest |
2 similar comments
/retest |
/retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
Just need those tests to pass..
It looks like you might need to rebase @RayyanSeliya . This looks like its missing https://github.com/knative/func/pull/3055/files TLDR: theres been changes in one of the dependencies we use to create clusters for these tests and the instalation changed, hence none of the tests that use a cluster will work |
Sure @gauron99 will update the branch I have seen many chats in these days on slack 🙂 going on so will do it after my exam today in theate evening I was busy with my exams so was inactive !! Thx ! |
… using 2 layer approach Signed-off-by: RayyanSeliya <[email protected]>
Signed-off-by: RayyanSeliya <[email protected]>
Signed-off-by: RayyanSeliya <[email protected]>
a0c77b9
to
9c9fc4a
Compare
hey @gauron99 is this quarkus test related to the changes in the pr i guess no i have updated the branch can have a look now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: gauron99, lkingland, RayyanSeliya The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Changes
func delete
when no function name provided/kind enhancement
Fixes #3053
Previously,
func delete
would show a cryptic "name required" error that didn't help users understand how to delete functions. Now it immediately shows clear guidance on the two ways to delete functions (by name or by path) with practical examples.Follows two layer approach as suggested to do so !
Release Note