Skip to content

Commit

Permalink
fix: use cockroachdb errors (#790)
Browse files Browse the repository at this point in the history
* fix: use cockroachdb errors

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix lint

Signed-off-by: Keming <kemingyang@tensorchord.ai>

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy committed Aug 17, 2022
1 parent 7d293d7 commit 8225eab
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package builder

import (
"context"
"errors"
"os"

"github.com/cockroachdb/errors"
"github.com/golang/mock/gomock"
"github.com/moby/buildkit/client"
. "github.com/onsi/ginkgo/v2"
Expand Down
2 changes: 1 addition & 1 deletion pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ please visit https://docs.docker.com/engine/install/linux-postinstall/ for more
// Normalize the name accord the spec of docker, It may support normalize imagea and container in the future.
func NormalizeNamed(s string) (string, error) {
if ok := anchoredIdentifierRegexp.MatchString(s); ok {
return "", fmt.Errorf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings, please rename it", s)
return "", errors.Newf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings, please rename it", s)
}
var remoteName string
var tagSep int
Expand Down
2 changes: 1 addition & 1 deletion pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package ir

import (
"errors"
"github.com/cockroachdb/errors"

"github.com/tensorchord/envd/pkg/editor/vscode"
"github.com/tensorchord/envd/pkg/lang/ir/parser"
Expand Down
3 changes: 1 addition & 2 deletions pkg/lang/ir/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package ir

import (
"fmt"
"os/user"
"regexp"
"strconv"
Expand Down Expand Up @@ -43,7 +42,7 @@ func parseLanguage(l string) (string, *string, error) {
case "python", "r", "julia":
return language, &version, nil
default:
return "", nil, fmt.Errorf("language %s is not supported", language)
return "", nil, errors.Newf("language %s is not supported", language)
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ package metrics

import (
"context"
"fmt"

"github.com/cockroachdb/errors"

"github.com/tensorchord/envd/pkg/docker"
)
Expand All @@ -27,8 +28,8 @@ type Collector interface {
}

func GetCollector(name string, handle interface{}) (Collector, error) {
ErrUnknownCollector := fmt.Errorf("unknown collector: %s", name)
ErrUnknownHandle := fmt.Errorf("unknown handler: %s", name)
ErrUnknownCollector := errors.Newf("unknown collector: %s", name)
ErrUnknownHandle := errors.Newf("unknown handler: %s", name)
switch name {
case "docker":
client, ok := handle.(docker.Client)
Expand Down
4 changes: 2 additions & 2 deletions pkg/remote/sshd/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
package sshd

import (
"fmt"
"os/exec"

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
)

var (
errNoShell = fmt.Errorf("failed to find any shell in the PATH")
errNoShell = errors.Newf("failed to find any shell in the PATH")
)

// GetShell returns the shell in $PATH.
Expand Down
23 changes: 12 additions & 11 deletions pkg/ssh/config/ssh_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"

"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"

"github.com/tensorchord/envd/pkg/util/osutil"
Expand Down Expand Up @@ -236,7 +237,7 @@ func (config *sshConfig) writeToFilepath(p string) error {
var mode os.FileMode
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to get info on %s: %w", p, err)
return errors.Newf("failed to get info on %s: %w", p, err)
}

// default for sshconfig
Expand All @@ -248,7 +249,7 @@ func (config *sshConfig) writeToFilepath(p string) error {
dir := filepath.Dir(p)
temp, err := os.CreateTemp(dir, "")
if err != nil {
return fmt.Errorf("failed to create temporary config file: %w", err)
return errors.Newf("failed to create temporary config file: %w", err)
}

defer os.Remove(temp.Name())
Expand All @@ -262,15 +263,15 @@ func (config *sshConfig) writeToFilepath(p string) error {
}

if err := os.Chmod(temp.Name(), mode); err != nil {
return fmt.Errorf("failed to set permissions to %s: %w", temp.Name(), err)
return errors.Newf("failed to set permissions to %s: %w", temp.Name(), err)
}

if _, err := getConfig(temp.Name()); err != nil {
return fmt.Errorf("new config is not valid: %w", err)
return errors.Newf("new config is not valid: %w", err)
}

if err := os.Rename(temp.Name(), p); err != nil {
return fmt.Errorf("failed to move %s to %s: %w", temp.Name(), p, err)
return errors.Newf("failed to move %s to %s: %w", temp.Name(), p, err)
}

return nil
Expand Down Expand Up @@ -459,17 +460,17 @@ func GetPort(name string) (int, error) {
hostname := buildHostname(name)
i, found := findHost(cfg, hostname)
if !found {
return 0, fmt.Errorf("development container not found")
return 0, errors.Newf("development container not found")
}

param := cfg.hosts[i].getParam(portKeyword)
if param == nil {
return 0, fmt.Errorf("port not found")
return 0, errors.Newf("port not found")
}

port, err := strconv.Atoi(param.value())
if err != nil {
return 0, fmt.Errorf("invalid port: %s", param.value())
return 0, errors.Newf("invalid port: %s", param.value())
}

return port, nil
Expand Down Expand Up @@ -523,22 +524,22 @@ func getConfig(path string) (*sshConfig, error) {
}, nil
}

return nil, fmt.Errorf("can't open %s: %w", path, err)
return nil, errors.Newf("can't open %s: %w", path, err)
}

defer f.Close()

cfg, err := parse(f)
if err != nil {
return nil, fmt.Errorf("fail to decode %s: %w", path, err)
return nil, errors.Newf("fail to decode %s: %w", path, err)
}

return cfg, nil
}

func save(cfg *sshConfig, path string) error {
if err := cfg.writeToFilepath(path); err != nil {
return fmt.Errorf("fail to update SSH config file %s: %w", path, err)
return errors.Newf("fail to update SSH config file %s: %w", path, err)
}

return nil
Expand Down
22 changes: 11 additions & 11 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,18 @@ func (c generalClient) Attach() error {
}()

if err := session.RequestPty("xterm-256color", height, width, modes); err != nil {
return fmt.Errorf("request for pseudo terminal failed: %w", err)
return errors.Newf("request for pseudo terminal failed: %w", err)
}

stdin, err := session.StdinPipe()
if err != nil {
return fmt.Errorf("unable to setup stdin for session: %w", err)
return errors.Newf("unable to setup stdin for session: %w", err)
}
Copy(os.Stdin, stdin)

stdout, err := session.StdoutPipe()
if err != nil {
return fmt.Errorf("unable to setup stdout for session: %w", err)
return errors.Newf("unable to setup stdout for session: %w", err)
}

go func() {
Expand All @@ -235,7 +235,7 @@ func (c generalClient) Attach() error {

stderr, err := session.StderrPipe()
if err != nil {
return fmt.Errorf("unable to setup stderr for session: %w", err)
return errors.Newf("unable to setup stderr for session: %w", err)
}

go func() {
Expand Down Expand Up @@ -286,7 +286,7 @@ func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
// nolint
pemBlock.Bytes, err = x509.DecryptPEMBlock(pemBlock, []byte(password))
if err != nil {
return nil, fmt.Errorf("decrypting PEM block failed %w", err)
return nil, errors.Newf("decrypting PEM block failed %w", err)
}

// get RSA, EC or DSA key
Expand All @@ -298,15 +298,15 @@ func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
// generate signer instance from key
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, fmt.Errorf("creating signer from encrypted key failed %w", err)
return nil, errors.Newf("creating signer from encrypted key failed %w", err)
}

return signer, nil
} else {
// generate signer instance from plain key
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, fmt.Errorf("parsing plain private key failed %w", err)
return nil, errors.Newf("parsing plain private key failed %w", err)
}

return signer, nil
Expand All @@ -318,25 +318,25 @@ func parsePemBlock(block *pem.Block) (interface{}, error) {
case "RSA PRIVATE KEY":
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing PKCS private key failed %w", err)
return nil, errors.Newf("Parsing PKCS private key failed %w", err)
} else {
return key, nil
}
case "EC PRIVATE KEY":
key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing EC private key failed %w", err)
return nil, errors.Newf("Parsing EC private key failed %w", err)
} else {
return key, nil
}
case "DSA PRIVATE KEY":
key, err := ssh.ParseDSAPrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Parsing DSA private key failed %w", err)
return nil, errors.Newf("Parsing DSA private key failed %w", err)
} else {
return key, nil
}
default:
return nil, fmt.Errorf("Parsing private key failed, unsupported key type %q", block.Type)
return nil, errors.Newf("Parsing private key failed, unsupported key type %q", block.Type)
}
}
3 changes: 1 addition & 2 deletions pkg/util/ziputil/unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package ziputil

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -102,7 +101,7 @@ func Unzip(src string, dest string) ([]string, error) {

// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
return filenames, errors.Newf("%s: illegal file path", fpath)
}

filenames = append(filenames, fpath)
Expand Down

0 comments on commit 8225eab

Please sign in to comment.