Skip to content

feat(core): add option to return specific error codes #909

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

Merged
merged 4 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,15 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
if _, ok := err.(*interactive.InterruptError); ok {
return 130, nil, err
}
errorCode := 1
if cliErr, ok := err.(*CliError); ok && cliErr.Code != 0 {
errorCode = cliErr.Code
}
printErr := meta.Printer.Print(err, nil)
if printErr != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
return 1, nil, err
return errorCode, nil, err
}
return 0, meta.result, nil
}
104 changes: 104 additions & 0 deletions internal/core/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,108 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw test interrupt error",
Check: TestCheckExitCode(130),
}))
t.Run("exit-code", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "code",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return nil, &CliError{Code: 99}
},
},
),
UseE2EClient: true,
DisableParallel: true, // because e2e client is used
Cmd: "scw test code error",
Check: TestCheckExitCode(99),
}))
t.Run("emtpy-error", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "empty",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return nil, &CliError{Code: 99, Empty: true}
},
},
),
UseE2EClient: true,
DisableParallel: true, // because e2e client is used
Cmd: "scw test empty error",
Check: TestCheckCombine(
TestCheckExitCode(99),
TestCheckStderrGolden(),
),
}))
t.Run("emtpy-error-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "empty",
Verb: "error",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return nil, &CliError{Code: 99, Empty: true}
},
},
),
UseE2EClient: true,
DisableParallel: true, // because e2e client is used
Cmd: "scw -o json test empty error",
Check: TestCheckCombine(
TestCheckExitCode(99),
TestCheckStderrGolden(),
),
}))
t.Run("emtpy-success", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return &SuccessResult{
Empty: true,
Message: "dummy",
Details: "dummy",
Resource: "dummy",
Verb: "dummy",
}, nil
},
},
),
UseE2EClient: true,
DisableParallel: true, // because e2e client is used
Cmd: "scw test empty success",
Check: TestCheckStdoutGolden(),
}))
t.Run("emtpy-success-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return &SuccessResult{
Empty: true,
Message: "dummy",
Details: "dummy",
Resource: "dummy",
Verb: "dummy",
}, nil

},
},
),
UseE2EClient: true,
DisableParallel: true, // because e2e client is used
Cmd: "scw -o json test empty success",
Check: TestCheckStdoutGolden(),
}))
}
13 changes: 13 additions & 0 deletions internal/core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ type CliError struct {

Details string
Hint string

// Code allows to return a sepcific error code from the main binary.
Code int

// Empty tells the marshaler to not print any message for the error
Empty bool
}

func (s *CliError) Error() string {
return s.Err.Error()
}

func (s *CliError) MarshalHuman() (string, error) {
if s.Empty {
return "", nil
}
sections := []string(nil)
if s.Err != nil {
humanError := s.Err
Expand Down Expand Up @@ -63,6 +72,10 @@ func (s *CliError) MarshalHuman() (string, error) {
}

func (s *CliError) MarshalJSON() ([]byte, error) {
if s.Empty {
type emptyRes struct{}
return json.Marshal(&emptyRes{})
}
type tmpRes struct {
Message string `json:"message"`
Error error `json:"error"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions internal/printer/human.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (o *humanPrinter) Print(data interface{}, opt *human.MarshalOpt) error {
return err
}

if str == "" {
return nil
}

if _, isError := data.(error); isError {
_, err = fmt.Fprintln(o.ErrorWriter, str)
return err
Expand Down