Return type.Error instead of plain error
authorGhe Rivero <ghe.rivero@gmail.com>
Thu, 16 Mar 2017 22:46:09 +0000 (23:46 +0100)
committerGhe Rivero <ghe.rivero@hpe.com>
Sat, 18 Mar 2017 09:43:02 +0000 (10:43 +0100)
This way, we can use Error.Code attribute to check error type without
needing to regexp the message string

pkg/invoke/raw_exec.go
pkg/types/types.go

index d1bd860..93f1e75 100644 (file)
@@ -50,13 +50,9 @@ func pluginErr(err error, output []byte) error {
        if _, ok := err.(*exec.ExitError); ok {
                emsg := types.Error{}
                if perr := json.Unmarshal(output, &emsg); perr != nil {
-                       return fmt.Errorf("netplugin failed but error parsing its diagnostic message %q: %v", string(output), perr)
+                       emsg.Msg = fmt.Sprintf("netplugin failed but error parsing its diagnostic message %q: %v", string(output), perr)
                }
-               details := ""
-               if emsg.Details != "" {
-                       details = fmt.Sprintf("; %v", emsg.Details)
-               }
-               return fmt.Errorf("%v%v", emsg.Msg, details)
+               return &emsg
        }
 
        return err
index 3263015..6412756 100644 (file)
@@ -136,7 +136,11 @@ type Error struct {
 }
 
 func (e *Error) Error() string {
-       return e.Msg
+       details := ""
+       if e.Details != "" {
+               details = fmt.Sprintf("; %v", e.Details)
+       }
+       return fmt.Sprintf("%v%v", e.Msg, details)
 }
 
 func (e *Error) Print() error {