-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from zignd/fix-errors-serialization
Fix errors serialization
- Loading branch information
Showing
7 changed files
with
204 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
|
||
cover.* |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
tests: | ||
go test -v ./... | ||
go test -v -coverprofile=cover.out ./... | ||
go tool cover -html=cover.out -o=cover.html |
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,42 @@ | ||
package errors | ||
|
||
import "errors" | ||
|
||
// toMapsSlice converts an error and its causes to flat slice of maps where each map represents an error. | ||
func toMapsSlice(err error) []map[string]any { | ||
errMaps := make([]map[string]any, 0) | ||
|
||
if err == nil { | ||
return errMaps | ||
} | ||
|
||
currentErr := err | ||
for { | ||
errMap, errCause := toMapAndCause(currentErr) | ||
errMaps = append(errMaps, errMap) | ||
if errCause == nil { | ||
break | ||
} | ||
currentErr = errCause | ||
} | ||
|
||
return errMaps | ||
} | ||
|
||
// toMapAndCause converts an error to a map and extracts the cause. | ||
func toMapAndCause(err error) (map[string]any, error) { | ||
errMap := make(map[string]any) | ||
var errCause error | ||
|
||
if e, ok := err.(*Err); ok { | ||
errMap["message"] = e.Message | ||
errMap["data"] = e.Data | ||
errMap["stack"] = e.Stack | ||
errCause = e.Cause | ||
} else { | ||
errMap["message"] = err.Error() | ||
errCause = errors.Unwrap(err) | ||
} | ||
|
||
return errMap, errCause | ||
} |
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,75 @@ | ||
package errors | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestJSONMarshaling(t *testing.T) { | ||
t.Run("when marshaling a nested chain of errors.Err errors, should marshal the full chain", func(t *testing.T) { | ||
err1 := New("context timeout") | ||
err2 := Wrap(err1, "failed to connect to the database") | ||
err3 := Wrap(err2, "failed to start the server") | ||
|
||
b, err := json.MarshalIndent(err3, "", " ") | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
var errs []map[string]any | ||
err = json.Unmarshal(b, &errs) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if len(errs) != 3 { | ||
t.Fatalf("unexpected number of errors, got %d, expected %d", len(errs), 3) | ||
} | ||
|
||
if fmt.Sprint(errs[0]["message"]) != err3.(*Err).Message { | ||
t.Errorf("unexpected error message, got %q, expected %q", errs[0]["message"], err3.(*Err).Message) | ||
} | ||
|
||
if fmt.Sprint(errs[1]["message"]) != err2.(*Err).Message { | ||
t.Errorf("unexpected error message, got %q, expected %q", errs[1]["message"], err2.(*Err).Message) | ||
} | ||
|
||
if fmt.Sprint(errs[2]["message"]) != err1.(*Err).Message { | ||
t.Errorf("unexpected error message, got %q, expected %q", errs[2]["message"], err1.(*Err).Message) | ||
} | ||
}) | ||
|
||
t.Run("when marshaling a chain of errors.Err and standard errors, should marshal the full chain", func(t *testing.T) { | ||
err1 := New("context timeout") | ||
err2 := fmt.Errorf("failed to connect to the database: %w", err1) | ||
err3 := Wrap(err2, "failed to start the server") | ||
|
||
b, err := json.MarshalIndent(err3, "", " ") | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
var errs []map[string]any | ||
err = json.Unmarshal(b, &errs) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if len(errs) != 3 { | ||
t.Fatalf("unexpected number of errors, got %d, expected %d", len(errs), 3) | ||
} | ||
|
||
if fmt.Sprint(errs[0]["message"]) != err3.(*Err).Message { | ||
t.Errorf("unexpected error message, got %q, expected %q", errs[0]["message"], err3.(*Err).Message) | ||
} | ||
|
||
if fmt.Sprint(errs[1]["message"]) != err2.Error() { | ||
t.Errorf("unexpected error message, got %q, expected %q", errs[1]["message"], err2.Error()) | ||
} | ||
|
||
if fmt.Sprint(errs[2]["message"]) != err1.(*Err).Message { | ||
t.Errorf("unexpected error message, got %q, expected %q", errs[2]["message"], err1.(*Err).Message) | ||
} | ||
}) | ||
} |
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