Skip to content

Commit

Permalink
Re-wrap errors (#1332)
Browse files Browse the repository at this point in the history
Use standard library for errors wrapping.

Most code changes done with `github.com/xdg-go/go-rewrap-errors` and `goimports`.

Steps followed to prepare this change:
* `find . -name "*.go" -exec go run github.com/xdg-go/go-rewrap-errors@latest -w {} \;`
* `find . -name "*.go" -exec goimports -w {} \;`
* `go mod tidy`
* Manually replaced uses of `log.Fatal(fmt.Errorf` with `log.Fatalf(...` (not really related, but took the opportunity to fix this).
* `make check-static`, and fix linting issues related to error messages.
* Fix case with nil error where `errors.Wrap` would have returned nil.
  • Loading branch information
jsoriano authored Jun 29, 2023
1 parent ab46f0f commit 43c68bc
Show file tree
Hide file tree
Showing 111 changed files with 765 additions and 786 deletions.
13 changes: 7 additions & 6 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package cmd

import (
"errors"
"fmt"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/builder"
Expand Down Expand Up @@ -55,24 +56,24 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
if signPackage {
err := files.VerifySignerConfiguration()
if err != nil {
return errors.Wrap(err, "can't verify signer configuration")
return fmt.Errorf("can't verify signer configuration: %w", err)
}
}

packageRoot, err := packages.MustFindPackageRoot()
if err != nil {
return errors.Wrap(err, "locating package root failed")
return fmt.Errorf("locating package root failed: %w", err)
}

buildDir, err := builder.BuildDirectory()
if err != nil {
return errors.Wrap(err, "can't prepare build directory")
return fmt.Errorf("can't prepare build directory: %w", err)
}
logger.Debugf("Use build directory: %s", buildDir)

targets, err := docs.UpdateReadmes(packageRoot)
if err != nil {
return errors.Wrap(err, "updating files failed")
return fmt.Errorf("updating files failed: %w", err)
}

for _, target := range targets {
Expand All @@ -87,7 +88,7 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
SkipValidation: skipValidation,
})
if err != nil {
return errors.Wrap(err, "building package failed")
return fmt.Errorf("building package failed: %w", err)
}
cmd.Printf("Package built: %s\n", target)

Expand Down
13 changes: 7 additions & 6 deletions cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
Expand Down Expand Up @@ -60,13 +61,13 @@ func setupChangelogCommand() *cobraext.Command {
func changelogAddCmd(cmd *cobra.Command, args []string) error {
packageRoot, err := packages.MustFindPackageRoot()
if err != nil {
return errors.Wrap(err, "locating package root failed")
return fmt.Errorf("locating package root failed: %w", err)
}

version, _ := cmd.Flags().GetString(cobraext.ChangelogAddVersionFlagName)
nextMode, _ := cmd.Flags().GetString(cobraext.ChangelogAddNextFlagName)
if version != "" && nextMode != "" {
return errors.Errorf("flags %q and %q cannot be used at the same time",
return fmt.Errorf("flags %q and %q cannot be used at the same time",
cobraext.ChangelogAddVersionFlagName,
cobraext.ChangelogAddNextFlagName)
}
Expand Down Expand Up @@ -109,15 +110,15 @@ func changelogAddCmd(cmd *cobra.Command, args []string) error {
func changelogCmdVersion(nextMode, packageRoot string) (*semver.Version, error) {
revisions, err := changelog.ReadChangelogFromPackageRoot(packageRoot)
if err != nil {
return nil, errors.Wrap(err, "failed to read current changelog")
return nil, fmt.Errorf("failed to read current changelog: %w", err)
}
if len(revisions) == 0 {
return semver.MustParse("0.0.0"), nil
}

version, err := semver.NewVersion(revisions[0].Version)
if err != nil {
return nil, errors.Wrapf(err, "invalid version in changelog %q", revisions[0].Version)
return nil, fmt.Errorf("invalid version in changelog %q: %w", revisions[0].Version, err)
}

switch nextMode {
Expand All @@ -133,7 +134,7 @@ func changelogCmdVersion(nextMode, packageRoot string) (*semver.Version, error)
v := version.IncPatch()
version = &v
default:
return nil, errors.Errorf("invalid value for %q: %s",
return nil, fmt.Errorf("invalid value for %q: %s",
cobraext.ChangelogAddNextFlagName, nextMode)
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package cmd

import (
"github.com/pkg/errors"
"fmt"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
Expand All @@ -27,7 +28,7 @@ func setupCheckCommand() *cobraext.Command {
setupBuildCommand(),
)
if err != nil {
return errors.Wrap(err, "checking package failed")
return fmt.Errorf("checking package failed: %w", err)
}
return nil
},
Expand Down
9 changes: 5 additions & 4 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package cmd

import (
"github.com/pkg/errors"
"fmt"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cleanup"
Expand All @@ -32,7 +33,7 @@ func cleanCommandAction(cmd *cobra.Command, args []string) error {

target, err := cleanup.Build()
if err != nil {
return errors.Wrap(err, "can't clean build resources")
return fmt.Errorf("can't clean build resources: %w", err)
}

if target != "" {
Expand All @@ -41,15 +42,15 @@ func cleanCommandAction(cmd *cobra.Command, args []string) error {

target, err = cleanup.Stack()
if err != nil {
return errors.Wrap(err, "can't clean the development stack")
return fmt.Errorf("can't clean the development stack: %w", err)
}
if target != "" {
cmd.Printf("Package removed from the development stack: %s\n", target)
}

target, err = cleanup.ServiceLogs()
if err != nil {
return errors.Wrap(err, "can't clean temporary service logs")
return fmt.Errorf("can't clean temporary service logs: %w", err)
}
if target != "" {
cmd.Printf("Temporary service logs removed: %s\n", target)
Expand Down
15 changes: 9 additions & 6 deletions cmd/create_data_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package cmd

import (
"errors"
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/packages"
Expand All @@ -31,7 +34,7 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {

packageRoot, found, err := packages.FindPackageRoot()
if err != nil {
return errors.Wrap(err, "locating package root failed")
return fmt.Errorf("locating package root failed: %w", err)
}
if !found {
return errors.New("package root not found, you can only create new data stream in the package context")
Expand Down Expand Up @@ -67,7 +70,7 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
var answers newDataStreamAnswers
err = survey.Ask(qs, &answers)
if err != nil {
return errors.Wrap(err, "prompt failed")
return fmt.Errorf("prompt failed: %w", err)
}

if answers.Type == "metrics" {
Expand All @@ -83,7 +86,7 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
}
err = survey.Ask(qs, &answers)
if err != nil {
return errors.Wrap(err, "prompt failed")
return fmt.Errorf("prompt failed: %w", err)
}

if !answers.SyntheticAndTimeSeries {
Expand All @@ -99,15 +102,15 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
}
err = survey.Ask(qs, &answers)
if err != nil {
return errors.Wrap(err, "prompt failed")
return fmt.Errorf("prompt failed: %w", err)
}
}
}

descriptor := createDataStreamDescriptorFromAnswers(answers, packageRoot)
err = archetype.CreateDataStream(descriptor)
if err != nil {
return errors.Wrap(err, "can't create new data stream")
return fmt.Errorf("can't create new data stream: %w", err)
}

cmd.Println("Done")
Expand Down
8 changes: 5 additions & 3 deletions cmd/create_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package cmd

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/licenses"
Expand Down Expand Up @@ -133,13 +135,13 @@ func createPackageCommandAction(cmd *cobra.Command, args []string) error {
var answers newPackageAnswers
err := survey.Ask(qs, &answers)
if err != nil {
return errors.Wrap(err, "prompt failed")
return fmt.Errorf("prompt failed: %w", err)
}

descriptor := createPackageDescriptorFromAnswers(answers)
err = archetype.CreatePackage(descriptor)
if err != nil {
return errors.Wrap(err, "can't create new package")
return fmt.Errorf("can't create new package: %w", err)
}

cmd.Println("Done")
Expand Down
13 changes: 6 additions & 7 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
Expand Down Expand Up @@ -82,13 +81,13 @@ func dumpInstalledObjectsCmdAction(cmd *cobra.Command, args []string) error {
}
client, err := elasticsearch.NewClient(clientOptions...)
if err != nil {
return errors.Wrap(err, "failed to initialize Elasticsearch client")
return fmt.Errorf("failed to initialize Elasticsearch client: %w", err)
}

dumper := dump.NewInstalledObjectsDumper(client.API, packageName)
n, err := dumper.DumpAll(cmd.Context(), outputPath)
if err != nil {
return errors.Wrap(err, "dump failed")
return fmt.Errorf("dump failed: %w", err)
}
if n == 0 {
cmd.Printf("No objects were dumped for package %s, is it installed?\n", packageName)
Expand Down Expand Up @@ -122,7 +121,7 @@ func dumpAgentPoliciesCmdAction(cmd *cobra.Command, args []string) error {
}
kibanaClient, err := kibana.NewClient(clientOptions...)
if err != nil {
return errors.Wrap(err, "failed to initialize Kibana client")
return fmt.Errorf("failed to initialize Kibana client: %w", err)
}

switch {
Expand All @@ -132,14 +131,14 @@ func dumpAgentPoliciesCmdAction(cmd *cobra.Command, args []string) error {
dumper := dump.NewAgentPoliciesDumper(kibanaClient)
err = dumper.DumpByName(cmd.Context(), outputPath, agentPolicy)
if err != nil {
return errors.Wrap(err, "dump failed")
return fmt.Errorf("dump failed: %w", err)
}
cmd.Printf("Dumped agent policy %s to %s\n", agentPolicy, outputPath)
case packageName != "":
dumper := dump.NewAgentPoliciesDumper(kibanaClient)
count, err := dumper.DumpByPackage(cmd.Context(), outputPath, packageName)
if err != nil {
return errors.Wrap(err, "dump failed")
return fmt.Errorf("dump failed: %w", err)
}
if count != 0 {
cmd.Printf("Dumped %d agent policies filtering by package name %s to %s\n", count, packageName, outputPath)
Expand All @@ -150,7 +149,7 @@ func dumpAgentPoliciesCmdAction(cmd *cobra.Command, args []string) error {
dumper := dump.NewAgentPoliciesDumper(kibanaClient)
count, err := dumper.DumpAll(cmd.Context(), outputPath)
if err != nil {
return errors.Wrap(err, "dump failed")
return fmt.Errorf("dump failed: %w", err)
}
if count != 0 {
cmd.Printf("Dumped %d agent policies to %s\n", count, outputPath)
Expand Down
14 changes: 7 additions & 7 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
Expand Down Expand Up @@ -67,26 +67,26 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {

kibanaClient, err := kibana.NewClient(opts...)
if err != nil {
return errors.Wrap(err, "can't create Kibana client")
return fmt.Errorf("can't create Kibana client: %w", err)
}

kibanaVersion, err := kibanaClient.Version()
if err != nil {
return errors.Wrap(err, "can't get Kibana status information")
return fmt.Errorf("can't get Kibana status information: %w", err)
}

if kibanaVersion.IsSnapshot() {
message := fmt.Sprintf("exporting dashboards from a SNAPSHOT version of Kibana (%s) is discouraged. It could lead to invalid dashboards (for example if they use features that are reverted or modified before the final release)", kibanaVersion.Version())
if !allowSnapshot {
return errors.Errorf("%s. --%s flag can be used to ignore this error", message, cobraext.AllowSnapshotFlagName)
return fmt.Errorf("%s. --%s flag can be used to ignore this error", message, cobraext.AllowSnapshotFlagName)
}
fmt.Printf("Warning: %s\n", message)
}

if len(dashboardIDs) == 0 {
dashboardIDs, err = promptDashboardIDs(kibanaClient)
if err != nil {
return errors.Wrap(err, "prompt for dashboard selection failed")
return fmt.Errorf("prompt for dashboard selection failed: %w", err)
}

if len(dashboardIDs) == 0 {
Expand All @@ -97,7 +97,7 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {

err = export.Dashboards(kibanaClient, dashboardIDs)
if err != nil {
return errors.Wrap(err, "dashboards export failed")
return fmt.Errorf("dashboards export failed: %w", err)
}

cmd.Println("Done")
Expand All @@ -107,7 +107,7 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error {
func promptDashboardIDs(kibanaClient *kibana.Client) ([]string, error) {
savedDashboards, err := kibanaClient.FindDashboards()
if err != nil {
return nil, errors.Wrap(err, "finding dashboards failed")
return nil, fmt.Errorf("finding dashboards failed: %w", err)
}

if len(savedDashboards) == 0 {
Expand Down
8 changes: 5 additions & 3 deletions cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package cmd

import (
"github.com/pkg/errors"
"errors"
"fmt"

"github.com/spf13/cobra"

"github.com/elastic/elastic-package/internal/cobraext"
Expand Down Expand Up @@ -34,7 +36,7 @@ func formatCommandAction(cmd *cobra.Command, args []string) error {

packageRoot, found, err := packages.FindPackageRoot()
if err != nil {
return errors.Wrap(err, "locating package root failed")
return fmt.Errorf("locating package root failed: %w", err)
}
if !found {
return errors.New("package root not found")
Expand All @@ -47,7 +49,7 @@ func formatCommandAction(cmd *cobra.Command, args []string) error {

err = formatter.Format(packageRoot, ff)
if err != nil {
return errors.Wrapf(err, "formatting the integration failed (path: %s, failFast: %t)", packageRoot, ff)
return fmt.Errorf("formatting the integration failed (path: %s, failFast: %t): %w", packageRoot, ff, err)
}

cmd.Println("Done")
Expand Down
Loading

0 comments on commit 43c68bc

Please sign in to comment.