-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
error_test.go
69 lines (59 loc) · 1.23 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// error_test.go
//
// Copyright (c) 2018-2021 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
package pixeldrain
import (
"context"
"errors"
"testing"
"github.com/go-openapi/swag"
"github.com/jkawamoto/go-pixeldrain/models"
)
type apiError struct {
err *models.StandardError
}
func newAPIError(e *models.StandardError) error {
return &apiError{err: e}
}
func (e *apiError) Error() string {
return "unexpected call"
}
func (e *apiError) GetPayload() *models.StandardError {
return e.err
}
func TestNewError(t *testing.T) {
sampleMsg := "this is a sample error message"
cases := []struct {
name string
err error
expect string
}{
{
name: "API error",
err: newAPIError(&models.StandardError{
Message: swag.String(sampleMsg),
}),
expect: sampleMsg,
},
{
name: "non API error",
err: context.Canceled,
expect: context.Canceled.Error(),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
err := NewError(c.err)
if msg := err.Error(); msg != c.expect {
t.Errorf("expect %v, got %v", c.expect, msg)
}
if !errors.Is(err, c.err) {
t.Errorf("expect %v is a %v", c.err, err)
}
})
}
}