Skip to content

Commit

Permalink
Correctly wrap errors everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
markusthoemmes committed Feb 12, 2021
1 parent 3763be0 commit 758624e
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ run:

linters:
enable:
- errorlint
- unconvert
- prealloc
disable:
Expand Down
2 changes: 1 addition & 1 deletion cmd/kn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func run(args []string) error {
// Get only the args provided but no options
func stripFlags(rootCmd *cobra.Command, args []string) ([]string, error) {
if err := rootCmd.ParseFlags(filterHelpOptions(args)); err != nil {
return []string{}, fmt.Errorf("error while parsing flags from args %v: %s", args, err.Error())
return []string{}, fmt.Errorf("error while parsing flags from args %v: %w", args, err)
}
return rootCmd.Flags().Args(), nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dynamic/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func groupFromUnstructured(u *unstructured.Unstructured) (string, error) {
content := u.UnstructuredContent()
group, found, err := unstructured.NestedString(content, "spec", "group")
if err != nil || !found {
return "", fmt.Errorf("can't find group for source GVR: %v", err)
return "", fmt.Errorf("can't find group for source GVR: %w", err)
}
return group, nil
}
Expand All @@ -62,7 +62,7 @@ func versionFromUnstructured(u *unstructured.Unstructured) (version string, err
// fallback to .spec.version
version, found, err = unstructured.NestedString(content, "spec", "version")
if err != nil || !found {
return version, fmt.Errorf("can't find version for source GVR: %v", err)
return version, fmt.Errorf("can't find version for source GVR: %w", err)
}
} else {
for _, v := range versions {
Expand All @@ -86,7 +86,7 @@ func resourceFromUnstructured(u *unstructured.Unstructured) (string, error) {
content := u.UnstructuredContent()
resource, found, err := unstructured.NestedString(content, "spec", "names", "plural")
if err != nil || !found {
return "", fmt.Errorf("can't find resource for source GVR: %v", err)
return "", fmt.Errorf("can't find resource for source GVR: %w", err)
}
return resource, nil
}
Expand All @@ -95,7 +95,7 @@ func kindFromUnstructured(u *unstructured.Unstructured) (string, error) {
content := u.UnstructuredContent()
kind, found, err := unstructured.NestedString(content, "spec", "names", "kind")
if !found || err != nil {
return "", fmt.Errorf("can't find source kind from source CRD: %v", err)
return "", fmt.Errorf("can't find source kind from source CRD: %w", err)
}
return kind, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ kn options`,
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true}, // wokeignore:rule=whitelist // TODO(#1031)
}
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
return fmt.Errorf("%s for '%s'", err.Error(), c.CommandPath())
return fmt.Errorf("%w for '%s'", err, c.CommandPath())
})
cmd.SetUsageFunc(templates.NewGlobalOptionsFunc())
cmd.SetHelpFunc(func(command *cobra.Command, args []string) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kn/commands/service/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func getRevisionDescriptions(client clientservingv1.KnServingClient, service *se
for _, target := range trafficTargets {
revision, err := extractRevisionFromTarget(client, target)
if err != nil {
return nil, fmt.Errorf("cannot extract revision from service %s: %v", service.Name, err)
return nil, fmt.Errorf("cannot extract revision from service %s: %w", service.Name, err)
}
revisionsSeen.Insert(revision.Name)
desc, err := newRevisionDesc(*revision, &target, service)
Expand Down Expand Up @@ -353,7 +353,7 @@ func completeWithUntargetedRevisions(client clientservingv1.KnServingClient, ser
func newRevisionDesc(revision servingv1.Revision, target *servingv1.TrafficTarget, service *servingv1.Service) (*revisionDesc, error) {
generation, err := strconv.ParseInt(revision.Labels[serving.ConfigurationGenerationLabelKey], 0, 0)
if err != nil {
return nil, fmt.Errorf("cannot extract configuration generation for revision %s: %v", revision.Name, err)
return nil, fmt.Errorf("cannot extract configuration generation for revision %s: %w", revision.Name, err)
}
revisionDesc := revisionDesc{
revision: &revision,
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func waitForService(client clientservingv1.KnServingClient, serviceName string,
func showUrl(client clientservingv1.KnServingClient, serviceName string, originalRevision string, what string, out io.Writer) error {
service, err := client.GetService(serviceName)
if err != nil {
return fmt.Errorf("cannot fetch service '%s' in namespace '%s' for extracting the URL: %v", serviceName, client.Namespace(), err)
return fmt.Errorf("cannot fetch service '%s' in namespace '%s' for extracting the URL: %w", serviceName, client.Namespace(), err)
}

url := service.Status.URL.String()
Expand Down
12 changes: 6 additions & 6 deletions pkg/kn/commands/source/duck/multisourcelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func sinkFromUnstructured(u *unstructured.Unstructured) (*duckv1.Destination, er
content := u.UnstructuredContent()
sink, found, err := unstructured.NestedFieldCopy(content, "spec", "sink")
if err != nil {
return nil, fmt.Errorf("cant find sink in given unstructured object at spec.sink field: %v", err)
return nil, fmt.Errorf("cant find sink in given unstructured object at spec.sink field: %w", err)
}

if !found {
Expand All @@ -118,12 +118,12 @@ func sinkFromUnstructured(u *unstructured.Unstructured) (*duckv1.Destination, er

sinkM, err := json.Marshal(sink)
if err != nil {
return nil, fmt.Errorf("error marshaling sink %v: %v", sink, err)
return nil, fmt.Errorf("error marshaling sink %v: %w", sink, err)
}

var sinkD duckv1.Destination
if err := json.Unmarshal(sinkM, &sinkD); err != nil {
return nil, fmt.Errorf("failed to unmarshal source sink: %v", err)
return nil, fmt.Errorf("failed to unmarshal source sink: %w", err)
}

return &sinkD, nil
Expand All @@ -133,17 +133,17 @@ func conditionsFromUnstructured(u *unstructured.Unstructured) (*duckv1.Condition
content := u.UnstructuredContent()
conds, found, err := unstructured.NestedFieldCopy(content, "status", "conditions")
if !found || err != nil {
return nil, fmt.Errorf("cant find conditions in given unstructured object at status.conditions field: %v", err)
return nil, fmt.Errorf("cant find conditions in given unstructured object at status.conditions field: %w", err)
}

condsM, err := json.Marshal(conds)
if err != nil {
return nil, fmt.Errorf("error marshaling conditions %v: %v", conds, err)
return nil, fmt.Errorf("error marshaling conditions %v: %w", conds, err)
}

var condsD duckv1.Conditions
if err := json.Unmarshal(condsM, &condsD); err != nil {
return nil, fmt.Errorf("failed to unmarshal source status conditions: %v", err)
return nil, fmt.Errorf("failed to unmarshal source status conditions: %w", err)
}

return &condsD, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/trigger/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewTriggerUpdateCommand(p *commands.KnParams) *cobra.Command {
updated, removed, err := triggerUpdateFlags.GetUpdateFilters()
if err != nil {
return fmt.Errorf(
"cannot update trigger '%s' because %s", name, err)
"cannot update trigger '%s' because %w", name, err)
}
existing := extractFilters(trigger)
b.Filters(existing.Merge(updated).Remove(removed))
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func NewRootCommand(helpFuncs *template.FuncMap) (*cobra.Command, error) {

// Add some command context when flags can not be parsed
rootCmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
return fmt.Errorf("%s for '%s'", err.Error(), c.CommandPath())
return fmt.Errorf("%w for '%s'", err, c.CommandPath())
})

// For glog parse error. TOO: Check why this is needed
Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/tablegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (h *HumanReadablePrinter) GenerateTable(obj runtime.Object, options PrintOp
func (h *HumanReadablePrinter) TableHandler(columnDefinitions []metav1beta1.TableColumnDefinition, printFunc interface{}) error {
printFuncValue := reflect.ValueOf(printFunc)
if err := ValidateRowPrintHandlerFunc(printFuncValue); err != nil {
utilruntime.HandleError(fmt.Errorf("unable to register print function: %v", err))
utilruntime.HandleError(fmt.Errorf("unable to register print function: %w", err))
return err
}
entry := &handlerEntry{
Expand Down
2 changes: 1 addition & 1 deletion pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func UpdateMaxScale(template *servingv1.RevisionTemplateSpec, max int) error {
func UpdateAutoscaleWindow(template *servingv1.RevisionTemplateSpec, window string) error {
_, err := time.ParseDuration(window)
if err != nil {
return fmt.Errorf("invalid duration for 'autoscale-window': %v", err)
return fmt.Errorf("invalid duration for 'autoscale-window': %w", err)
}
return UpdateRevisionTemplateAnnotation(template, autoscaling.WindowAnnotationKey, window)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/logging_http_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (t *LoggingHttpTransport) RoundTrip(r *http.Request) (*http.Response, error
reqBytes, err := httputil.DumpRequestOut(r, true)
if err != nil {
fmt.Fprintln(stream, "error dumping request:", err)
return nil, fmt.Errorf("dumping request: %v", err)
return nil, fmt.Errorf("dumping request: %w", err)
}
fmt.Fprintln(stream, "===== REQUEST =====")
fmt.Fprintln(stream, string(reqBytes))
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/logging_http_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type fakeTransport struct {
func (d *fakeTransport) RoundTrip(r *http.Request) (*http.Response, error) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
return nil, fmt.Errorf("dumping request: %v", err)
return nil, fmt.Errorf("dumping request: %w", err)
}
d.requestDump = string(dump)
return &http.Response{
Expand Down

0 comments on commit 758624e

Please sign in to comment.