✨ Add JSON marshalling
This commit is contained in:
parent
f27c669aeb
commit
2895433239
3 changed files with 54 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ package errors
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
|
|
@ -102,3 +103,24 @@ func (s *ErrorfTestSuite) TestSetStatusOutsideRange() {
|
|||
err.SetStatus(600)
|
||||
s.Assert().Equal(http.StatusPaymentRequired, err.Status())
|
||||
}
|
||||
|
||||
func (s *ErrorfTestSuite) TestJSON() {
|
||||
var err SettableError = Errorf(http.StatusPaymentRequired, "Hello")
|
||||
s.Assert().NotNil(err)
|
||||
|
||||
m := err.JSON()
|
||||
ma, _ := m.(map[string]any)
|
||||
s.Assert().Equal("Hello", ma["error"])
|
||||
s.Assert().Nil(ma["number"])
|
||||
|
||||
j, _ := json.Marshal(err)
|
||||
s.Assert().Equal(`{"error":"Hello"}`, string(j))
|
||||
|
||||
err.SetField("number",42)
|
||||
m = err.JSON()
|
||||
ma, _ = m.(map[string]any)
|
||||
s.Assert().Equal(42, ma["number"])
|
||||
|
||||
j, _ = json.Marshal(err)
|
||||
s.Assert().Equal(`{"error":"Hello","number":42}`, string(j))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue