Revert errorid (#797)
* Revert "Refactor domain error to use errorId (#790)" This reverts commitd30dd0ff64. * Revert "Create common errorID generation (#788)" This reverts commitb8a5b9adf1.
This commit is contained in:
1 parent
7f1a12ecaa
commit
aa655f6991
10 files changed
+103
-207
No files matched your search
@@ -27,10 +27,6 @@ dev: build-ui-ifne
|
||||
@echo "==> Building Boundary with dev and UI features enabled"
|
||||
@CGO_ENABLED=$(CGO_ENABLED) BUILD_TAGS='$(BUILD_TAGS)' BOUNDARY_DEV_BUILD=1 sh -c "'$(CURDIR)/scripts/build.sh'"
|
||||
|
||||
rand:
|
||||
@echo "==> Generating random boundary error ID"
|
||||
@go run scripts/errorid.go
|
||||
|
||||
bin: BUILD_TAGS+=ui
|
||||
bin: build-ui
|
||||
@echo "==> Building Boundary with UI features enabled"
|
||||
|
||||
+19
-34
@@ -8,12 +8,12 @@ import (
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// ErrorId is an id that is unique to the error (not the instance of the error).
|
||||
// The id should be generated using `make rand`, and used as the `errorid` param when calling `errors.New`
|
||||
type ErrorId string
|
||||
// Op represents an operation (package.function).
|
||||
// For example iam.CreateRole
|
||||
type Op string
|
||||
|
||||
// Err provides the ability to specify a Msg, ErrorId, Code and Wrapped error.
|
||||
// Errs must have a Code and ErrorId all other fields are optional. We've chosen Err
|
||||
// Err provides the ability to specify a Msg, Op, Code and Wrapped error.
|
||||
// Errs must have a Code and all other fields are optional. We've chosen Err
|
||||
// over Error for the identifier to support the easy embedding of Errs. Errs
|
||||
// can be embedded without a conflict between the embedded Err and Err.Error().
|
||||
type Err struct {
|
||||
@@ -21,78 +21,63 @@ type Err struct {
|
||||
// errorCodeInfo, which contains the error's Kind and Message
|
||||
Code Code
|
||||
|
||||
// Unique ID used to identify the error being returned
|
||||
ErrorId ErrorId
|
||||
|
||||
// Msg for the error
|
||||
Msg string
|
||||
|
||||
// Op represents the operation raising/propagating an error and is optional
|
||||
Op Op
|
||||
|
||||
// Wrapped is the error which this Err wraps and will be nil if there's no
|
||||
// error to wrap.
|
||||
Wrapped error
|
||||
}
|
||||
|
||||
// New creates a new Err and supports the options of:
|
||||
// WithOp - allows you to specify an optional Op (operation)
|
||||
// WithMsg() - allows you to specify an optional error msg, if the default
|
||||
// msg for the error Code is not sufficient.
|
||||
// WithWrap() - allows you to specify
|
||||
// an error to wrap
|
||||
func New(c Code, errorid ErrorId, opt ...Option) error {
|
||||
func New(c Code, opt ...Option) error {
|
||||
opts := GetOpts(opt...)
|
||||
|
||||
return &Err{
|
||||
Code: c,
|
||||
ErrorId: errorid,
|
||||
Op: opts.withOp,
|
||||
Wrapped: opts.withErrWrapped,
|
||||
Msg: opts.withErrMsg,
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap creates a new Err, but preserves the Code of the original error being wrapped
|
||||
func Wrap(e error, errorid ErrorId, opt ...Option) error {
|
||||
var code Code
|
||||
if err, ok := e.(*Err); ok {
|
||||
// get code from wrapped error
|
||||
code = err.Code
|
||||
}
|
||||
opt = append(opt, WithWrap(e))
|
||||
return New(code, errorid, opt...)
|
||||
}
|
||||
|
||||
// Convert will convert the error to a Boundary *Err (returning it as an error)
|
||||
// and attempt to add a helpful error msg as well. If that's not possible, it
|
||||
// will return nil
|
||||
func Convert(e error, errorid ErrorId) *Err {
|
||||
func Convert(e error) *Err {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
if err, ok := e.(*Err); ok {
|
||||
return err
|
||||
}
|
||||
|
||||
var pqError *pq.Error
|
||||
if As(e, &pqError) {
|
||||
if pqError.Code.Class() == "23" { // class of integrity constraint violations
|
||||
switch pqError.Code {
|
||||
case "23505": // unique_violation
|
||||
return New(NotUnique, errorid, WithMsg(pqError.Detail), WithWrap(ErrNotUnique)).(*Err)
|
||||
return New(NotUnique, WithMsg(pqError.Detail), WithWrap(ErrNotUnique)).(*Err)
|
||||
case "23502": // not_null_violation
|
||||
msg := fmt.Sprintf("%s must not be empty", pqError.Column)
|
||||
return New(NotNull, errorid, WithMsg(msg), WithWrap(ErrNotNull)).(*Err)
|
||||
return New(NotNull, WithMsg(msg), WithWrap(ErrNotNull)).(*Err)
|
||||
case "23514": // check_violation
|
||||
msg := fmt.Sprintf("%s constraint failed", pqError.Constraint)
|
||||
return New(CheckConstraint, errorid, WithMsg(msg), WithWrap(ErrCheckConstraint)).(*Err)
|
||||
return New(CheckConstraint, WithMsg(msg), WithWrap(ErrCheckConstraint)).(*Err)
|
||||
default:
|
||||
return New(NotSpecificIntegrity, errorid, WithMsg(pqError.Message)).(*Err)
|
||||
return New(NotSpecificIntegrity, WithMsg(pqError.Message)).(*Err)
|
||||
}
|
||||
}
|
||||
if pqError.Code == "42P01" {
|
||||
return New(MissingTable, errorid, WithMsg(pqError.Message)).(*Err)
|
||||
return New(MissingTable, WithMsg(pqError.Message)).(*Err)
|
||||
}
|
||||
}
|
||||
if errors.Is(e, ErrRecordNotFound) {
|
||||
return New(RecordNotFound, errorid, WithMsg(e.Error())).(*Err)
|
||||
}
|
||||
// unfortunately, we can't help.
|
||||
return nil
|
||||
}
|
||||
@@ -112,8 +97,8 @@ func (e *Err) Error() string {
|
||||
return ""
|
||||
}
|
||||
var s strings.Builder
|
||||
if e.ErrorId != "" {
|
||||
join(&s, ": ", string(e.ErrorId))
|
||||
if e.Op != "" {
|
||||
join(&s, ": ", string(e.Op))
|
||||
}
|
||||
if e.Msg != "" {
|
||||
join(&s, ": ", e.Msg)
|
||||
|
||||
+21
-100
@@ -3,7 +3,6 @@ package errors_test
|
||||
import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
@@ -15,7 +14,6 @@ import (
|
||||
|
||||
func Test_NewError(t *testing.T) {
|
||||
t.Parallel()
|
||||
testId := errors.ErrorId("testid")
|
||||
tests := []struct {
|
||||
name string
|
||||
code errors.Code
|
||||
@@ -26,12 +24,13 @@ func Test_NewError(t *testing.T) {
|
||||
name: "all-options",
|
||||
code: errors.InvalidParameter,
|
||||
opt: []errors.Option{
|
||||
errors.WithOp("alice.Bob"),
|
||||
errors.WithWrap(errors.ErrRecordNotFound),
|
||||
errors.WithMsg("test msg"),
|
||||
},
|
||||
want: &errors.Err{
|
||||
Op: "alice.Bob",
|
||||
Wrapped: errors.ErrRecordNotFound,
|
||||
ErrorId: testId,
|
||||
Msg: "test msg",
|
||||
Code: errors.InvalidParameter,
|
||||
},
|
||||
@@ -40,79 +39,14 @@ func Test_NewError(t *testing.T) {
|
||||
name: "no-options",
|
||||
opt: nil,
|
||||
want: &errors.Err{
|
||||
ErrorId: testId,
|
||||
Code: errors.Unknown,
|
||||
Code: errors.Unknown,
|
||||
},
|
||||
},
|
||||
}
|
||||
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, testId, tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_WrapError(t *testing.T) {
|
||||
t.Parallel()
|
||||
testId := errors.ErrorId("testid")
|
||||
testErr := errors.New(errors.InvalidParameter, "uniqueId")
|
||||
tests := []struct {
|
||||
name string
|
||||
opt []errors.Option
|
||||
err error
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "boundary-error",
|
||||
err: testErr,
|
||||
opt: []errors.Option{
|
||||
errors.WithMsg("test msg"),
|
||||
},
|
||||
want: &errors.Err{
|
||||
Wrapped: testErr,
|
||||
ErrorId: testId,
|
||||
Msg: "test msg",
|
||||
Code: errors.InvalidParameter,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "boundary-error-no-msg",
|
||||
err: testErr,
|
||||
want: &errors.Err{
|
||||
Wrapped: testErr,
|
||||
ErrorId: testId,
|
||||
Code: errors.InvalidParameter,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "std-error",
|
||||
err: fmt.Errorf("std error"),
|
||||
want: &errors.Err{
|
||||
Wrapped: fmt.Errorf("std error"),
|
||||
ErrorId: testId,
|
||||
Code: errors.Unknown,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "conflicting-with-wrap",
|
||||
err: testErr,
|
||||
opt: []errors.Option{
|
||||
errors.WithWrap(fmt.Errorf("dont wrap this error")),
|
||||
},
|
||||
want: &errors.Err{
|
||||
Wrapped: testErr,
|
||||
ErrorId: testId,
|
||||
Code: errors.InvalidParameter,
|
||||
},
|
||||
},
|
||||
}
|
||||
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, testId, tt.opt...)
|
||||
err := errors.New(tt.code, tt.opt...)
|
||||
require.Error(err)
|
||||
assert.Equal(tt.want, err)
|
||||
})
|
||||
@@ -133,12 +67,12 @@ func TestError_Info(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Unknown",
|
||||
err: errors.New(errors.Unknown, "").(*errors.Err),
|
||||
err: errors.New(errors.Unknown).(*errors.Err),
|
||||
want: errors.Unknown,
|
||||
},
|
||||
{
|
||||
name: "InvalidParameter",
|
||||
err: errors.New(errors.InvalidParameter, "").(*errors.Err),
|
||||
err: errors.New(errors.InvalidParameter).(*errors.Err),
|
||||
want: errors.InvalidParameter,
|
||||
},
|
||||
}
|
||||
@@ -159,27 +93,22 @@ func TestError_Error(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "msg",
|
||||
err: errors.New(errors.Unknown, "", errors.WithMsg("test msg")),
|
||||
err: errors.New(errors.Unknown, errors.WithMsg("test msg")),
|
||||
want: "test msg: unknown: error #0",
|
||||
},
|
||||
{
|
||||
name: "code",
|
||||
err: errors.New(errors.CheckConstraint, ""),
|
||||
err: errors.New(errors.CheckConstraint),
|
||||
want: "constraint check failed, integrity violation: error #1000",
|
||||
},
|
||||
{
|
||||
name: "id",
|
||||
err: errors.New(errors.Unknown, "uniqueId"),
|
||||
want: "uniqueId: unknown, unknown: error #0",
|
||||
},
|
||||
{
|
||||
name: "id-msg-and-code",
|
||||
err: errors.New(errors.CheckConstraint, "uniqueId", errors.WithMsg("test msg")),
|
||||
want: "uniqueId: test msg: integrity violation: error #1000",
|
||||
name: "op-msg-and-code",
|
||||
err: errors.New(errors.CheckConstraint, errors.WithOp("alice.bob"), errors.WithMsg("test msg")),
|
||||
want: "alice.bob: test msg: integrity violation: error #1000",
|
||||
},
|
||||
{
|
||||
name: "unknown",
|
||||
err: errors.New(errors.Unknown, ""),
|
||||
err: errors.New(errors.Unknown),
|
||||
want: "unknown, unknown: error #0",
|
||||
},
|
||||
}
|
||||
@@ -200,8 +129,7 @@ func TestError_Error(t *testing.T) {
|
||||
|
||||
func TestError_Unwrap(t *testing.T) {
|
||||
t.Parallel()
|
||||
testId := errors.ErrorId("testid")
|
||||
testErr := errors.New(errors.Unknown, testId, errors.WithMsg("test error"))
|
||||
testErr := errors.New(errors.Unknown, errors.WithMsg("test error"))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -210,14 +138,8 @@ func TestError_Unwrap(t *testing.T) {
|
||||
wantIsErr error
|
||||
}{
|
||||
{
|
||||
name: "ErrInvalidParameterWithWrap",
|
||||
err: errors.New(errors.InvalidParameter, testId, errors.WithWrap(errors.ErrInvalidParameter)),
|
||||
want: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "ErrInvalidParameterWrap",
|
||||
err: errors.Wrap(errors.ErrInvalidParameter, testId),
|
||||
name: "ErrInvalidParameter",
|
||||
err: errors.New(errors.InvalidParameter, errors.WithWrap(errors.ErrInvalidParameter)),
|
||||
want: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
},
|
||||
@@ -248,7 +170,6 @@ func TestError_Unwrap(t *testing.T) {
|
||||
|
||||
func TestConvertError(t *testing.T) {
|
||||
t.Parallel()
|
||||
testId := errors.ErrorId("testid")
|
||||
const (
|
||||
createTable = `
|
||||
create table if not exists test_table (
|
||||
@@ -289,13 +210,13 @@ func TestConvertError(t *testing.T) {
|
||||
e: &pq.Error{
|
||||
Code: pq.ErrorCode("23001"),
|
||||
},
|
||||
wantErr: errors.New(errors.NotSpecificIntegrity, testId),
|
||||
wantErr: errors.New(errors.NotSpecificIntegrity),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
err := errors.Convert(tt.e, testId)
|
||||
err := errors.Convert(tt.e)
|
||||
if tt.wantErr == nil {
|
||||
assert.Nil(err)
|
||||
return
|
||||
@@ -313,7 +234,7 @@ func TestConvertError(t *testing.T) {
|
||||
_, err = rw.Exec(ctx, insert, []interface{}{"alice", "dup coworker", nil})
|
||||
require.Error(err)
|
||||
|
||||
e := errors.Convert(err, "")
|
||||
e := errors.Convert(err)
|
||||
require.NotNil(e)
|
||||
assert.True(errors.Is(e, errors.ErrNotUnique))
|
||||
assert.Equal("Key (name)=(alice) already exists.: integrity violation: error #1002: \nunique constraint violation: integrity violation: error #1002", e.Error())
|
||||
@@ -325,7 +246,7 @@ func TestConvertError(t *testing.T) {
|
||||
_, err = rw.Exec(ctx, insert, []interface{}{"alice", nil, nil})
|
||||
require.Error(err)
|
||||
|
||||
e := errors.Convert(err, "")
|
||||
e := errors.Convert(err)
|
||||
require.NotNil(e)
|
||||
assert.True(errors.Is(e, errors.ErrNotNull))
|
||||
assert.Equal("description must not be empty: integrity violation: error #1001: \nnot null constraint violated: integrity violation: error #1001", e.Error())
|
||||
@@ -337,7 +258,7 @@ func TestConvertError(t *testing.T) {
|
||||
_, err = rw.Exec(ctx, insert, []interface{}{"alice", "coworker", "one"})
|
||||
require.Error(err)
|
||||
|
||||
e := errors.Convert(err, "")
|
||||
e := errors.Convert(err)
|
||||
require.NotNil(e)
|
||||
assert.True(errors.Is(e, errors.ErrCheckConstraint))
|
||||
assert.Equal("test_table_five_check constraint failed: integrity violation: error #1000: \ncheck constraint violated: integrity violation: error #1000", e.Error())
|
||||
@@ -346,7 +267,7 @@ func TestConvertError(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
_, err := rw.Exec(ctx, missingTable, nil)
|
||||
require.Error(err)
|
||||
e := errors.Convert(err, "")
|
||||
e := errors.Convert(err)
|
||||
require.NotNil(e)
|
||||
assert.True(errors.Match(errors.T(errors.MissingTable), e))
|
||||
assert.Equal("relation \"not_a_defined_table\" does not exist: integrity violation: error #1004", e.Error())
|
||||
|
||||
@@ -44,9 +44,7 @@ func TestError_IsUnique(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "wrapped-pq-is-unique",
|
||||
in: errors.New(
|
||||
errors.NotUnique,
|
||||
"",
|
||||
in: errors.New(errors.NotUnique,
|
||||
errors.WithWrap(&pq.Error{
|
||||
Code: pq.ErrorCode("23505"),
|
||||
}),
|
||||
@@ -98,14 +96,12 @@ func TestError_IsCheckConstraint(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "ErrCodeCheckConstraint",
|
||||
in: errors.New(errors.CheckConstraint, ""),
|
||||
in: errors.New(errors.CheckConstraint),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped-pq-is-check-constraint",
|
||||
in: errors.New(
|
||||
errors.CheckConstraint,
|
||||
"",
|
||||
in: errors.New(errors.CheckConstraint,
|
||||
errors.WithWrap(&pq.Error{
|
||||
Code: pq.ErrorCode("23514"),
|
||||
}),
|
||||
@@ -164,14 +160,12 @@ func TestError_IsNotNullError(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "ErrCodeNotNull",
|
||||
in: errors.New(errors.NotNull, ""),
|
||||
in: errors.New(errors.NotNull),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped-pq-is-not-null",
|
||||
in: errors.New(
|
||||
errors.NotNull,
|
||||
"",
|
||||
in: errors.New(errors.NotNull,
|
||||
errors.WithWrap(&pq.Error{
|
||||
Code: pq.ErrorCode("23502"),
|
||||
}),
|
||||
|
||||
@@ -16,10 +16,10 @@ func T(args ...interface{}) *Template {
|
||||
switch arg := a.(type) {
|
||||
case Code:
|
||||
t.Code = arg
|
||||
case ErrorId:
|
||||
t.ErrorId = arg
|
||||
case string:
|
||||
t.Msg = arg
|
||||
case Op:
|
||||
t.Op = arg
|
||||
case *Err: // order is important, this match must before "case error:"
|
||||
c := *arg
|
||||
t.Wrapped = &c
|
||||
@@ -78,7 +78,7 @@ func Match(t *Template, err error) bool {
|
||||
if t.Msg != "" && t.Msg != e.Msg {
|
||||
return false
|
||||
}
|
||||
if t.ErrorId != "" && t.ErrorId != e.ErrorId {
|
||||
if t.Op != "" && t.Op != e.Op {
|
||||
return false
|
||||
}
|
||||
if t.Kind != Other && t.Info().Kind != e.Info().Kind {
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
func TestT(t *testing.T) {
|
||||
t.Parallel()
|
||||
stdErr := stderrors.New("test error")
|
||||
testId := ErrorId("testid")
|
||||
tests := []struct {
|
||||
name string
|
||||
args []interface{}
|
||||
@@ -20,7 +19,7 @@ func TestT(t *testing.T) {
|
||||
name: "all fields",
|
||||
args: []interface{}{
|
||||
"test error msg",
|
||||
testId,
|
||||
Op("alice.Bob"),
|
||||
InvalidParameter,
|
||||
stdErr,
|
||||
Integrity,
|
||||
@@ -28,8 +27,8 @@ func TestT(t *testing.T) {
|
||||
want: &Template{
|
||||
Err: Err{
|
||||
Code: InvalidParameter,
|
||||
ErrorId: testId,
|
||||
Msg: "test error msg",
|
||||
Op: "alice.Bob",
|
||||
Wrapped: stdErr,
|
||||
},
|
||||
Kind: Integrity,
|
||||
@@ -110,7 +109,6 @@ func TestTemplate_Info(t *testing.T) {
|
||||
func TestTemplate_Error(t *testing.T) {
|
||||
t.Parallel()
|
||||
stdErr := stderrors.New("test error")
|
||||
testId := ErrorId("testid")
|
||||
tests := []struct {
|
||||
name string
|
||||
template *Template
|
||||
@@ -123,7 +121,7 @@ func TestTemplate_Error(t *testing.T) {
|
||||
name: "all params",
|
||||
template: T(
|
||||
"test error msg",
|
||||
testId,
|
||||
Op("alice.Bob"),
|
||||
InvalidParameter,
|
||||
stdErr,
|
||||
Integrity,
|
||||
@@ -142,7 +140,6 @@ func TestTemplate_Error(t *testing.T) {
|
||||
func TestMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
stdErr := stderrors.New("test error")
|
||||
testId := ErrorId("testid")
|
||||
tests := []struct {
|
||||
name string
|
||||
template *Template
|
||||
@@ -152,7 +149,7 @@ func TestMatch(t *testing.T) {
|
||||
{
|
||||
name: "nil template",
|
||||
template: nil,
|
||||
err: New(NotUnique, testId, WithMsg("this thing was must be unique")),
|
||||
err: New(NotUnique, WithMsg("this thing was must be unique")),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
@@ -166,8 +163,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(Integrity),
|
||||
err: New(
|
||||
NotUnique,
|
||||
testId,
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: true,
|
||||
@@ -177,8 +174,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(Integrity),
|
||||
err: New(
|
||||
RecordNotFound,
|
||||
testId,
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: false,
|
||||
@@ -188,8 +185,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(NotUnique),
|
||||
err: New(
|
||||
NotUnique,
|
||||
testId,
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: true,
|
||||
@@ -199,30 +196,30 @@ func TestMatch(t *testing.T) {
|
||||
template: T(NotUnique),
|
||||
err: New(
|
||||
RecordNotFound,
|
||||
testId,
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "match on Op only",
|
||||
template: T(ErrorId("unique")),
|
||||
template: T(Op("alice.Bob")),
|
||||
err: New(
|
||||
NotUnique,
|
||||
"unique",
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "no match on Op only",
|
||||
template: T(ErrorId("unique")),
|
||||
template: T(Op("alice.Alice")),
|
||||
err: New(
|
||||
RecordNotFound,
|
||||
testId,
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: false,
|
||||
@@ -234,12 +231,12 @@ func TestMatch(t *testing.T) {
|
||||
Integrity,
|
||||
InvalidParameter,
|
||||
ErrInvalidFieldMask,
|
||||
ErrorId("unique"),
|
||||
Op("alice.Bob"),
|
||||
),
|
||||
err: New(
|
||||
InvalidParameter,
|
||||
"unique",
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: true,
|
||||
@@ -249,8 +246,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(ErrInvalidFieldMask),
|
||||
err: New(
|
||||
NotUnique,
|
||||
testId,
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: true,
|
||||
@@ -260,8 +257,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(ErrNotUnique),
|
||||
err: New(
|
||||
RecordNotFound,
|
||||
testId,
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(ErrInvalidFieldMask),
|
||||
),
|
||||
want: false,
|
||||
@@ -271,8 +268,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(stdErr),
|
||||
err: New(
|
||||
NotUnique,
|
||||
testId,
|
||||
WithMsg("this thing must be unique"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(stdErr),
|
||||
),
|
||||
want: true,
|
||||
@@ -282,8 +279,8 @@ func TestMatch(t *testing.T) {
|
||||
template: T(stderrors.New("no match")),
|
||||
err: New(
|
||||
RecordNotFound,
|
||||
testId,
|
||||
WithMsg("this thing is missing"),
|
||||
WithOp("alice.Bob"),
|
||||
WithWrap(stdErr),
|
||||
),
|
||||
want: false,
|
||||
|
||||
@@ -16,6 +16,7 @@ type Option func(*Options)
|
||||
type Options struct {
|
||||
withErrWrapped error
|
||||
withErrMsg string
|
||||
withOp Op
|
||||
}
|
||||
|
||||
func getDefaultOptions() Options {
|
||||
@@ -37,3 +38,11 @@ func WithMsg(msg string) Option {
|
||||
o.withErrMsg = msg
|
||||
}
|
||||
}
|
||||
|
||||
// WithOp provides an option to provide the operation that's raising/propagating
|
||||
// the error.
|
||||
func WithOp(op Op) Option {
|
||||
return func(o *Options) {
|
||||
o.withOp = op
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -32,9 +31,21 @@ func Test_getOpts(t *testing.T) {
|
||||
assert.Equal(opts, testOpts)
|
||||
|
||||
// try setting it
|
||||
err := fmt.Errorf("test error")
|
||||
opts = GetOpts(WithWrap(err))
|
||||
testOpts.withErrWrapped = err
|
||||
opts = GetOpts(WithWrap(ErrInvalidParameter))
|
||||
testOpts.withErrWrapped = ErrInvalidParameter
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
t.Run("WithOp", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
// test default
|
||||
opts := GetOpts()
|
||||
testOpts := getDefaultOptions()
|
||||
testOpts.withOp = ""
|
||||
assert.Equal(opts, testOpts)
|
||||
|
||||
// try setting it
|
||||
opts = GetOpts(WithOp("alice.bob"))
|
||||
testOpts.withOp = "alice.bob"
|
||||
assert.Equal(opts, testOpts)
|
||||
})
|
||||
}
|
||||
@@ -1,42 +1,44 @@
|
||||
package errors
|
||||
|
||||
// Errors returned from this package may be tested against these errors with errors.Is.
|
||||
// TODO: Deprecate sentinel errors in favor of the new Err type that includes unique ErrorId and Code.
|
||||
// Errors returned from this package may be tested against these errors
|
||||
// with errors.Is. Creating new Sentinel type errors like these should be
|
||||
// deprecated in favor of the new Err type that includes unique Codes and a
|
||||
// Matching function.
|
||||
var (
|
||||
// ErrInvalidPublicId indicates an invalid PublicId.
|
||||
ErrInvalidPublicId = New(InvalidParameter, "", WithMsg("invalid publicId"))
|
||||
ErrInvalidPublicId = New(InvalidParameter, WithMsg("invalid publicId"))
|
||||
|
||||
// ErrInvalidParameter is returned by create and update methods if
|
||||
// an attribute on a struct contains illegal or invalid values.
|
||||
ErrInvalidParameter = New(InvalidParameter, "", WithMsg("invalid parameter"))
|
||||
ErrInvalidParameter = New(InvalidParameter, WithMsg("invalid parameter"))
|
||||
|
||||
// ErrInvalidFieldMask is returned by update methods if the field mask
|
||||
// contains unknown fields or fields that cannot be updated.
|
||||
ErrInvalidFieldMask = New(InvalidParameter, "", WithMsg("invalid field mask"))
|
||||
ErrInvalidFieldMask = New(InvalidParameter, WithMsg("invalid field mask"))
|
||||
|
||||
// ErrEmptyFieldMask is returned by update methods if the field mask is
|
||||
// empty.
|
||||
ErrEmptyFieldMask = New(InvalidParameter, "", WithMsg("empty field mask"))
|
||||
ErrEmptyFieldMask = New(InvalidParameter, WithMsg("empty field mask"))
|
||||
|
||||
// ErrNotUnique is returned by create and update methods when a write
|
||||
// to the repository resulted in a unique constraint violation.
|
||||
ErrNotUnique = New(NotUnique, "", WithMsg("unique constraint violation"))
|
||||
ErrNotUnique = New(NotUnique, WithMsg("unique constraint violation"))
|
||||
|
||||
// ErrNotNull is returned by methods when a write to the repository resulted
|
||||
// in a check constraint violation
|
||||
ErrCheckConstraint = New(CheckConstraint, "", WithMsg("check constraint violated"))
|
||||
ErrCheckConstraint = New(CheckConstraint, WithMsg("check constraint violated"))
|
||||
|
||||
// ErrNotNull is returned by methods when a write to the repository resulted
|
||||
// in a not null constraint violation
|
||||
ErrNotNull = New(NotNull, "", WithMsg("not null constraint violated"))
|
||||
ErrNotNull = New(NotNull, WithMsg("not null constraint violated"))
|
||||
|
||||
// ErrRecordNotFound returns a "record not found" error and it only occurs
|
||||
// when attempting to read from the database into struct.
|
||||
// When reading into a slice it won't return this error.
|
||||
ErrRecordNotFound = New(RecordNotFound, "", WithMsg("record not found"))
|
||||
ErrRecordNotFound = New(RecordNotFound, WithMsg("record not found"))
|
||||
|
||||
// ErrMultipleRecords is returned by update and delete methods when a
|
||||
// write to the repository would result in more than one record being
|
||||
// changed resulting in the transaction being rolled back.
|
||||
ErrMultipleRecords = New(MultipleRecords, "", WithMsg("multiple records"))
|
||||
ErrMultipleRecords = New(MultipleRecords, WithMsg("multiple records"))
|
||||
)
|
||||
@@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/hashicorp/vault/sdk/helper/base62"
|
||||
)
|
||||
|
||||
const idLength = 10
|
||||
|
||||
func main() {
|
||||
rand, err := base62.Random(idLength)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to generate error ID: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(rand)
|
||||
}
|
||||
Reference in new issue
Block a user