2015-05-22 14:39:15 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-04-11 16:29:34 +00:00
|
|
|
"fmt"
|
2015-05-22 14:39:15 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-08-30 11:04:03 +00:00
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
2015-05-22 14:39:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
baseError := errors.New("test error")
|
|
|
|
err := &Error{
|
|
|
|
Err: baseError,
|
|
|
|
Type: ErrorTypePrivate,
|
|
|
|
}
|
|
|
|
assert.Equal(t, err.Error(), baseError.Error())
|
2018-07-03 07:39:18 +00:00
|
|
|
assert.Equal(t, H{"error": baseError.Error()}, err.JSON())
|
2015-05-22 14:39:15 +00:00
|
|
|
|
|
|
|
assert.Equal(t, err.SetType(ErrorTypePublic), err)
|
2018-07-03 07:39:18 +00:00
|
|
|
assert.Equal(t, ErrorTypePublic, err.Type)
|
2015-05-22 14:39:15 +00:00
|
|
|
|
|
|
|
assert.Equal(t, err.SetMeta("some data"), err)
|
2018-07-03 07:39:18 +00:00
|
|
|
assert.Equal(t, "some data", err.Meta)
|
|
|
|
assert.Equal(t, H{
|
2015-05-22 14:39:15 +00:00
|
|
|
"error": baseError.Error(),
|
|
|
|
"meta": "some data",
|
2018-07-03 07:39:18 +00:00
|
|
|
}, err.JSON())
|
2015-05-22 14:39:15 +00:00
|
|
|
|
2015-05-22 16:34:42 +00:00
|
|
|
jsonBytes, _ := json.Marshal(err)
|
2017-07-08 10:19:09 +00:00
|
|
|
assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))
|
2015-05-22 16:34:42 +00:00
|
|
|
|
2019-01-18 01:32:53 +00:00
|
|
|
err.SetMeta(H{ // nolint: errcheck
|
2015-05-22 14:39:15 +00:00
|
|
|
"status": "200",
|
|
|
|
"data": "some data",
|
|
|
|
})
|
2018-07-03 07:39:18 +00:00
|
|
|
assert.Equal(t, H{
|
2015-05-22 14:39:15 +00:00
|
|
|
"error": baseError.Error(),
|
|
|
|
"status": "200",
|
|
|
|
"data": "some data",
|
2018-07-03 07:39:18 +00:00
|
|
|
}, err.JSON())
|
2015-05-22 14:39:15 +00:00
|
|
|
|
2019-01-18 01:32:53 +00:00
|
|
|
err.SetMeta(H{ // nolint: errcheck
|
2015-05-22 14:39:15 +00:00
|
|
|
"error": "custom error",
|
|
|
|
"status": "200",
|
|
|
|
"data": "some data",
|
|
|
|
})
|
2018-07-03 07:39:18 +00:00
|
|
|
assert.Equal(t, H{
|
2015-05-22 14:39:15 +00:00
|
|
|
"error": "custom error",
|
|
|
|
"status": "200",
|
|
|
|
"data": "some data",
|
2018-07-03 07:39:18 +00:00
|
|
|
}, err.JSON())
|
2017-06-13 02:37:20 +00:00
|
|
|
|
|
|
|
type customError struct {
|
|
|
|
status string
|
|
|
|
data string
|
|
|
|
}
|
2019-01-18 01:32:53 +00:00
|
|
|
err.SetMeta(customError{status: "200", data: "other data"}) // nolint: errcheck
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())
|
2015-05-22 14:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorSlice(t *testing.T) {
|
|
|
|
errs := errorMsgs{
|
|
|
|
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
|
|
|
{Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},
|
|
|
|
{Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},
|
|
|
|
}
|
|
|
|
|
2015-07-03 02:20:18 +00:00
|
|
|
assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "third", errs.Last().Error())
|
|
|
|
assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())
|
|
|
|
assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())
|
|
|
|
assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())
|
|
|
|
assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())
|
2015-05-22 14:39:15 +00:00
|
|
|
assert.Empty(t, errs.ByType(ErrorTypeBind))
|
2015-06-06 15:24:16 +00:00
|
|
|
assert.Empty(t, errs.ByType(ErrorTypeBind).String())
|
2015-05-22 14:39:15 +00:00
|
|
|
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, `Error #01: first
|
2015-05-22 14:39:15 +00:00
|
|
|
Error #02: second
|
|
|
|
Meta: some data
|
|
|
|
Error #03: third
|
|
|
|
Meta: map[status:400]
|
2017-08-04 05:45:59 +00:00
|
|
|
`, errs.String())
|
|
|
|
assert.Equal(t, []interface{}{
|
2015-05-22 14:39:15 +00:00
|
|
|
H{"error": "first"},
|
|
|
|
H{"error": "second", "meta": "some data"},
|
|
|
|
H{"error": "third", "status": "400"},
|
2017-08-04 05:45:59 +00:00
|
|
|
}, errs.JSON())
|
2015-05-22 16:34:42 +00:00
|
|
|
jsonBytes, _ := json.Marshal(errs)
|
2017-07-08 10:19:09 +00:00
|
|
|
assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
|
2015-05-22 14:39:15 +00:00
|
|
|
errs = errorMsgs{
|
|
|
|
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
|
|
|
}
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, H{"error": "first"}, errs.JSON())
|
2015-05-22 16:34:42 +00:00
|
|
|
jsonBytes, _ = json.Marshal(errs)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))
|
2015-05-22 14:39:15 +00:00
|
|
|
|
|
|
|
errs = errorMsgs{}
|
|
|
|
assert.Nil(t, errs.Last())
|
|
|
|
assert.Nil(t, errs.JSON())
|
|
|
|
assert.Empty(t, errs.String())
|
|
|
|
}
|
2021-04-11 16:29:34 +00:00
|
|
|
|
|
|
|
type TestErr string
|
|
|
|
|
|
|
|
func (e TestErr) Error() string { return string(e) }
|
|
|
|
|
|
|
|
// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()".
|
|
|
|
// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13.
|
|
|
|
func TestErrorUnwrap(t *testing.T) {
|
2021-09-30 02:04:28 +00:00
|
|
|
innerErr := TestErr("some error")
|
2021-04-11 16:29:34 +00:00
|
|
|
|
|
|
|
// 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr
|
|
|
|
err := fmt.Errorf("wrapped: %w", &Error{
|
|
|
|
Err: innerErr,
|
|
|
|
Type: ErrorTypeAny,
|
|
|
|
})
|
|
|
|
|
|
|
|
// check that 'errors.Is()' and 'errors.As()' behave as expected :
|
|
|
|
assert.True(t, errors.Is(err, innerErr))
|
|
|
|
var testErr TestErr
|
|
|
|
assert.True(t, errors.As(err, &testErr))
|
|
|
|
}
|