Skip to content
Myr

errors_test.go

This file

Path

errors_test.go

128 lines · 3.8 KiB · Go

Last changed by

chore: upgrade golangci-lint and fix golangci-lint error (#3278)thinkeroubefore adoptionb04917c

History of this file

Take 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/internal/json"13	"github.com/stretchr/testify/assert"14)1516func TestError(t *testing.T) {17	baseError := errors.New("test error")18	err := &Error{19		Err:  baseError,20		Type: ErrorTypePrivate,21	}22	assert.Equal(t, err.Error(), baseError.Error())23	assert.Equal(t, H{"error": baseError.Error()}, err.JSON())2425	assert.Equal(t, err.SetType(ErrorTypePublic), err)26	assert.Equal(t, ErrorTypePublic, err.Type)2728	assert.Equal(t, err.SetMeta("some data"), err)29	assert.Equal(t, "some data", err.Meta)30	assert.Equal(t, H{31		"error": baseError.Error(),32		"meta":  "some data",33	}, err.JSON())3435	jsonBytes, _ := json.Marshal(err)36	assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))3738	err.SetMeta(H{ //nolint: errcheck39		"status": "200",40		"data":   "some data",41	})42	assert.Equal(t, H{43		"error":  baseError.Error(),44		"status": "200",45		"data":   "some data",46	}, err.JSON())4748	err.SetMeta(H{ //nolint: errcheck49		"error":  "custom error",50		"status": "200",51		"data":   "some data",52	})53	assert.Equal(t, H{54		"error":  "custom error",55		"status": "200",56		"data":   "some data",57	}, err.JSON())5859	type customError struct {60		status string61		data   string62	}63	err.SetMeta(customError{status: "200", data: "other data"}) //nolint: errcheck64	assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())65}6667func TestErrorSlice(t *testing.T) {68	errs := errorMsgs{69		{Err: errors.New("first"), Type: ErrorTypePrivate},70		{Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},71		{Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},72	}7374	assert.Equal(t, errs, errs.ByType(ErrorTypeAny))75	assert.Equal(t, "third", errs.Last().Error())76	assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())77	assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())78	assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())79	assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())80	assert.Empty(t, errs.ByType(ErrorTypeBind))81	assert.Empty(t, errs.ByType(ErrorTypeBind).String())8283	assert.Equal(t, `Error #01: first84Error #02: second85     Meta: some data86Error #03: third87     Meta: map[status:400]88`, errs.String())89	assert.Equal(t, []any{90		H{"error": "first"},91		H{"error": "second", "meta": "some data"},92		H{"error": "third", "status": "400"},93	}, errs.JSON())94	jsonBytes, _ := json.Marshal(errs)95	assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))96	errs = errorMsgs{97		{Err: errors.New("first"), Type: ErrorTypePrivate},98	}99	assert.Equal(t, H{"error": "first"}, errs.JSON())100	jsonBytes, _ = json.Marshal(errs)101	assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))102103	errs = errorMsgs{}104	assert.Nil(t, errs.Last())105	assert.Nil(t, errs.JSON())106	assert.Empty(t, errs.String())107}108109type TestErr string110111func (e TestErr) Error() string { return string(e) }112113// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()".114// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13.115func TestErrorUnwrap(t *testing.T) {116	innerErr := TestErr("some error")117118	// 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr119	err := fmt.Errorf("wrapped: %w", &Error{120		Err:  innerErr,121		Type: ErrorTypeAny,122	})123124	// check that 'errors.Is()' and 'errors.As()' behave as expected :125	assert.True(t, errors.Is(err, innerErr))126	var testErr TestErr127	assert.True(t, errors.As(err, &testErr))128}