|
1 | 1 | package errors_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "net/http" |
| 6 | + "os" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/pkg/errors" |
| 10 | + "github.com/rs/zerolog" |
8 | 11 |
|
9 | 12 | cerr "github.com/aserto-dev/errors" |
10 | 13 | "github.com/stretchr/testify/require" |
@@ -98,7 +101,6 @@ func TestFromGRPCStatus(t *testing.T) { |
98 | 101 | Metadata: initialErr.Data(), |
99 | 102 | Domain: initialErr.Code, |
100 | 103 | }) |
101 | | - |
102 | 104 | if err != nil { |
103 | 105 | assert.Fail(err.Error()) |
104 | 106 | } |
@@ -126,7 +128,6 @@ func TestEquals(t *testing.T) { |
126 | 128 | err2 := ErrAlreadyExists.Msgf("error 2").Str("key2", "val2").Err(errors.New("zoom")) |
127 | 129 |
|
128 | 130 | assert.True(cerr.Equals(err1, err2)) |
129 | | - |
130 | 131 | } |
131 | 132 |
|
132 | 133 | func TestEqualsNil(t *testing.T) { |
@@ -198,3 +199,130 @@ func TestWithHttpError(t *testing.T) { |
198 | 199 | unAerr := cerr.UnwrapAsertoError(aerr) |
199 | 200 | assert.Equal(http.StatusNotAcceptable, unAerr.HTTPCode) |
200 | 201 | } |
| 202 | + |
| 203 | +// returns nil logger if error is nil. |
| 204 | +func TestLoggerWithNilError(t *testing.T) { |
| 205 | + assert := require.New(t) |
| 206 | + |
| 207 | + var err error |
| 208 | + logger := cerr.Logger(err) |
| 209 | + assert.Nil(logger) |
| 210 | +} |
| 211 | + |
| 212 | +func TestLoggerWithWrappedNilError(t *testing.T) { |
| 213 | + assert := require.New(t) |
| 214 | + |
| 215 | + var err error |
| 216 | + ctx := context.Background() |
| 217 | + |
| 218 | + logger := cerr.Logger(cerr.WithContext(err, ctx)) |
| 219 | + assert.Nil(logger) |
| 220 | +} |
| 221 | + |
| 222 | +func TestLoggerWithWrappedErrorsWithEmptyContext(t *testing.T) { |
| 223 | + assert := require.New(t) |
| 224 | + |
| 225 | + ctx := context.Background() |
| 226 | + err := cerr.WithContext(cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error"), ctx) |
| 227 | + wrappedErr := errors.Wrap(err, "wrapped error") |
| 228 | + |
| 229 | + logger := cerr.Logger(wrappedErr) |
| 230 | + assert.Nil(logger) |
| 231 | +} |
| 232 | + |
| 233 | +func TestLoggerWithWrappedErrorsWithLoggerContext(t *testing.T) { |
| 234 | + assert := require.New(t) |
| 235 | + initialLogger := zerolog.New(os.Stderr) |
| 236 | + |
| 237 | + ctx := context.Background() |
| 238 | + ctx = initialLogger.WithContext(ctx) |
| 239 | + err := cerr.WithContext(cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error"), ctx) |
| 240 | + wrappedErr := errors.Wrap(err, "wrapped error") |
| 241 | + |
| 242 | + logger := cerr.Logger(wrappedErr) |
| 243 | + assert.NotNil(logger) |
| 244 | + assert.Equal(logger, zerolog.Ctx(ctx)) |
| 245 | +} |
| 246 | + |
| 247 | +func TestLoggerWithWrappedMultipleWithoutErrorsWithContext(t *testing.T) { |
| 248 | + assert := require.New(t) |
| 249 | + initialLogger := zerolog.New(os.Stderr) |
| 250 | + |
| 251 | + ctx := context.Background() |
| 252 | + ctx = initialLogger.WithContext(ctx) |
| 253 | + err := cerr.WithContext(cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error"), ctx) |
| 254 | + errWithoutCtx := cerr.NewAsertoError("E00002", codes.Internal, http.StatusInternalServerError, "internal error") |
| 255 | + wrappedErr := errWithoutCtx.Err(errors.Wrap(err, "wrapped error")) |
| 256 | + |
| 257 | + logger := cerr.Logger(wrappedErr) |
| 258 | + assert.NotNil(logger) |
| 259 | + assert.Equal(logger, zerolog.Ctx(ctx)) |
| 260 | +} |
| 261 | + |
| 262 | +func TestLoggerWithWrappedMultipleErrorsWithContext(t *testing.T) { |
| 263 | + assert := require.New(t) |
| 264 | + initialLogger := zerolog.New(os.Stderr) |
| 265 | + |
| 266 | + ctx := context.Background() |
| 267 | + ctx = initialLogger.WithContext(ctx) |
| 268 | + err := cerr.WithContext(cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error"), ctx) |
| 269 | + errWithoutCtx := cerr.NewAsertoError("E00002", codes.Internal, http.StatusInternalServerError, "internal error") |
| 270 | + wrappedErr := errors.Wrap(errWithoutCtx.Err(err), "wrapped error") |
| 271 | + |
| 272 | + logger := cerr.Logger(wrappedErr) |
| 273 | + assert.NotNil(logger) |
| 274 | + assert.Equal(logger, zerolog.Ctx(ctx)) |
| 275 | +} |
| 276 | + |
| 277 | +func TestLoggerWithWrappedMultipleErrorsWithMultipleContexts(t *testing.T) { |
| 278 | + assert := require.New(t) |
| 279 | + initialLogger := zerolog.New(os.Stderr) |
| 280 | + ctx1 := context.Background() |
| 281 | + ctx2 := initialLogger.WithContext(ctx1) |
| 282 | + err := cerr.WithContext(cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error"), ctx1) |
| 283 | + wrappedErr := cerr.WithContext(cerr.WithContext(err, ctx2), ctx1) |
| 284 | + |
| 285 | + logger := cerr.Logger(wrappedErr) |
| 286 | + ctx1Logger := zerolog.Ctx(ctx1) |
| 287 | + ctx2Logger := zerolog.Ctx(ctx2) |
| 288 | + |
| 289 | + assert.NotNil(logger) |
| 290 | + assert.NotEqual(logger, ctx1Logger) |
| 291 | + assert.Equal(logger, ctx2Logger) |
| 292 | +} |
| 293 | + |
| 294 | +func TestLoggerWithWrappedMultipleErrorsWithMultipleContextsOuter(t *testing.T) { |
| 295 | + assert := require.New(t) |
| 296 | + initialLogger := zerolog.New(os.Stderr) |
| 297 | + ctx1 := context.Background() |
| 298 | + ctx2 := initialLogger.WithContext(ctx1) |
| 299 | + err := cerr.WithContext(cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error"), ctx1) |
| 300 | + err2 := cerr.WithContext(cerr.NewAsertoError("E00002", codes.Internal, http.StatusInternalServerError, "internal error"), ctx2) |
| 301 | + wrappedErr := errors.Wrap(errors.Wrap(err2, err.Error()), "wrapped error") |
| 302 | + |
| 303 | + logger := cerr.Logger(wrappedErr) |
| 304 | + ctx1Logger := zerolog.Ctx(ctx1) |
| 305 | + ctx2Logger := zerolog.Ctx(ctx2) |
| 306 | + |
| 307 | + assert.NotNil(logger) |
| 308 | + assert.NotEqual(logger, ctx1Logger) |
| 309 | + assert.Equal(logger, ctx2Logger) |
| 310 | +} |
| 311 | + |
| 312 | +func TestLoggerWithWrappedMultipleAsertoErrorsWithMultipleContextsOuter(t *testing.T) { |
| 313 | + assert := require.New(t) |
| 314 | + initialLogger := zerolog.New(os.Stderr) |
| 315 | + ctx1 := context.Background() |
| 316 | + ctx2 := initialLogger.WithContext(ctx1) |
| 317 | + err := cerr.NewAsertoError("E00001", codes.Internal, http.StatusInternalServerError, "internal error").Ctx(ctx1) |
| 318 | + err2 := cerr.NewAsertoError("E00002", codes.Internal, http.StatusInternalServerError, "internal error").Ctx(ctx2) |
| 319 | + wrappedErr := errors.Wrap(errors.Wrap(err2, err.Error()), "wrapped error") |
| 320 | + |
| 321 | + logger := cerr.Logger(wrappedErr) |
| 322 | + ctx1Logger := zerolog.Ctx(ctx1) |
| 323 | + ctx2Logger := zerolog.Ctx(ctx2) |
| 324 | + |
| 325 | + assert.NotNil(logger) |
| 326 | + assert.NotEqual(logger, ctx1Logger) |
| 327 | + assert.Equal(logger, ctx2Logger) |
| 328 | +} |
0 commit comments