Skip to content

Commit

Permalink
[WIP] Fix error return bug and some minor code style (temporalio#4234)
Browse files Browse the repository at this point in the history
* Fix error return bug and some minor code style

* Add unit tests

* Update error formatting
  • Loading branch information
mindaugasrukas authored and samanbarghi committed May 2, 2023
1 parent 90b53e3 commit 9e5f61f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
15 changes: 7 additions & 8 deletions tools/cassandra/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package cassandra

import (
"fmt"
"strings"

"github.com/urfave/cli"
Expand Down Expand Up @@ -96,7 +97,8 @@ func createKeyspace(cli *cli.Context, logger log.Logger) error {
}
keyspace := cli.String(schema.CLIOptKeyspace)
if keyspace == "" {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError("missing "+flag(schema.CLIOptKeyspace)+" argument ")))
err := fmt.Errorf("missing %s argument", flag(schema.CLIOptKeyspace))
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError(err.Error())))
return err
}
err = doCreateKeyspace(config, keyspace, logger)
Expand All @@ -115,7 +117,8 @@ func dropKeyspace(cli *cli.Context, logger log.Logger) error {
}
keyspace := cli.String(schema.CLIOptKeyspace)
if keyspace == "" {
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError("missing "+flag(schema.CLIOptKeyspace)+" argument ")))
err := fmt.Errorf("missing %s argument", flag(schema.CLIOptKeyspace))
logger.Error("Unable to read config.", tag.Error(schema.NewConfigError(err.Error())))
return err
}
err = doDropKeyspace(config, keyspace, logger)
Expand Down Expand Up @@ -161,12 +164,8 @@ func doDropKeyspace(cfg *CQLClientConfig, name string, logger log.Logger) error
if err != nil {
return err
}
err = client.dropKeyspace(name)
if err != nil {
return err
}
client.Close()
return nil
defer client.Close()
return client.dropKeyspace(name)
}

func newCQLClientConfig(cli *cli.Context) (*CQLClientConfig, error) {
Expand Down
26 changes: 26 additions & 0 deletions tools/cassandra/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@ func (s *HandlerTestSuite) TestParsingOfOptionsMap() {

s.Assert().Equal(0, len(parsedMap2))
}

func (s *HandlerTestSuite) TestDropKeyspaceError() {
// fake exit function to avoid exiting the application
back := osExit
defer func() { osExit = back }()
osExit = func(i int) {
s.Equal(1, i)
}
args := []string{"./tool", "drop-keyspace", "-f", "--keyspace", ""}
app := buildCLIOptions()
err := app.Run(args)
s.Nil(err)
}

func (s *HandlerTestSuite) TestCreateKeyspaceError() {
// fake exit function to avoid exiting the application
back := osExit
defer func() { osExit = back }()
osExit = func(i int) {
s.Equal(1, i)
}
args := []string{"./tool", "create-keyspace", "--keyspace", ""}
app := buildCLIOptions()
err := app.Run(args)
s.Nil(err)
}
4 changes: 3 additions & 1 deletion tools/cassandra/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func RunTool(args []string) error {
return app.Run(args)
}

var osExit = os.Exit

// root handler for all cli commands
func cliHandler(c *cli.Context, handler func(c *cli.Context, logger log.Logger) error, logger log.Logger) {
quiet := c.GlobalBool(schema.CLIOptQuiet)
err := handler(c, logger)
if err != nil && !quiet {
os.Exit(1)
osExit(1)
}
}

Expand Down

0 comments on commit 9e5f61f

Please sign in to comment.