errors_test.go
This file
Path
errors_test.go
140 lines · 4.2 KiB · Go
Last changed by
feat: support custom json codec at runtime (#3391)Timbefore adoption688a429
History of this fileTake it
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 "fmt"10 "testing"1112 "github.com/gin-gonic/gin/codec/json"13 "github.com/stretchr/testify/assert"14 "github.com/stretchr/testify/require"15)1617func TestError(t *testing.T) {18 baseError := errors.New("test error")19 err := &Error{20 Err: baseError,21 Type: ErrorTypePrivate,22 }23 assert.Equal(t, err.Error(), baseError.Error())24 assert.Equal(t, H{"error": baseError.Error()}, err.JSON())2526 assert.Equal(t, err.SetType(ErrorTypePublic), err)27 assert.Equal(t, ErrorTypePublic, err.Type)2829 assert.Equal(t, err.SetMeta("some data"), err)30 assert.Equal(t, "some data", err.Meta)31 assert.Equal(t, H{32 "error": baseError.Error(),33 "meta": "some data",34 }, err.JSON())3536 jsonBytes, _ := json.API.Marshal(err)37 assert.JSONEq(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))3839 err.SetMeta(H{ //nolint: errcheck40 "status": "200",41 "data": "some data",42 })43 assert.Equal(t, H{44 "error": baseError.Error(),45 "status": "200",46 "data": "some data",47 }, err.JSON())4849 err.SetMeta(H{ //nolint: errcheck50 "error": "custom error",51 "status": "200",52 "data": "some data",53 })54 assert.Equal(t, H{55 "error": "custom error",56 "status": "200",57 "data": "some data",58 }, err.JSON())5960 type customError struct {61 status string62 data string63 }64 err.SetMeta(customError{status: "200", data: "other data"}) //nolint: errcheck65 assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())66}6768func TestErrorSlice(t *testing.T) {69 errs := errorMsgs{70 {Err: errors.New("first"), Type: ErrorTypePrivate},71 {Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},72 {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},73 }7475 assert.Equal(t, errs, errs.ByType(ErrorTypeAny))76 assert.Equal(t, "third", errs.Last().Error())77 assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())78 assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())79 assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())80 assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())81 assert.Empty(t, errs.ByType(ErrorTypeBind))82 assert.Empty(t, errs.ByType(ErrorTypeBind).String())8384 assert.Equal(t, `Error #01: first85Error #02: second86 Meta: some data87Error #03: third88 Meta: map[status:400]89`, errs.String())90 assert.Equal(t, []any{91 H{"error": "first"},92 H{"error": "second", "meta": "some data"},93 H{"error": "third", "status": "400"},94 }, errs.JSON())95 jsonBytes, _ := json.API.Marshal(errs)96 assert.JSONEq(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))97 errs = errorMsgs{98 {Err: errors.New("first"), Type: ErrorTypePrivate},99 }100 assert.Equal(t, H{"error": "first"}, errs.JSON())101 jsonBytes, _ = json.API.Marshal(errs)102 assert.JSONEq(t, "{\"error\":\"first\"}", string(jsonBytes))103104 errs = errorMsgs{}105 assert.Nil(t, errs.Last())106 assert.Nil(t, errs.JSON())107 assert.Empty(t, errs.String())108}109110type TestErr string111112func (e TestErr) Error() string { return string(e) }113114// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()".115// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13.116func TestErrorUnwrap(t *testing.T) {117 innerErr := TestErr("some error")118119 // 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr120 err := fmt.Errorf("wrapped: %w", &Error{121 Err: innerErr,122 Type: ErrorTypeAny,123 })124125 // check that 'errors.Is()' and 'errors.As()' behave as expected :126 require.ErrorIs(t, err, innerErr)127 var testErr TestErr128 require.ErrorAs(t, err, &testErr)129130 // Test non-pointer usage of gin.Error131 errNonPointer := Error{132 Err: innerErr,133 Type: ErrorTypeAny,134 }135 wrappedErr := fmt.Errorf("wrapped: %w", errNonPointer)136 // Check that 'errors.Is()' and 'errors.As()' behave as expected for non-pointer usage137 require.ErrorIs(t, wrappedErr, innerErr)138 var testErrNonPointer TestErr139 require.ErrorAs(t, wrappedErr, &testErrNonPointer)140}