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

[Feature] add err-logic #24

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $ %s chains list
$ %s ch l`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
if app.Config == nil {
return fmt.Errorf("config does not exist: %s", app.HomePath)
return relayer.ErrConfigNotExist(app.HomePath)
}

i := 1
Expand Down
3 changes: 1 addition & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ $ %s config show --home %s
$ %s cfg list`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
if app.Config == nil {
return fmt.Errorf("config does not exist: %s", app.HomePath)
return relayer.ErrConfigNotExist(app.HomePath)
}

b, err := toml.Marshal(app.Config)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/bandprotocol/falcon/internal/relayertest"
"github.com/bandprotocol/falcon/relayer"
)

func TestConfigShow(t *testing.T) {
Expand Down Expand Up @@ -133,5 +134,5 @@ func TestConfigInitAlreadyExist(t *testing.T) {
require.FileExists(t, cfgPath)

res = sys.RunWithInput(t, "config", "init")
require.ErrorContains(t, res.Err, "config already exists")
require.Error(t, res.Err, relayer.ErrConfigNotExist(sys.HomeDir))
}
46 changes: 21 additions & 25 deletions relayer/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (a *App) InitConfigFile(homePath string, customFilePath string) error {
// check if the config file already exists
// https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go
if _, err := os.Stat(cfgPath); err == nil {
return fmt.Errorf("config already exists: %s", cfgPath)
return ErrConfigExist(cfgPath)
} else if !os.IsNotExist(err) {
return err
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (a *App) InitPassphrase() error {
// QueryTunnelInfo queries tunnel information by given tunnel ID
func (a *App) QueryTunnelInfo(ctx context.Context, tunnelID uint64) (*types.Tunnel, error) {
if a.Config == nil {
return nil, fmt.Errorf("config is not initialized")
return nil, ErrConfigNotExist(a.HomePath)
}

tunnel, err := a.BandClient.GetTunnel(ctx, tunnelID)
Expand Down Expand Up @@ -287,7 +287,7 @@ func (a *App) QueryTunnelInfo(ctx context.Context, tunnelID uint64) (*types.Tunn
// QueryTunnelPacketInfo queries tunnel packet information by given tunnel ID
func (a *App) QueryTunnelPacketInfo(ctx context.Context, tunnelID uint64, sequence uint64) (*bandtypes.Packet, error) {
if a.Config == nil {
return nil, fmt.Errorf("config is not initialized")
return nil, ErrConfigNotExist(a.HomePath)
}

return a.BandClient.GetTunnelPacket(ctx, tunnelID, sequence)
Expand All @@ -296,11 +296,11 @@ func (a *App) QueryTunnelPacketInfo(ctx context.Context, tunnelID uint64, sequen
// AddChainConfig adds a new chain configuration to the config file.
func (a *App) AddChainConfig(chainName string, filePath string) error {
if a.Config == nil {
return fmt.Errorf("config does not exist: %s", a.HomePath)
return ErrConfigNotExist(a.HomePath)
}

if _, ok := a.Config.TargetChains[chainName]; ok {
return fmt.Errorf("existing chain name : %s", chainName)
return ErrChainNameExist(chainName)
}

chainProviderConfig, err := LoadChainConfig(filePath)
Expand All @@ -325,11 +325,11 @@ func (a *App) AddChainConfig(chainName string, filePath string) error {
// DeleteChainConfig deletes the chain configuration from the config file.
func (a *App) DeleteChainConfig(chainName string) error {
if a.Config == nil {
return fmt.Errorf("config does not exist: %s", a.HomePath)
return ErrConfigNotExist(a.HomePath)
}

if _, ok := a.Config.TargetChains[chainName]; !ok {
return fmt.Errorf("not existing chain name : %s", chainName)
return ErrChainNameNotExist(chainName)
}

delete(a.Config.TargetChains, chainName)
Expand All @@ -349,13 +349,13 @@ func (a *App) DeleteChainConfig(chainName string) error {
// GetChainConfig retrieves the chain configuration by given chain name.
func (a *App) GetChainConfig(chainName string) (chains.ChainProviderConfig, error) {
if a.Config == nil {
return nil, fmt.Errorf("config does not exist: %s", a.HomePath)
return nil, ErrConfigNotExist(a.HomePath)
}

chainProviders := a.Config.TargetChains

if _, ok := chainProviders[chainName]; !ok {
return nil, fmt.Errorf("not existing chain name : %s", chainName)
return nil, ErrChainNameNotExist(chainName)
}

return chainProviders[chainName], nil
Expand All @@ -372,7 +372,7 @@ func (a *App) AddKey(
index uint,
) (*chainstypes.Key, error) {
if a.Config == nil {
return nil, fmt.Errorf("config does not exist: %s", a.HomePath)
return nil, ErrConfigNotExist(a.HomePath)
}

if err := a.ValidatePassphrase(a.EnvPassphrase); err != nil {
Expand All @@ -381,7 +381,7 @@ func (a *App) AddKey(

cp, exist := a.TargetChains[chainName]
if !exist {
return nil, fmt.Errorf("chain name does not exist: %s", chainName)
return nil, ErrChainNameNotExist(chainName)
}

keyOutput, err := cp.AddKey(keyName, mnemonic, privateKey, a.HomePath, coinType, account, index, a.EnvPassphrase)
Expand All @@ -395,7 +395,7 @@ func (a *App) AddKey(
// DeleteKey deletes the key from the chain provider.
func (a *App) DeleteKey(chainName string, keyName string) error {
if a.Config == nil {
return fmt.Errorf("config does not exist: %s", a.HomePath)
return ErrConfigNotExist(a.HomePath)
}

if err := a.ValidatePassphrase(a.EnvPassphrase); err != nil {
Expand All @@ -404,7 +404,7 @@ func (a *App) DeleteKey(chainName string, keyName string) error {

cp, exist := a.TargetChains[chainName]
if !exist {
return fmt.Errorf("chain name does not exist: %s", chainName)
return ErrChainNameNotExist(chainName)
}

return cp.DeleteKey(a.HomePath, keyName, a.EnvPassphrase)
Expand All @@ -413,7 +413,7 @@ func (a *App) DeleteKey(chainName string, keyName string) error {
// ExportKey exports the private key from the chain provider.
func (a *App) ExportKey(chainName string, keyName string) (string, error) {
if a.Config == nil {
return "", fmt.Errorf("config does not exist: %s", a.HomePath)
return "", ErrConfigNotExist(a.HomePath)
}

if err := a.ValidatePassphrase(a.EnvPassphrase); err != nil {
Expand All @@ -422,7 +422,7 @@ func (a *App) ExportKey(chainName string, keyName string) (string, error) {

cp, exist := a.TargetChains[chainName]
if !exist {
return "", fmt.Errorf("chain name does not exist: %s", chainName)
return "", ErrChainNameNotExist(chainName)
}

privateKey, err := cp.ExportPrivateKey(keyName, a.EnvPassphrase)
Expand Down Expand Up @@ -450,12 +450,12 @@ func (a *App) ListKeys(chainName string) ([]*chainstypes.Key, error) {
// ShowKey retrieves the key information from the chain provider.
func (a *App) ShowKey(chainName string, keyName string) (string, error) {
if a.Config == nil {
return "", fmt.Errorf("config does not exist: %s", a.HomePath)
return "", ErrConfigNotExist(a.HomePath)
}

cp, exist := a.TargetChains[chainName]
if !exist {
return "", fmt.Errorf("chain name does not exist: %s", chainName)
return "", ErrChainNameNotExist(chainName)
}

return cp.ShowKey(keyName)
Expand All @@ -464,17 +464,13 @@ func (a *App) ShowKey(chainName string, keyName string) (string, error) {
// QueryBalance retrieves the balance of the key from the chain provider.
func (a *App) QueryBalance(ctx context.Context, chainName string, keyName string) (*big.Int, error) {
if a.Config == nil {
return nil, fmt.Errorf("config does not exist: %s", a.HomePath)
return nil, ErrConfigNotExist(a.HomePath)
}

cp, exist := a.TargetChains[chainName]

if !exist {
return nil, fmt.Errorf("chain name does not exist: %s", chainName)
}

if !cp.IsKeyNameExist(keyName) {
return nil, fmt.Errorf("key name does not exist: %s", chainName)
return nil, ErrChainNameNotExist(chainName)
}

return cp.QueryBalance(ctx, keyName)
Expand Down Expand Up @@ -559,7 +555,7 @@ func (a *App) Start(ctx context.Context, tunnelIDs []uint64) error {
for _, tunnel := range tunnels {
chainProvider, ok := a.TargetChains[tunnel.TargetChainID]
if !ok {
return fmt.Errorf("target chain provider not found: %s", tunnel.TargetChainID)
return ErrChainNameNotExist(tunnel.TargetChainID)
}

tr := NewTunnelRelayer(
Expand Down Expand Up @@ -604,7 +600,7 @@ func (a *App) Relay(ctx context.Context, tunnelID uint64) error {

chainProvider, ok := a.TargetChains[tunnel.TargetChainID]
if !ok {
return fmt.Errorf("target chain provider not found: %s", tunnel.TargetChainID)
return ErrChainNameNotExist(tunnel.TargetChainID)
}

if err := chainProvider.LoadFreeSenders(a.HomePath, a.EnvPassphrase); err != nil {
Expand Down
20 changes: 10 additions & 10 deletions relayer/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *AppTestSuite) TestInitConfig() {
s.Require().NoError(err)
},
in: "",
err: fmt.Errorf("config already exists:"),
err: relayer.ErrConfigExist(s.app.HomePath),
},
{
name: "init config from specific file",
Expand Down Expand Up @@ -208,7 +208,7 @@ func (s *AppTestSuite) TestAddChainConfig() {
err := os.WriteFile(chainCfgPath, []byte(relayertest.ChainCfgInvalidChainTypeText), 0o600)
s.Require().NoError(err)
},
err: fmt.Errorf("unsupported chain type"),
err: relayer.ErrUnsupportedChainType(""),
},
{
name: "existing chain name",
Expand All @@ -226,7 +226,7 @@ func (s *AppTestSuite) TestAddChainConfig() {
err := os.WriteFile(chainCfgPath, []byte(relayertest.ChainCfgText), 0o600)
s.Require().NoError(err)
},
err: fmt.Errorf("existing chain name :"),
err: relayer.ErrChainNameExist("testnet"),
},
}

Expand Down Expand Up @@ -292,7 +292,7 @@ func (s *AppTestSuite) TestDeleteChainConfig() {
{
name: "not existing chain name",
in: "testnet2",
err: fmt.Errorf("not existing chain name"),
err: relayer.ErrChainNameNotExist("testnet2"),
},
}

Expand Down Expand Up @@ -340,7 +340,7 @@ func (s *AppTestSuite) TestGetChainConfig() {
{
name: "not existing chain name",
in: "testnet_evm2",
err: fmt.Errorf("not existing chain name"),
err: relayer.ErrChainNameNotExist("testnet_evm2"),
},
}

Expand Down Expand Up @@ -559,7 +559,7 @@ func (s *AppTestSuite) TestAddKey() {
keyName: "testkey",
privateKey: "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // anvil
coinType: 60,
err: fmt.Errorf("chain name does not exist:"),
err: relayer.ErrChainNameNotExist("testnet_evm2"),
},
}

Expand Down Expand Up @@ -622,7 +622,7 @@ func (s *AppTestSuite) TestDeleteKey() {
name: "chain name does not exist",
chainName: "testnet_evm2",
keyName: "testkey",
err: fmt.Errorf("chain name does not exist:"),
err: relayer.ErrChainNameNotExist("testnet_evm2"),
},
}

Expand Down Expand Up @@ -678,7 +678,7 @@ func (s *AppTestSuite) TestExportKey() {
name: "chain name does not exist",
chainName: "testnet_evm2",
keyName: "testkey",
err: fmt.Errorf("chain name does not exist:"),
err: relayer.ErrChainNameNotExist("testnet_evm2"),
},
}

Expand Down Expand Up @@ -726,7 +726,7 @@ func (s *AppTestSuite) TestListKeys() {
{
name: "chain name does not exist",
in: "testnet_evm2",
err: fmt.Errorf("chain name does not exist:"),
err: relayer.ErrChainNameNotExist("testnet_evm2"),
},
}

Expand Down Expand Up @@ -783,7 +783,7 @@ func (s *AppTestSuite) TestShowKey() {
name: "chain name does not exist",
chainName: "testnet_evm2",
keyName: "testkey",
err: fmt.Errorf("chain name does not exist:"),
err: relayer.ErrChainNameNotExist("testnet_evm2"),
},
}

Expand Down
12 changes: 6 additions & 6 deletions relayer/band/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *client) startLivelinessCheck(ctx context.Context, timeout uint, interva
func (c *client) GetTunnel(ctx context.Context, tunnelID uint64) (*types.Tunnel, error) {
// check connection to bandchain
if c.QueryClient == nil {
return nil, fmt.Errorf("cannot connect to bandchain")
return nil, ErrBandChainNotConnect
}

res, err := c.QueryClient.Tunnel(ctx, &tunneltypes.QueryTunnelRequest{
Expand All @@ -141,7 +141,7 @@ func (c *client) GetTunnel(ctx context.Context, tunnelID uint64) (*types.Tunnel,
}

if res.Tunnel.Route.TypeUrl != "/band.tunnel.v1beta1.TSSRoute" {
return nil, fmt.Errorf("unsupported route type: %s", res.Tunnel.Route.TypeUrl)
return nil, ErrUnsupportedRouteType(res.Tunnel.Route.TypeUrl)
}

// Extract route information
Expand All @@ -153,7 +153,7 @@ func (c *client) GetTunnel(ctx context.Context, tunnelID uint64) (*types.Tunnel,

tssRoute, ok := route.(*tunneltypes.TSSRoute)
if !ok {
return nil, fmt.Errorf("unsupported route type: %T", route)
return nil, ErrUnsupportedRouteType(route.String())
}

return types.NewTunnel(
Expand All @@ -169,7 +169,7 @@ func (c *client) GetTunnel(ctx context.Context, tunnelID uint64) (*types.Tunnel,
func (c *client) GetTunnelPacket(ctx context.Context, tunnelID uint64, sequence uint64) (*types.Packet, error) {
// check connection to bandchain
if c.QueryClient == nil {
return nil, fmt.Errorf("cannot connect to bandchain")
return nil, ErrBandChainNotConnect
}

// Get packet information by given tunnel ID and sequence
Expand Down Expand Up @@ -199,7 +199,7 @@ func (c *client) GetTunnelPacket(ctx context.Context, tunnelID uint64, sequence

tssPacketReceipt, ok := packetReceipt.(*tunneltypes.TSSPacketReceipt)
if !ok {
return nil, fmt.Errorf("unsupported packet content type: %T", packetReceipt)
return nil, ErrUnsupportedPacketContentType(packetReceipt)
}
signingID := uint64(tssPacketReceipt.SigningID)

Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *client) GetTunnelPacket(ctx context.Context, tunnelID uint64, sequence
func (c *client) GetTunnels(ctx context.Context) ([]types.Tunnel, error) {
// check connection to bandchain
if c.QueryClient == nil {
return nil, fmt.Errorf("cannot connect to BandChain")
return nil, ErrBandChainNotConnect
}

tunnels := make([]types.Tunnel, 0)
Expand Down
4 changes: 2 additions & 2 deletions relayer/band/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *ClientTestSuite) TestGetTunnel() {
{
name: "unsupported route type",
in: 2,
err: fmt.Errorf("unsupported route type"),
err: band.ErrUnsupportedRouteType(""),
preprocess: func(c context.Context) {
s.bandQueryClient.EXPECT().Tunnel(s.ctx, &tunneltypes.QueryTunnelRequest{
TunnelId: uint64(2),
Expand Down Expand Up @@ -286,7 +286,7 @@ func (s *ClientTestSuite) TestGetOtherTunnelPacket() {

// actual result
_, err = s.client.GetTunnelPacket(s.ctx, uint64(1), uint64(100))
s.Require().ErrorContains(err, "unsupported packet content type")
s.Require().ErrorContains(err, band.ErrUnsupportedPacketContentType(msg).Error())
}

func (s *ClientTestSuite) TestGetTunnels() {
Expand Down
19 changes: 19 additions & 0 deletions relayer/band/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package band

import (
"fmt"

tunneltypes "github.com/bandprotocol/falcon/internal/bandchain/tunnel"
)

var (
ErrBandChainNotConnect = fmt.Errorf("cannot connect to Bandchain")

ErrUnsupportedRouteType = func(route string) error {
return fmt.Errorf("unsupported route type: %s", route)
}

ErrUnsupportedPacketContentType = func(packetReceipt tunneltypes.PacketReceiptI) error {
return fmt.Errorf("unsupported packet content type: %T", packetReceipt)
}
)
Loading
Loading