File: errors_test.go

package info (click to toggle)
golang-github-joyent-gocommon 0.0~git20161202.b787089-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 188 kB
  • sloc: makefile: 2
file content (224 lines) | stat: -rw-r--r-- 8,654 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// gocommon - Go library to interact with the JoyentCloud
//
//
// Copyright (c) 2013 Joyent Inc.
//
// Written by Daniele Stroppa <daniele.stroppa@joyent.com>
//

package errors_test

import (
	"github.com/joyent/gocommon/errors"
	gc "launchpad.net/gocheck"
	"testing"
)

func Test(t *testing.T) { gc.TestingT(t) }

type ErrorsSuite struct {
}

var _ = gc.Suite(&ErrorsSuite{})

func (s *ErrorsSuite) TestCreateSimpleBadRequestError(c *gc.C) {
	context := "context"
	err := errors.NewBadRequestf(nil, context, "")
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Bad Request: context")
}

func (s *ErrorsSuite) TestCreateBadRequestError(c *gc.C) {
	context := "context"
	err := errors.NewBadRequestf(nil, context, "It was bad request: %s", context)
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was bad request: context")
}

func (s *ErrorsSuite) TestCreateSimpleInternalErrorError(c *gc.C) {
	context := "context"
	err := errors.NewInternalErrorf(nil, context, "")
	c.Assert(errors.IsInternalError(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Internal Error: context")
}

func (s *ErrorsSuite) TestCreateInternalErrorError(c *gc.C) {
	context := "context"
	err := errors.NewInternalErrorf(nil, context, "It was internal error: %s", context)
	c.Assert(errors.IsInternalError(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was internal error: context")
}

func (s *ErrorsSuite) TestCreateSimpleInvalidArgumentError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidArgumentf(nil, context, "")
	c.Assert(errors.IsInvalidArgument(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Invalid Argument: context")
}

func (s *ErrorsSuite) TestCreateInvalidArgumentError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidArgumentf(nil, context, "It was invalid argument: %s", context)
	c.Assert(errors.IsInvalidArgument(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was invalid argument: context")
}

func (s *ErrorsSuite) TestCreateSimpleInvalidCredentialsError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidCredentialsf(nil, context, "")
	c.Assert(errors.IsInvalidCredentials(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Invalid Credentials: context")
}

func (s *ErrorsSuite) TestCreateInvalidCredentialsError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidCredentialsf(nil, context, "It was invalid credentials: %s", context)
	c.Assert(errors.IsInvalidCredentials(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was invalid credentials: context")
}

func (s *ErrorsSuite) TestCreateSimpleInvalidHeaderError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidHeaderf(nil, context, "")
	c.Assert(errors.IsInvalidHeader(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Invalid Header: context")
}

func (s *ErrorsSuite) TestCreateInvalidHeaderError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidHeaderf(nil, context, "It was invalid header: %s", context)
	c.Assert(errors.IsInvalidHeader(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was invalid header: context")
}

func (s *ErrorsSuite) TestCreateSimpleInvalidVersionError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidVersionf(nil, context, "")
	c.Assert(errors.IsInvalidVersion(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Invalid Version: context")
}

func (s *ErrorsSuite) TestCreateInvalidVersionError(c *gc.C) {
	context := "context"
	err := errors.NewInvalidVersionf(nil, context, "It was invalid version: %s", context)
	c.Assert(errors.IsInvalidVersion(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was invalid version: context")
}

func (s *ErrorsSuite) TestCreateSimpleMissingParameterError(c *gc.C) {
	context := "context"
	err := errors.NewMissingParameterf(nil, context, "")
	c.Assert(errors.IsMissingParameter(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Missing Parameter: context")
}

func (s *ErrorsSuite) TestCreateMissingParameterError(c *gc.C) {
	context := "context"
	err := errors.NewMissingParameterf(nil, context, "It was missing parameter: %s", context)
	c.Assert(errors.IsMissingParameter(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was missing parameter: context")
}

func (s *ErrorsSuite) TestCreateSimpleNotAuthorizedError(c *gc.C) {
	context := "context"
	err := errors.NewNotAuthorizedf(nil, context, "")
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Not Authorized: context")
}

func (s *ErrorsSuite) TestCreateNotAuthorizedError(c *gc.C) {
	context := "context"
	err := errors.NewNotAuthorizedf(nil, context, "It was not authorized: %s", context)
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was not authorized: context")
}

func (s *ErrorsSuite) TestCreateSimpleRequestThrottledError(c *gc.C) {
	context := "context"
	err := errors.NewRequestThrottledf(nil, context, "")
	c.Assert(errors.IsRequestThrottled(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Request Throttled: context")
}

func (s *ErrorsSuite) TestCreateRequestThrottledError(c *gc.C) {
	context := "context"
	err := errors.NewRequestThrottledf(nil, context, "It was request throttled: %s", context)
	c.Assert(errors.IsRequestThrottled(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was request throttled: context")
}

func (s *ErrorsSuite) TestCreateSimpleRequestTooLargeError(c *gc.C) {
	context := "context"
	err := errors.NewRequestTooLargef(nil, context, "")
	c.Assert(errors.IsRequestTooLarge(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Request Too Large: context")
}

func (s *ErrorsSuite) TestCreateRequestTooLargeError(c *gc.C) {
	context := "context"
	err := errors.NewRequestTooLargef(nil, context, "It was request too large: %s", context)
	c.Assert(errors.IsRequestTooLarge(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was request too large: context")
}

func (s *ErrorsSuite) TestCreateSimpleRequestMovedError(c *gc.C) {
	context := "context"
	err := errors.NewRequestMovedf(nil, context, "")
	c.Assert(errors.IsRequestMoved(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Request Moved: context")
}

func (s *ErrorsSuite) TestCreateRequestMovedError(c *gc.C) {
	context := "context"
	err := errors.NewRequestMovedf(nil, context, "It was request moved: %s", context)
	c.Assert(errors.IsRequestMoved(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was request moved: context")
}

func (s *ErrorsSuite) TestCreateSimpleResourceNotFoundError(c *gc.C) {
	context := "context"
	err := errors.NewResourceNotFoundf(nil, context, "")
	c.Assert(errors.IsResourceNotFound(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Resource Not Found: context")
}

func (s *ErrorsSuite) TestCreateResourceNotFoundError(c *gc.C) {
	context := "context"
	err := errors.NewResourceNotFoundf(nil, context, "It was resource not found: %s", context)
	c.Assert(errors.IsResourceNotFound(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was resource not found: context")
}

func (s *ErrorsSuite) TestCreateSimpleUnknownErrorError(c *gc.C) {
	context := "context"
	err := errors.NewUnknownErrorf(nil, context, "")
	c.Assert(errors.IsUnknownError(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "Unknown Error: context")
}

func (s *ErrorsSuite) TestCreateUnknownErrorError(c *gc.C) {
	context := "context"
	err := errors.NewUnknownErrorf(nil, context, "It was unknown error: %s", context)
	c.Assert(errors.IsUnknownError(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was unknown error: context")
}

func (s *ErrorsSuite) TestErrorCause(c *gc.C) {
	rootCause := errors.NewResourceNotFoundf(nil, "some value", "")
	// Construct a new error, based on a resource not found root cause.
	err := errors.Newf(rootCause, "an error occurred")
	c.Assert(err.Cause(), gc.Equals, rootCause)
	// Check the other error attributes.
	c.Assert(err.Error(), gc.Equals, "an error occurred\ncaused by: Resource Not Found: some value")
}

func (s *ErrorsSuite) TestErrorIsType(c *gc.C) {
	rootCause := errors.NewBadRequestf(nil, "some value", "")
	// Construct a new error, based on a bad request root cause.
	err := errors.Newf(rootCause, "an error occurred")
	// Check that the error is not falsely identified as something it is not.
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, false)
	// Check that the error is correctly identified as a not found error.
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
}