From a849fd59f0d4e3f6bd773ff142c498cf8575e298 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:31:24 +0100 Subject: [PATCH] cmd: change exit code from 1 to 2 for syntax and usage errors --- cmd/cmd.go | 10 +++++----- cmdtest/cmdtest_test.go | 2 +- docs/content/docs.md | 6 +++--- lib/exitcode/exitcode.go | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 13b3ba34d..37a7d6f72 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -50,7 +50,6 @@ var ( version bool // Errors errorCommandNotFound = errors.New("command not found") - errorUncategorized = errors.New("uncategorized error") errorNotEnoughArguments = errors.New("not enough arguments") errorTooManyArguments = errors.New("too many arguments") ) @@ -495,8 +494,6 @@ func resolveExitCode(err error) { os.Exit(exitcode.DirNotFound) case errors.Is(err, fs.ErrorObjectNotFound): os.Exit(exitcode.FileNotFound) - case errors.Is(err, errorUncategorized): - os.Exit(exitcode.UncategorizedError) case errors.Is(err, accounting.ErrorMaxTransferLimitReached): os.Exit(exitcode.TransferExceeded) case errors.Is(err, fssync.ErrorMaxDurationReached): @@ -507,8 +504,10 @@ func resolveExitCode(err error) { os.Exit(exitcode.NoRetryError) case fserrors.IsFatalError(err): os.Exit(exitcode.FatalError) - default: + case errors.Is(err, errorCommandNotFound), errors.Is(err, errorNotEnoughArguments), errors.Is(err, errorTooManyArguments): os.Exit(exitcode.UsageError) + default: + os.Exit(exitcode.UncategorizedError) } } @@ -536,6 +535,7 @@ func Main() { if strings.HasPrefix(err.Error(), "unknown command") && selfupdateEnabled { Root.PrintErrf("You could use '%s selfupdate' to get latest features.\n\n", Root.CommandPath()) } - fs.Fatalf(nil, "Fatal error: %v", err) + fs.Logf(nil, "Fatal error: %v", err) + os.Exit(exitcode.UsageError) } } diff --git a/cmdtest/cmdtest_test.go b/cmdtest/cmdtest_test.go index c29167342..3f65284b3 100644 --- a/cmdtest/cmdtest_test.go +++ b/cmdtest/cmdtest_test.go @@ -174,7 +174,7 @@ func TestCmdTest(t *testing.T) { // Test error and error output out, err = rclone("version", "--provoke-an-error") if assert.Error(t, err) { - assert.Contains(t, err.Error(), "exit status 1") + assert.Contains(t, err.Error(), "exit status 2") assert.Contains(t, out, "Error: unknown flag") } diff --git a/docs/content/docs.md b/docs/content/docs.md index eed34ed21..1d1bdaa76 100644 --- a/docs/content/docs.md +++ b/docs/content/docs.md @@ -2868,9 +2868,9 @@ messages may not be valid after the retry. If rclone has done a retry it will log a high priority message if the retry was successful. ### List of exit codes ### - * `0` - success - * `1` - Syntax or usage error - * `2` - Error not otherwise categorised + * `0` - Success + * `1` - Error not otherwise categorised + * `2` - Syntax or usage error * `3` - Directory not found * `4` - File not found * `5` - Temporary error (one that more retries might fix) (Retry errors) diff --git a/lib/exitcode/exitcode.go b/lib/exitcode/exitcode.go index 6cedb3303..afe886197 100644 --- a/lib/exitcode/exitcode.go +++ b/lib/exitcode/exitcode.go @@ -4,10 +4,10 @@ package exitcode const ( // Success is returned when rclone finished without error. Success = iota - // UsageError is returned when there was a syntax or usage error in the arguments. - UsageError // UncategorizedError is returned for any error not categorised otherwise. UncategorizedError + // UsageError is returned when there was a syntax or usage error in the arguments. + UsageError // DirNotFound is returned when a source or destination directory is not found. DirNotFound // FileNotFound is returned when a source or destination file is not found.