-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove multierror to fix error in error handling
multierror struct has a formatting closure, making it unserializable Thus, multierror cannot be stored in temporal state Replace with jsonerr, with fallback logic for non serializable errors
- Loading branch information
Showing
5 changed files
with
113 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package shared | ||
|
||
import "encoding/json" | ||
|
||
type ErrString struct { | ||
Message string | ||
} | ||
|
||
func (e ErrString) Error() string { | ||
return e.Message | ||
} | ||
|
||
type JSONErr struct { | ||
E error | ||
} | ||
|
||
func (je JSONErr) Error() string { | ||
return je.E.Error() | ||
} | ||
|
||
func (je JSONErr) MarshalJSON() ([]byte, error) { | ||
if jm, ok := je.E.(json.Marshaler); ok { | ||
return jm.MarshalJSON() | ||
} else { | ||
return json.Marshal(je.E.Error()) | ||
} | ||
} | ||
|
||
func (je *JSONErr) UnmarshalJSON(data []byte) error { | ||
var res error | ||
err := json.Unmarshal(data, &res) | ||
if err == nil { | ||
*je = JSONErr{E: res} | ||
return nil | ||
} | ||
var msg string | ||
err = json.Unmarshal(data, &msg) | ||
if err != nil { | ||
return err | ||
} | ||
*je = JSONErr{ErrString{Message: msg}} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package shared | ||
|
||
import "encoding/json" | ||
import "testing" | ||
|
||
type jsonSafeErr struct { | ||
Err string | ||
} | ||
|
||
func (jse jsonSafeErr) Error() string { | ||
return jse.Err | ||
} | ||
|
||
type jsonUnsafeErr struct { | ||
Err string | ||
Fn func() | ||
} | ||
|
||
func (jue jsonUnsafeErr) Error() string { | ||
return jue.Err | ||
} | ||
|
||
func TestJsonSafeError(t *testing.T) { | ||
e := jsonSafeErr{Err: "test"} | ||
je := JSONErr{E: e} | ||
j, err := json.Marshal(je) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Log(j) | ||
|
||
var newje JSONErr | ||
err = json.Unmarshal(j, &newje) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
errmsg := newje.Error() | ||
if errmsg != "test" { | ||
t.Error("Expected 'test'", errmsg) | ||
} | ||
} | ||
|
||
func TestJsonUnsafeError(t *testing.T) { | ||
e := jsonUnsafeErr{Err: "test", Fn: func() {}} | ||
je := JSONErr{E: e} | ||
j, err := json.Marshal(je) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
t.Log(j) | ||
|
||
var newje JSONErr | ||
err = json.Unmarshal(j, &newje) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
errmsg := newje.Error() | ||
if errmsg != "test" { | ||
t.Error("Expected 'test'", errmsg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters