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

fix: throw all errors from low level #216

Merged
merged 1 commit into from
Nov 21, 2023
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
15 changes: 9 additions & 6 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -118,7 +117,7 @@ func RunCurrentPkg(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
opts.SetPkgPath(pwd)

if err != nil {
reporter.ExitWithReport("kpm: internal bug: failed to load working directory")
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to load working directory.")
}

return RunPkgWithOpt(opts)
Expand Down Expand Up @@ -168,7 +167,7 @@ func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResult
// 1. Create the temporary directory to pull the tar.
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, errors.InternalBug
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}
// clean the temp dir.
defer os.RemoveAll(tmpDir)
Expand All @@ -185,7 +184,11 @@ func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResult
// 3.Get the (*.tar) file path.
matches, err := filepath.Glob(filepath.Join(localPath, constants.KCL_PKG_TAR))
if err != nil || len(matches) != 1 {
return nil, errors.FailedPull
if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedGetPkg, err, "failed to pull kcl package")
} else {
return nil, errors.FailedPull
}
}

// 4. Untar the tar file.
Expand All @@ -210,12 +213,12 @@ func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResult
func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
pkgPath, err := filepath.Abs(opts.PkgPath())
if err != nil {
return nil, errors.InternalBug
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}

kclPkg, err := pkg.LoadKclPkg(pkgPath)
if err != nil {
return nil, fmt.Errorf("kpm: failed to load package, please check the package path '%s' is valid", pkgPath)
return nil, err
}

kclPkg.SetVendorMode(opts.IsVendor())
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestRunPkgInPathInvalidPkg(t *testing.T) {
opts.Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "invalid_pkg", "not_exist.k")))
result, err := RunPkgInPath(opts)
assert.NotEqual(t, err, nil)
assert.Equal(t, err.Error(), fmt.Sprintf("kpm: failed to load package, please check the package path '%s' is valid", pkgPath))
assert.Equal(t, true, strings.Contains(err.Error(), fmt.Sprintf("kpm: could not load 'kcl.mod' in '%s'\n", pkgPath)))
assert.Equal(t, result, "")
}

Expand Down
30 changes: 17 additions & 13 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func (c *KpmClient) GetSettings() *settings.Settings {
func (c *KpmClient) LoadPkgFromPath(pkgPath string) (*pkg.KclPkg, error) {
modFile, err := c.LoadModFile(pkgPath)
if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod' in '%s'.", pkgPath))
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod' in '%s'", pkgPath))
}

// Get dependencies from kcl.mod.lock.
deps, err := c.LoadLockDeps(pkgPath)

if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod.lock' in '%s'.", pkgPath))
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod.lock' in '%s'", pkgPath))
}

return &pkg.KclPkg{
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *KpmClient) ResolveDepsMetadataInJsonStr(kclPkg *pkg.KclPkg, update bool
// 2. Serialize to JSON
jsonData, err := json.Marshal(kclPkg.Dependencies)
if err != nil {
return "", errors.InternalBug
return "", reporter.NewErrorEvent(reporter.Bug, err, "internal bug: failed to marshal the dependencies into json")
}

return string(jsonData), nil
Expand Down Expand Up @@ -299,12 +299,12 @@ func (c *KpmClient) Compile(kclPkg *pkg.KclPkg, kclvmCompiler *runner.Compiler)
func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
pkgPath, err := filepath.Abs(opts.PkgPath())
if err != nil {
return nil, errors.InternalBug
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}

kclPkg, err := pkg.LoadKclPkg(pkgPath)
if err != nil {
return nil, fmt.Errorf("kpm: failed to load package, please check the package path '%s' is valid", pkgPath)
return nil, err
}

kclPkg.SetVendorMode(opts.IsVendor())
Expand Down Expand Up @@ -417,7 +417,7 @@ func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOp
// 1. Create the temporary directory to pull the tar.
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, errors.InternalBug
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}
// clean the temp dir.
defer os.RemoveAll(tmpDir)
Expand All @@ -434,7 +434,11 @@ func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOp
// 3.Get the (*.tar) file path.
matches, err := filepath.Glob(filepath.Join(localPath, constants.KCL_PKG_TAR))
if err != nil || len(matches) != 1 {
return nil, errors.FailedPull
if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedGetPkg, err, "failed to pull kcl package")
} else {
return nil, errors.FailedPull
}
}

return c.CompileTarPkg(matches[0], opts)
Expand Down Expand Up @@ -572,14 +576,14 @@ func (c *KpmClient) Package(kclPkg *pkg.KclPkg, tarPath string, vendorMode bool)
if vendorMode {
err := c.VendorDeps(kclPkg)
if err != nil {
return errors.FailedToVendorDependency
return reporter.NewErrorEvent(reporter.FailedVendor, err, "failed to vendor dependencies")
}
}

// Tar the current kcl package into a "*.tar" file.
err := utils.TarDir(kclPkg.HomePath, tarPath)
if err != nil {
return errors.FailedToPackage
return reporter.NewErrorEvent(reporter.FailedPackage, err, "failed to package the kcl module")
}
return nil
}
Expand Down Expand Up @@ -616,24 +620,24 @@ func (c *KpmClient) VendorDeps(kclPkg *pkg.KclPkg) error {
// If there is, copy it into the 'vendor' directory.
err := copy.Copy(cacheFullPath, vendorFullPath)
if err != nil {
return errors.FailedToVendorDependency
return err
}
} else if utils.DirExists(d.GetLocalFullPath(kclPkg.HomePath)) && check(d, d.GetLocalFullPath(kclPkg.HomePath)) {
// If there is, copy it into the 'vendor' directory.
err := copy.Copy(d.GetLocalFullPath(kclPkg.HomePath), vendorFullPath)
if err != nil {
return errors.FailedToVendorDependency
return err
}
} else {
// re-download if not.
err = c.AddDepToPkg(kclPkg, &d)
if err != nil {
return errors.FailedToVendorDependency
return err
}
// re-vendor again with new kcl.mod and kcl.mod.lock
err = c.VendorDeps(kclPkg)
if err != nil {
return errors.FailedToVendorDependency
return err
}
return nil
}
Expand Down
16 changes: 3 additions & 13 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ func TestInitEmptyPkg(t *testing.T) {
assert.Equal(t, err, nil)

testKclPkg, err := pkg.LoadKclPkg(testDir)
if err != nil {
t.Errorf("Failed to 'LoadKclPkg'.")
}

assert.Equal(t, err, nil)
assert.Equal(t, testKclPkg.ModFile.Pkg.Name, "test_name")
assert.Equal(t, testKclPkg.ModFile.Pkg.Version, "0.0.1")
assert.Equal(t, testKclPkg.ModFile.Pkg.Edition, "0.0.1")
Expand Down Expand Up @@ -180,16 +177,9 @@ func TestUpdateKclModAndLock(t *testing.T) {
kclPkg.ModFile.Dependencies.Deps["test"] = dep

err = kclPkg.ModFile.StoreModFile()

if err != nil {
t.Errorf("failed to LockDepsVersion.")
}

assert.Equal(t, err, nil)
err = kclPkg.LockDepsVersion()

if err != nil {
t.Errorf("failed to LockDepsVersion.")
}
assert.Equal(t, err, nil)

expectDir := getTestDir("expected")

Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/cmd_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package cmd

import (
"fmt"
"os"

"github.com/urfave/cli/v2"
"kcl-lang.io/kcl-go/pkg/tools/gen"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/reporter"
"os"
)

// NewImportCmd new a Command for `kpm import`.
Expand Down Expand Up @@ -82,7 +83,7 @@ func NewImportCmd(kpmcli *client.KpmClient) *cli.Command {

outputWriter, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("kpm: failed to create output file: %s", outputFile)
return reporter.NewErrorEvent(reporter.FailedCreateFile, err, fmt.Sprintf("failed to create output file: %s", outputFile))
}

return gen.GenKcl(outputWriter, inputFile, nil, opt)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewInitCmd(kpmcli *client.KpmClient) *cli.Command {
pwd, err := os.Getwd()

if err != nil {
reporter.Fatal("kpm: internal bugs, please contact us to fix it")
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to load working directory.")
}

var pkgName string
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/cmd_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/errors"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewMetadataCmd(kpmcli *client.KpmClient) *cli.Command {

pwd, err := os.Getwd()
if err != nil {
return errors.InternalBug
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it")
}

kclPkg, err := pkg.LoadKclPkg(pwd)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/cmd_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/errors"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/utils"
Expand Down Expand Up @@ -48,7 +47,7 @@ func NewPkgCmd(kpmcli *client.KpmClient) *cli.Command {
pwd, err := os.Getwd()

if err != nil {
reporter.ExitWithReport("kpm: internal bug: failed to load working directory")
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to load working directory.")
}

kclPkg, err := pkg.LoadKclPkg(pwd)
Expand All @@ -62,7 +61,7 @@ func NewPkgCmd(kpmcli *client.KpmClient) *cli.Command {
if !utils.DirExists(tarPath) {
err := os.MkdirAll(tarPath, os.ModePerm)
if err != nil {
return errors.InternalBug
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to create the target directory")
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func pushTarPackage(ociUrl, localTarPath string, vendorMode bool, kpmcli *client
if kclPkg != nil && utils.DirExists(kclPkg.HomePath) {
err = os.RemoveAll(kclPkg.HomePath)
if err != nil {
err = errors.InternalBug
err = reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to clean the temp dir.")
}
}
}()
Expand Down Expand Up @@ -141,7 +141,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, kpmcli *cli
if kclPkg != nil && utils.DirExists(tarPath) {
err = os.RemoveAll(tarPath)
if err != nil {
err = errors.InternalBug
err = reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to clean the temp dir.")
}
}
}()
Expand All @@ -167,7 +167,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, kpmcli *cli
"only support url scheme 'oci://'.",
)
}

ociOpts.Annotations, err = oci.GenOciManifestFromPkg(kclPkg)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/cmd_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/reporter"
)

Expand Down Expand Up @@ -45,7 +44,7 @@ func KpmUpdate(c *cli.Context, kpmcli *client.KpmClient) error {
if len(input_paths) == 0 {
pwd, err := os.Getwd()
if err != nil {
return errors.InternalBug
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it")
}
pkg_paths = append(pkg_paths, pwd)
} else {
Expand Down
17 changes: 12 additions & 5 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/thoas/go-funk"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
Expand All @@ -36,7 +35,11 @@ func Login(hostname, username, password string, setting *settings.Settings) erro
authClient, err := dockerauth.NewClientWithDockerFallback(setting.CredentialsFile)

if err != nil {
return errors.FailedLogin
return reporter.NewErrorEvent(
reporter.FailedLogin,
err,
fmt.Sprintf("failed to login '%s', please check registry, username and password is valid", hostname),
)
}

err = authClient.LoginWithOpts(
Expand All @@ -48,7 +51,11 @@ func Login(hostname, username, password string, setting *settings.Settings) erro
)

if err != nil {
return errors.FailedLogin
return reporter.NewErrorEvent(
reporter.FailedLogin,
err,
fmt.Sprintf("failed to login '%s', please check registry, username and password is valid", hostname),
)
}

return nil
Expand All @@ -60,13 +67,13 @@ func Logout(hostname string, setting *settings.Settings) error {
authClient, err := dockerauth.NewClientWithDockerFallback(setting.CredentialsFile)

if err != nil {
return errors.FailedLogout
return reporter.NewErrorEvent(reporter.FailedLogout, err, fmt.Sprintf("failed to logout '%s'", hostname))
}

err = authClient.Logout(context.Background(), hostname)

if err != nil {
return errors.FailedLogout
return reporter.NewErrorEvent(reporter.FailedLogout, err, fmt.Sprintf("failed to logout '%s'", hostname))
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/settings"
"kcl-lang.io/kpm/pkg/utils"
)
Expand Down Expand Up @@ -39,5 +38,5 @@ func TestLogin(t *testing.T) {
userPwd := "invalid_password"

err := Login(hostName, userName, userPwd, &settings)
assert.Equal(t, err, errors.FailedLogin)
assert.Equal(t, err.Error(), "kpm: failed to login 'ghcr.io', please check registry, username and password is valid\nkpm: Get \"https://ghcr.io/v2/\": denied: denied\n")
}
Loading
Loading