Skip to content
Myr

errors_test.go

This file

Path

errors_test.go

106 lines · 3.1 KiB · Go

Last changed by

refactor(json): make jsonite optional with build tags (#1026)Bo-Yi Wubefore adoptionce670a6

History of this file

Take it

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 1–10why, one step back
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "errors"9 "testing"10

refactor(json): make jsonite optional with build tags (#1026)Bo-Yi Wubefore adoptionce670a6

line 11why, one step back
11 "github.com/gin-gonic/gin/json"

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 12–33why, one step back
12 "github.com/stretchr/testify/assert"
13)1415func TestError(t *testing.T) {16 baseError := errors.New("test error")17 err := &Error{18 Err: baseError,19 Type: ErrorTypePrivate,20 }21 assert.Equal(t, err.Error(), baseError.Error())22 assert.Equal(t, err.JSON(), H{"error": baseError.Error()})2324 assert.Equal(t, err.SetType(ErrorTypePublic), err)25 assert.Equal(t, err.Type, ErrorTypePublic)2627 assert.Equal(t, err.SetMeta("some data"), err)28 assert.Equal(t, err.Meta, "some data")29 assert.Equal(t, err.JSON(), H{30 "error": baseError.Error(),31 "meta": "some data",32 })33

Errors conforms to MarshalJSON interfaceManu Mtz-Almeidabefore adoption4eeca21

line 34why, one step back
34 jsonBytes, _ := json.Marshal(err)

Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765

line 35why, one step back
35 assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))

Errors conforms to MarshalJSON interfaceManu Mtz-Almeidabefore adoption4eeca21

line 36why, one step back
36

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 37–56why, one step back
37 err.SetMeta(H{
38 "status": "200",39 "data": "some data",40 })41 assert.Equal(t, err.JSON(), H{42 "error": baseError.Error(),43 "status": "200",44 "data": "some data",45 })4647 err.SetMeta(H{48 "error": "custom error",49 "status": "200",50 "data": "some data",51 })52 assert.Equal(t, err.JSON(), H{53 "error": "custom error",54 "status": "200",55 "data": "some data",56 })

Merge pull request #871 from gin-gonic/developJavier Provecho Fernandezbefore adoptiond459835

lines 57–63why, one step back
57
58 type customError struct {59 status string60 data string61 }62 err.SetMeta(customError{status: "200", data: "other data"})63 assert.Equal(t, err.JSON(), customError{status: "200", data: "other data"})

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 64–72why, one step back
64}
6566func TestErrorSlice(t *testing.T) {67 errs := errorMsgs{68 {Err: errors.New("first"), Type: ErrorTypePrivate},69 {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},70 {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},71 }72

Merge branch 'master' into developManu Mtz-Almeidabefore adoptione8bc8f4

line 73why, one step back
73 assert.Equal(t, errs, errs.ByType(ErrorTypeAny))

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 74–79why, one step back
74 assert.Equal(t, errs.Last().Error(), "third")
75 assert.Equal(t, errs.Errors(), []string{"first", "second", "third"})76 assert.Equal(t, errs.ByType(ErrorTypePublic).Errors(), []string{"third"})77 assert.Equal(t, errs.ByType(ErrorTypePrivate).Errors(), []string{"first", "second"})78 assert.Equal(t, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors(), []string{"first", "second", "third"})79 assert.Empty(t, errs.ByType(ErrorTypeBind))

Logger only prints internal errorsManu Mtz-Almeidabefore adoptiondde06a0

line 80why, one step back
80 assert.Empty(t, errs.ByType(ErrorTypeBind).String())

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 81–92why, one step back
81
82 assert.Equal(t, errs.String(), `Error #01: first83Error #02: second84 Meta: some data85Error #03: third86 Meta: map[status:400]87`)88 assert.Equal(t, errs.JSON(), []interface{}{89 H{"error": "first"},90 H{"error": "second", "meta": "some data"},91 H{"error": "third", "status": "400"},92 })

Errors conforms to MarshalJSON interfaceManu Mtz-Almeidabefore adoption4eeca21

line 93why, one step back
93 jsonBytes, _ := json.Marshal(errs)

Merge pull request #990 from appleboy/jsonJavier Provecho Fernandezbefore adoptionad08765

line 94why, one step back
94 assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 95–98why, one step back
95 errs = errorMsgs{
96 {Err: errors.New("first"), Type: ErrorTypePrivate},97 }98 assert.Equal(t, errs.JSON(), H{"error": "first"})

Errors conforms to MarshalJSON interfaceManu Mtz-Almeidabefore adoption4eeca21

lines 99–100why, one step back
99 jsonBytes, _ = json.Marshal(errs)
100 assert.Equal(t, string(jsonBytes), "{\"error\":\"first\"}")

Merge branch 'develop'Manu Mtz-Almeidabefore adoption9163ee5

lines 101–106why, one step back
101
102 errs = errorMsgs{}103 assert.Nil(t, errs.Last())104 assert.Nil(t, errs.JSON())105 assert.Empty(t, errs.String())106}