Update primary error functions to take a context, deprecate old functions (#1358)
* add new error funcs which take a ctx and deprecate existing funcs * use context where available when writing error events * remove circular internal/errors pkg dep * suppress some chatty errors using errors.WithoutEvent() * convert auth oidc funcs to take context where needed to write events Co-authored-by: Jim <jlambert@hashicorp.com>
This commit is contained in:
1 parent
02ff2db66e
commit
6b78108ecf
227 files changed
+3945
-3736
No files matched your search
+80
-14
@@ -1,10 +1,13 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
@@ -35,6 +38,9 @@ type Err struct {
|
||||
|
||||
// E creates a new Err with provided code and supports the options of:
|
||||
//
|
||||
// * WithoutEvent - allows you to specify that an error event should not be
|
||||
// emitted.
|
||||
//
|
||||
// * WithOp() - allows you to specify an optional Op (operation).
|
||||
//
|
||||
// * WithMsg() - allows you to specify an optional error msg, if the default
|
||||
@@ -46,7 +52,8 @@ type Err struct {
|
||||
//
|
||||
// * WithCode() - allows you to specify an optional Code, this code will be prioritized
|
||||
// over a code used from WithWrap().
|
||||
func E(opt ...Option) error {
|
||||
func E(ctx context.Context, opt ...Option) error {
|
||||
// nil ctx is allowed and tested for in unit tests
|
||||
opts := GetOpts(opt...)
|
||||
var code Code
|
||||
|
||||
@@ -62,19 +69,55 @@ func E(opt ...Option) error {
|
||||
code = opts.withCode
|
||||
}
|
||||
|
||||
return &Err{
|
||||
err = &Err{
|
||||
Code: code,
|
||||
Op: opts.withOp,
|
||||
Wrapped: opts.withErrWrapped,
|
||||
Msg: opts.withErrMsg,
|
||||
}
|
||||
if opts.withoutEvent {
|
||||
return err
|
||||
}
|
||||
|
||||
{
|
||||
// events require an Op, but we don't want to change the error specified
|
||||
// by the caller. So we'll build a new event and conditionally set a
|
||||
// reasonable Op based on the call stack.
|
||||
eventErr := &Err{
|
||||
Code: err.Code,
|
||||
Op: err.Op,
|
||||
Wrapped: err.Wrapped,
|
||||
Msg: err.Msg,
|
||||
}
|
||||
if eventErr.Op == "" {
|
||||
const has = "github.com/hashicorp/boundary/internal/errors."
|
||||
const trim = "github.com/hashicorp/boundary/"
|
||||
for i := 0; i < 5; i++ {
|
||||
pc, _, _, ok := runtime.Caller(i)
|
||||
details := runtime.FuncForPC(pc)
|
||||
if ok && details != nil {
|
||||
if strings.HasPrefix(details.Name(), has) {
|
||||
continue
|
||||
}
|
||||
eventErr.Op = Op(strings.TrimPrefix(details.Name(), trim))
|
||||
break
|
||||
}
|
||||
}
|
||||
if eventErr.Op == "" {
|
||||
eventErr.Op = "unknown operation"
|
||||
}
|
||||
}
|
||||
event.WriteError(ctx, event.Op(err.Op), eventErr)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// New creates a new Err with provided code, op and msg
|
||||
// It supports the options of:
|
||||
//
|
||||
// * WithWrap() - allows you to specify an error to wrap
|
||||
func New(c Code, op Op, msg string, opt ...Option) error {
|
||||
func New(ctx context.Context, c Code, op Op, msg string, opt ...Option) error {
|
||||
if c != Unknown {
|
||||
opt = append(opt, WithCode(c))
|
||||
}
|
||||
@@ -84,8 +127,7 @@ func New(c Code, op Op, msg string, opt ...Option) error {
|
||||
if msg != "" {
|
||||
opt = append(opt, WithMsg(msg))
|
||||
}
|
||||
|
||||
return E(opt...)
|
||||
return E(ctx, opt...)
|
||||
}
|
||||
|
||||
// Wrap creates a new Err from the provided err and op,
|
||||
@@ -94,7 +136,7 @@ func New(c Code, op Op, msg string, opt ...Option) error {
|
||||
//
|
||||
// * WithMsg() - allows you to specify an optional error msg, if the default
|
||||
// msg for the error Code is not sufficient.
|
||||
func Wrap(e error, op Op, opt ...Option) error {
|
||||
func Wrap(ctx context.Context, e error, op Op, opt ...Option) error {
|
||||
if op != "" {
|
||||
opt = append(opt, WithOp(op))
|
||||
}
|
||||
@@ -109,7 +151,31 @@ func Wrap(e error, op Op, opt ...Option) error {
|
||||
opt = append(opt, WithWrap(e))
|
||||
}
|
||||
|
||||
return E(opt...)
|
||||
return E(ctx, opt...)
|
||||
}
|
||||
|
||||
// EDeprecated is the legacy version of E which does not
|
||||
// create an event. Please refrain from using this.
|
||||
// When all calls are moved from EDeprecated to
|
||||
// E, please update ICU-1883
|
||||
func EDeprecated(opt ...Option) error {
|
||||
return E(context.TODO(), opt...)
|
||||
}
|
||||
|
||||
// NewDeprecated is the legacy version of New which does not
|
||||
// create an event. Please refrain from using this.
|
||||
// When all calls are moved from NewDeprecated to
|
||||
// New, please update ICU-1883
|
||||
func NewDeprecated(c Code, op Op, msg string, opt ...Option) error {
|
||||
return New(context.TODO(), c, op, msg, opt...)
|
||||
}
|
||||
|
||||
// WrapDeprecated is the legacy version of New which does not
|
||||
// create an event. Please refrain from using this.
|
||||
// When all calls are moved from WrapDeprecated to
|
||||
// New, please update ICU-1884
|
||||
func WrapDeprecated(e error, op Op, opt ...Option) error {
|
||||
return Wrap(context.TODO(), e, op, opt...)
|
||||
}
|
||||
|
||||
// Convert will convert the error to a Boundary *Err (returning it as an error)
|
||||
@@ -130,24 +196,24 @@ func Convert(e error) *Err {
|
||||
if pqError.Code.Class() == "23" { // class of integrity constraint violations
|
||||
switch pqError.Code {
|
||||
case "23505": // unique_violation
|
||||
return E(WithMsg(pqError.Message), WithWrap(E(WithCode(NotUnique), WithMsg("unique constraint violation")))).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithMsg(pqError.Message), WithWrap(EDeprecated(WithCode(NotUnique), WithMsg("unique constraint violation")))).(*Err)
|
||||
case "23502": // not_null_violation
|
||||
msg := fmt.Sprintf("%s must not be empty", pqError.Column)
|
||||
return E(WithMsg(msg), WithWrap(E(WithCode(NotNull), WithMsg("not null constraint violated")))).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithMsg(msg), WithWrap(EDeprecated(WithCode(NotNull), WithMsg("not null constraint violated")))).(*Err)
|
||||
case "23514": // check_violation
|
||||
msg := fmt.Sprintf("%s constraint failed", pqError.Constraint)
|
||||
return E(WithMsg(msg), WithWrap(E(WithCode(CheckConstraint), WithMsg("check constraint violated")))).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithMsg(msg), WithWrap(EDeprecated(WithCode(CheckConstraint), WithMsg("check constraint violated")))).(*Err)
|
||||
default:
|
||||
return E(WithCode(NotSpecificIntegrity), WithMsg(pqError.Message)).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithCode(NotSpecificIntegrity), WithMsg(pqError.Message)).(*Err)
|
||||
}
|
||||
}
|
||||
switch pqError.Code {
|
||||
case "42P01":
|
||||
return E(WithCode(MissingTable), WithMsg(pqError.Message)).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithCode(MissingTable), WithMsg(pqError.Message)).(*Err)
|
||||
case "42703":
|
||||
return E(WithCode(ColumnNotFound), WithMsg(pqError.Message)).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithCode(ColumnNotFound), WithMsg(pqError.Message)).(*Err)
|
||||
case "P0001":
|
||||
return E(WithCode(Exception), WithMsg(pqError.Message)).(*Err)
|
||||
return E(context.TODO(), WithoutEvent(), WithCode(Exception), WithMsg(pqError.Message)).(*Err)
|
||||
}
|
||||
}
|
||||
// unfortunately, we can't help.
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
func Test_ErrorE(t *testing.T) {
|
||||
t.Parallel()
|
||||
errRecordNotFound := errors.E(errors.WithCode(errors.RecordNotFound))
|
||||
errRecordNotFound := errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.RecordNotFound))
|
||||
tests := []struct {
|
||||
name string
|
||||
opt []errors.Option
|
||||
@@ -77,11 +77,24 @@ func Test_ErrorE(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
err := errors.E(tt.opt...)
|
||||
err := errors.EDeprecated(tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
|
||||
err = errors.E(context.TODO(), tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
})
|
||||
}
|
||||
t.Run("nil-context", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
//nolint SA1012 intentionally passing a nil context.
|
||||
err := errors.E(nil, errors.WithCode(errors.InvalidParameter))
|
||||
require.Error(err)
|
||||
assert.Equal(&errors.Err{
|
||||
Code: errors.InvalidParameter,
|
||||
}, err)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewError(t *testing.T) {
|
||||
@@ -100,15 +113,25 @@ func Test_NewError(t *testing.T) {
|
||||
op: "alice.Bob",
|
||||
msg: "test msg",
|
||||
opt: []errors.Option{
|
||||
errors.WithWrap(errors.E(errors.WithCode(errors.RecordNotFound))),
|
||||
errors.WithWrap(errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.RecordNotFound))),
|
||||
},
|
||||
want: &errors.Err{
|
||||
Op: "alice.Bob",
|
||||
Wrapped: errors.E(errors.WithCode(errors.RecordNotFound)),
|
||||
Wrapped: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.RecordNotFound)),
|
||||
Msg: "test msg",
|
||||
Code: errors.InvalidParameter,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty-op",
|
||||
code: errors.InvalidParameter,
|
||||
op: "",
|
||||
msg: "test msg",
|
||||
want: &errors.Err{
|
||||
Msg: "test msg",
|
||||
Code: errors.InvalidParameter,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no-options",
|
||||
opt: nil,
|
||||
@@ -142,7 +165,11 @@ func Test_NewError(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
err := errors.New(tt.code, tt.op, tt.msg, tt.opt...)
|
||||
err := errors.NewDeprecated(tt.code, tt.op, tt.msg, tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
|
||||
err = errors.New(context.TODO(), tt.code, tt.op, tt.msg, tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
})
|
||||
@@ -151,7 +178,7 @@ func Test_NewError(t *testing.T) {
|
||||
|
||||
func Test_WrapError(t *testing.T) {
|
||||
t.Parallel()
|
||||
testErr := errors.E(errors.WithCode(errors.InvalidParameter), errors.WithOp("alice.Bob"), errors.WithMsg("test msg"))
|
||||
testErr := errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.InvalidParameter), errors.WithOp("alice.Bob"), errors.WithMsg("test msg"))
|
||||
tests := []struct {
|
||||
name string
|
||||
opt []errors.Option
|
||||
@@ -232,7 +259,7 @@ func Test_WrapError(t *testing.T) {
|
||||
Message: "test msg",
|
||||
},
|
||||
want: &errors.Err{
|
||||
Wrapped: errors.E(errors.WithCode(errors.NotSpecificIntegrity), errors.WithMsg("test msg")),
|
||||
Wrapped: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.NotSpecificIntegrity), errors.WithMsg("test msg")),
|
||||
Code: errors.NotSpecificIntegrity,
|
||||
},
|
||||
},
|
||||
@@ -240,7 +267,11 @@ func Test_WrapError(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
err := errors.Wrap(tt.err, tt.op, tt.opt...)
|
||||
err := errors.WrapDeprecated(tt.err, tt.op, tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
|
||||
err = errors.Wrap(context.TODO(), tt.err, tt.op, tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
})
|
||||
@@ -261,12 +292,12 @@ func TestError_Info(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Unknown",
|
||||
err: errors.E().(*errors.Err),
|
||||
err: errors.E(context.TODO()).(*errors.Err),
|
||||
want: errors.Unknown,
|
||||
},
|
||||
{
|
||||
name: "InvalidParameter",
|
||||
err: errors.E(errors.WithCode(errors.InvalidParameter)).(*errors.Err),
|
||||
err: errors.E(context.TODO(), errors.WithCode(errors.InvalidParameter)).(*errors.Err),
|
||||
want: errors.InvalidParameter,
|
||||
},
|
||||
}
|
||||
@@ -287,37 +318,37 @@ func TestError_Error(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "msg",
|
||||
err: errors.E(errors.WithMsg("test msg")),
|
||||
err: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithMsg("test msg")),
|
||||
want: "test msg: unknown: error #0",
|
||||
},
|
||||
{
|
||||
name: "code",
|
||||
err: errors.E(errors.WithCode(errors.CheckConstraint)),
|
||||
err: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.CheckConstraint)),
|
||||
want: "constraint check failed, integrity violation: error #1000",
|
||||
},
|
||||
{
|
||||
name: "op-msg-and-code",
|
||||
err: errors.E(errors.WithCode(errors.CheckConstraint), errors.WithOp("alice.bob"), errors.WithMsg("test msg")),
|
||||
err: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.CheckConstraint), errors.WithOp("alice.bob"), errors.WithMsg("test msg")),
|
||||
want: "alice.bob: test msg: integrity violation: error #1000",
|
||||
},
|
||||
{
|
||||
name: "unknown",
|
||||
err: errors.E(),
|
||||
err: errors.EDeprecated(),
|
||||
want: "unknown, unknown: error #0",
|
||||
},
|
||||
{
|
||||
name: "wrapped-no-code",
|
||||
err: errors.E(errors.WithWrap(errors.E(errors.WithCode(errors.InvalidParameter), errors.WithMsg("wrapped msg"))), errors.WithMsg("test msg")),
|
||||
err: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithWrap(errors.EDeprecated(errors.WithCode(errors.InvalidParameter), errors.WithMsg("wrapped msg"))), errors.WithMsg("test msg")),
|
||||
want: "test msg: wrapped msg: parameter violation: error #100",
|
||||
},
|
||||
{
|
||||
name: "wrapped-different-error-codes",
|
||||
err: errors.E(errors.WithCode(errors.CheckConstraint), errors.WithWrap(errors.E(errors.WithCode(errors.InvalidParameter), errors.WithMsg("wrapped msg"))), errors.WithMsg("test msg")),
|
||||
err: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.CheckConstraint), errors.WithWrap(errors.EDeprecated(errors.WithCode(errors.InvalidParameter), errors.WithMsg("wrapped msg"))), errors.WithMsg("test msg")),
|
||||
want: "test msg: integrity violation: error #1000: wrapped msg: parameter violation: error #100",
|
||||
},
|
||||
{
|
||||
name: "wrapped-same-error-codes",
|
||||
err: errors.E(errors.WithCode(errors.CheckConstraint), errors.WithWrap(errors.E(errors.WithCode(errors.CheckConstraint), errors.WithMsg("wrapped msg"))), errors.WithMsg("test msg")),
|
||||
err: errors.E(context.TODO(), errors.WithoutEvent(), errors.WithCode(errors.CheckConstraint), errors.WithWrap(errors.EDeprecated(errors.WithCode(errors.CheckConstraint), errors.WithMsg("wrapped msg"))), errors.WithMsg("test msg")),
|
||||
want: "test msg: wrapped msg: integrity violation: error #1000",
|
||||
},
|
||||
}
|
||||
@@ -325,7 +356,7 @@ func TestError_Error(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got := tt.err.Error()
|
||||
assert.Equal(tt.want, got)
|
||||
assert.Contains(got, tt.want)
|
||||
})
|
||||
}
|
||||
t.Run("nil *Err", func(t *testing.T) {
|
||||
@@ -338,8 +369,8 @@ func TestError_Error(t *testing.T) {
|
||||
|
||||
func TestError_Unwrap(t *testing.T) {
|
||||
t.Parallel()
|
||||
testErr := errors.E(errors.WithMsg("test error"))
|
||||
errInvalidParameter := errors.E(errors.WithCode(errors.InvalidParameter), errors.WithMsg("test error"))
|
||||
testErr := errors.EDeprecated(errors.WithMsg("test error"))
|
||||
errInvalidParameter := errors.EDeprecated(errors.WithCode(errors.InvalidParameter), errors.WithMsg("test error"))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -349,7 +380,7 @@ func TestError_Unwrap(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "ErrInvalidParameter",
|
||||
err: errors.E(errors.WithWrap(errInvalidParameter)),
|
||||
err: errors.EDeprecated(errors.WithWrap(errInvalidParameter)),
|
||||
want: errInvalidParameter,
|
||||
wantIsErr: errInvalidParameter,
|
||||
},
|
||||
@@ -380,7 +411,7 @@ func TestError_Unwrap(t *testing.T) {
|
||||
|
||||
func TestConvertError(t *testing.T) {
|
||||
t.Parallel()
|
||||
testErr := errors.E(errors.WithCode(errors.InvalidParameter), errors.WithOp("alice.Bob"), errors.WithMsg("test msg"))
|
||||
testErr := errors.EDeprecated(errors.WithCode(errors.InvalidParameter), errors.WithOp("alice.Bob"), errors.WithMsg("test msg"))
|
||||
const (
|
||||
createTable = `
|
||||
create table if not exists test_table (
|
||||
@@ -421,7 +452,7 @@ func TestConvertError(t *testing.T) {
|
||||
e: &pq.Error{
|
||||
Code: pq.ErrorCode("23001"),
|
||||
},
|
||||
wantErr: errors.E(errors.WithCode(errors.NotSpecificIntegrity)),
|
||||
wantErr: errors.EDeprecated(errors.WithCode(errors.NotSpecificIntegrity)),
|
||||
},
|
||||
{
|
||||
name: "convert-domain-error",
|
||||
|
||||
+14
-13
@@ -40,12 +40,13 @@ func TestError_IsUnique(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "CodeUnique",
|
||||
in: errors.E(errors.WithCode(errors.NotUnique)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.NotUnique)),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped-pq-is-unique",
|
||||
in: errors.E(
|
||||
context.TODO(),
|
||||
errors.WithWrap(&pq.Error{
|
||||
Code: pq.ErrorCode("23505"),
|
||||
}),
|
||||
@@ -54,12 +55,12 @@ func TestError_IsUnique(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "RecordNotFound",
|
||||
in: errors.E(errors.WithCode(errors.RecordNotFound)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.RecordNotFound)),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "conflicting-wrapped-code",
|
||||
in: errors.E(errors.WithCode(errors.NotNull), errors.WithWrap(errors.E(errors.WithCode(errors.NotUnique)))),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.NotNull), errors.WithWrap(errors.E(context.TODO(), errors.WithCode(errors.NotUnique)))),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
@@ -102,12 +103,12 @@ func TestError_IsCheckConstraint(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "ErrCodeCheckConstraint",
|
||||
in: errors.E(errors.WithCode(errors.CheckConstraint)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.CheckConstraint)),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped-pq-is-check-constraint",
|
||||
in: errors.E(errors.WithCode(errors.CheckConstraint),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.CheckConstraint),
|
||||
errors.WithWrap(&pq.Error{
|
||||
Code: pq.ErrorCode("23514"),
|
||||
}),
|
||||
@@ -116,12 +117,12 @@ func TestError_IsCheckConstraint(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "RecordNotFound",
|
||||
in: errors.E(errors.WithCode(errors.RecordNotFound)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.RecordNotFound)),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "conflicting-wrapped-code",
|
||||
in: errors.E(errors.WithCode(errors.NotNull), errors.WithWrap(errors.E(errors.WithCode(errors.CheckConstraint)))),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.NotNull), errors.WithWrap(errors.E(context.TODO(), errors.WithCode(errors.CheckConstraint)))),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
@@ -171,12 +172,12 @@ func TestError_IsNotNullError(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "ErrCodeNotNull",
|
||||
in: errors.E(errors.WithCode(errors.NotNull)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.NotNull)),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped-pq-is-not-null",
|
||||
in: errors.E(errors.WithCode(errors.NotNull),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.NotNull),
|
||||
errors.WithWrap(&pq.Error{
|
||||
Code: pq.ErrorCode("23502"),
|
||||
}),
|
||||
@@ -185,12 +186,12 @@ func TestError_IsNotNullError(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "RecordNotFound",
|
||||
in: errors.E(errors.WithCode(errors.RecordNotFound)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.RecordNotFound)),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "conflicting-wrapped-code",
|
||||
in: errors.E(errors.WithCode(errors.CheckConstraint), errors.WithWrap(errors.E(errors.WithCode(errors.NotNull)))),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.CheckConstraint), errors.WithWrap(errors.E(context.TODO(), errors.WithCode(errors.NotNull)))),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
@@ -270,7 +271,7 @@ func TestError_IsNotFoundError(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "not-found-error",
|
||||
in: errors.E(errors.WithCode(errors.RecordNotFound)),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.RecordNotFound)),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
@@ -280,7 +281,7 @@ func TestError_IsNotFoundError(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "conflicting-wrapped-code",
|
||||
in: errors.E(errors.WithCode(errors.NotNull), errors.WithWrap(errors.E(errors.WithCode(errors.RecordNotFound)))),
|
||||
in: errors.E(context.TODO(), errors.WithCode(errors.NotNull), errors.WithWrap(errors.E(context.TODO(), errors.WithCode(errors.RecordNotFound)))),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
"testing"
|
||||
|
||||
@@ -140,8 +141,8 @@ func TestTemplate_Error(t *testing.T) {
|
||||
func TestMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
stdErr := stderrors.New("test error")
|
||||
errInvalidFieldMask := E(WithCode(InvalidFieldMask), WithMsg("test invalid field mask error"))
|
||||
errNotUnique := E(WithCode(NotUnique), WithMsg("test not unique error"))
|
||||
errInvalidFieldMask := E(context.TODO(), WithCode(InvalidFieldMask), WithMsg("test invalid field mask error"))
|
||||
errNotUnique := E(context.TODO(), WithCode(NotUnique), WithMsg("test not unique error"))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -152,7 +153,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "nil template",
|
||||
template: nil,
|
||||
err: E(WithCode(NotUnique), WithMsg("this thing was must be unique")),
|
||||
err: E(context.TODO(), WithCode(NotUnique), WithMsg("this thing was must be unique")),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
@@ -164,7 +165,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "match on Kind only",
|
||||
template: T(Integrity),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(NotUnique),
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -175,7 +176,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "no match on Kind only",
|
||||
template: T(Integrity),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(RecordNotFound),
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -186,7 +187,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "match on Code only",
|
||||
template: T(NotUnique),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(NotUnique),
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -197,7 +198,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "no match on Code only",
|
||||
template: T(NotUnique),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(RecordNotFound),
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -208,7 +209,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "match on Op only",
|
||||
template: T(Op("alice.Bob")),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(NotUnique),
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -219,7 +220,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "no match on Op only",
|
||||
template: T(Op("alice.Alice")),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(RecordNotFound),
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -236,7 +237,7 @@ func TestMatch(t *testing.T) {
|
||||
errInvalidFieldMask,
|
||||
Op("alice.Bob"),
|
||||
),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(InvalidParameter),
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -247,7 +248,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "match on Wrapped only",
|
||||
template: T(errInvalidFieldMask),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(NotUnique),
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -258,7 +259,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "no match on Wrapped only",
|
||||
template: T(errNotUnique),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(RecordNotFound),
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -269,7 +270,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "match on Wrapped only stderror",
|
||||
template: T(stdErr),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(NotUnique),
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
@@ -280,7 +281,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "no match on Wrapped only stderror",
|
||||
template: T(stderrors.New("no match")),
|
||||
err: E(
|
||||
err: E(context.TODO(),
|
||||
WithCode(RecordNotFound),
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
|
||||
@@ -18,6 +18,7 @@ type Options struct {
|
||||
withErrWrapped error
|
||||
withErrMsg string
|
||||
withOp Op
|
||||
withoutEvent bool
|
||||
}
|
||||
|
||||
func getDefaultOptions() Options {
|
||||
@@ -55,3 +56,9 @@ func WithCode(code Code) Option {
|
||||
o.withCode = code
|
||||
}
|
||||
}
|
||||
|
||||
func WithoutEvent() Option {
|
||||
return func(o *Options) {
|
||||
o.withoutEvent = true
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -30,7 +31,7 @@ func Test_getOpts(t *testing.T) {
|
||||
testOpts.withErrWrapped = nil
|
||||
assert.Equal(opts, testOpts)
|
||||
|
||||
testErr := E(WithCode(InvalidParameter), WithMsg("test error"))
|
||||
testErr := E(context.TODO(), WithCode(InvalidParameter), WithMsg("test error"))
|
||||
|
||||
// try setting it
|
||||
opts = GetOpts(WithWrap(testErr))
|
||||
@@ -63,4 +64,17 @@ func Test_getOpts(t *testing.T) {
|
||||
testOpts.withCode = NotUnique
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
t.Run("WithoutEvent", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
// test default
|
||||
opts := GetOpts()
|
||||
testOpts := getDefaultOptions()
|
||||
testOpts.withoutEvent = false
|
||||
assert.Equal(opts, testOpts)
|
||||
|
||||
// try setting it
|
||||
opts = GetOpts(WithoutEvent())
|
||||
testOpts.withoutEvent = true
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user