Skip to content
Myr

Errors conforms to MarshalJSON interface

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Manu Mtz-Almeida · committed by Manu Mtz-Almeida · parent 9163ee5 · Browse files · 2 files+17 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−1 removed

  • other:+17 added−1 removed

2 files+17 added−1 removed

modified:errors_test.go+8 added-1 removed
3 unmodified lines
45package gin67import (8 "encoding/json"9 "errors"10 "testing"1112 "github.com/stretchr/testify/assert"
17 unmodified lines
30 "error": baseError.Error(),31 "meta": "some data",32 })3334 jsonBytes, _ := json.Marshal(err)35 assert.Equal(t, string(jsonBytes), "{\"error\":\"test error\",\"meta\":\"some data\"}")3637 err.SetMeta(H{38 "status": "200",39 "data": "some data",40 })
39 unmodified lines
80 H{"error": "first"},81 H{"error": "second", "meta": "some data"},82 H{"error": "third", "status": "400"},83 })8084 jsonBytes, _ := json.Marshal(errs)85 assert.Equal(t, string(jsonBytes), "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]")86 errs = errorMsgs{87 {Err: errors.New("first"), Type: ErrorTypePrivate},88 }89 assert.Equal(t, errs.JSON(), H{"error": "first"})90 jsonBytes, _ = json.Marshal(errs)91 assert.Equal(t, string(jsonBytes), "{\"error\":\"first\"}")9293 errs = errorMsgs{}94 assert.Nil(t, errs.Last())95 assert.Nil(t, errs.JSON())
2 unmodified lines
modified:errors.go+9 added
4 unmodified lines
5package gin67import (8 "bytes"9 "encoding/json"10 "fmt"11 "reflect"12)13
46 unmodified lines
60 }61 return json62}6364func (msg *Error) MarshalJSON() ([]byte, error) {65 return json.Marshal(msg.JSON())66}6768func (msg *Error) Error() string {69 return msg.Err.Error()70}71
45 unmodified lines
117 return json118 }119}120121func (a errorMsgs) MarshalJSON() ([]byte, error) {122 return json.Marshal(a.JSON())123}124125func (a errorMsgs) String() string {126 if len(a) == 0 {127 return ""128 }
9 unmodified lines