diff --git a/pkg/iodine/iodine.go b/pkg/iodine/iodine.go index c37453827..adc2f9a6b 100644 --- a/pkg/iodine/iodine.go +++ b/pkg/iodine/iodine.go @@ -19,6 +19,7 @@ package iodine import ( "bytes" "encoding/json" + "errors" "fmt" "os" "path" @@ -93,6 +94,27 @@ func GetGlobalStateKey(k string) string { return result } +func ToError(err error) error { + switch err := err.(type) { + case nil: + { + return nil + } + case Error: + { + if err.EmbeddedError != nil { + return err.EmbeddedError + } else { + return errors.New(err.ErrorMessage) + } + } + default: + { + return err + } + } +} + // New - instantiate an error, turning it into an iodine error. // Adds an initial stack trace. func New(err error, data map[string]string) error { diff --git a/pkg/iodine/iodine_test.go b/pkg/iodine/iodine_test.go index f13a11c7a..c7fa0a132 100644 --- a/pkg/iodine/iodine_test.go +++ b/pkg/iodine/iodine_test.go @@ -22,6 +22,7 @@ import ( "testing" "encoding/json" + "os" ) func TestIodine(t *testing.T) { @@ -101,3 +102,15 @@ func TestState(t *testing.T) { } } } + +func TestToError(t *testing.T) { + _, err := os.Stat("hello") + ierr := New(err, nil) + if ToError(ierr) != err { + t.Error("Error is not the same") + } + ierr = New(ierr, nil) + if ToError(ierr) != err { + t.Error("Stacked Error is not the same") + } +}