Skip to content

Commit

Permalink
added ToErrorf
Browse files Browse the repository at this point in the history
  • Loading branch information
mscno committed Sep 6, 2024
1 parent 4302586 commit ee17edb
Show file tree
Hide file tree
Showing 26 changed files with 235 additions and 13 deletions.
6 changes: 5 additions & 1 deletion already_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func AlreadyExistsBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrAlreadyExists)
}

func ToAlreadyExists(parent error, format string, a ...interface{}) error {
func ToAlreadyExists(parent error, KVs ...interface{}) error {
return oops.Code(ErrAlreadyExists).With(KVs...).Wrap(parent)
}

func ToAlreadyExistsf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrAlreadyExists).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions already_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestAlreadyExistsf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToAlreadyExistsf(t *testing.T) {
err := ToAlreadyExistsf(errors.New("something happened"), "key: %s", "value")
ok := IsAlreadyExists(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrAlreadyExists, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion deadline_exceeded.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func DeadlineExceededBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrDeadlineExceeded)
}

func ToDeadlineExceeded(parent error, format string, a ...interface{}) error {
func ToDeadlineExceeded(parent error, KVs ...interface{}) error {
return oops.Code(ErrDeadlineExceeded).With(KVs...).Wrap(parent)
}

func ToDeadlineExceededf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrDeadlineExceeded).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions deadline_exceeded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestDeadlineExceededf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToDeadlineExceededf(t *testing.T) {
err := ToDeadlineExceededf(errors.New("something happened"), "key: %s", "value")
ok := IsDeadlineExceeded(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrDeadlineExceeded, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion failed_precondition.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func FailedPreconditionBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrFailedPrecondition)
}

func ToFailedPrecondition(parent error, format string, a ...interface{}) error {
func ToFailedPrecondition(parent error, KVs ...interface{}) error {
return oops.Code(ErrFailedPrecondition).With(KVs...).Wrap(parent)
}

func ToFailedPreconditionf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrFailedPrecondition).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions failed_precondition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestFailedPreconditionf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToFailedPreconditionf(t *testing.T) {
err := ToFailedPreconditionf(errors.New("something happened"), "key: %s", "value")
ok := IsFailedPrecondition(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrFailedPrecondition, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion generate/error.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func {{ .ErrorName }}Builder() oops.OopsErrorBuilder {
return oops.Code(Err{{ .ErrorName }})
}

func To{{ .ErrorName }}(parent error, format string, a ...interface{}) error {
func To{{ .ErrorName }}(parent error, KVs ...interface{}) error {
return oops.Code(Err{{ .ErrorName }}).With(KVs...).Wrap(parent)
}

func To{{ .ErrorName }}f(parent error, format string, a ...interface{}) error {
return oops.Code(Err{{ .ErrorName }}).Wrapf(parent, format, a...)
}

Expand Down
14 changes: 14 additions & 0 deletions generate/error_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ func Test{{ .ErrorName }}f(t *testing.T) {
assert.True(t, ok)

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestTo{{ .ErrorName }}f(t *testing.T) {
err := To{{ .ErrorName }}f(errors.New("something happened"),"key: %s", "value")
ok := Is{{ .ErrorName }}(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, Err{{ .ErrorName }}, e.Code())


assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func InternalBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrInternal)
}

func ToInternal(parent error, format string, a ...interface{}) error {
func ToInternal(parent error, KVs ...interface{}) error {
return oops.Code(ErrInternal).With(KVs...).Wrap(parent)
}

func ToInternalf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrInternal).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestInternalf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToInternalf(t *testing.T) {
err := ToInternalf(errors.New("something happened"), "key: %s", "value")
ok := IsInternal(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrInternal, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion invalid_argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func InvalidArgumentBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrInvalidArgument)
}

func ToInvalidArgument(parent error, format string, a ...interface{}) error {
func ToInvalidArgument(parent error, KVs ...interface{}) error {
return oops.Code(ErrInvalidArgument).With(KVs...).Wrap(parent)
}

func ToInvalidArgumentf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrInvalidArgument).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions invalid_argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestInvalidArgumentf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToInvalidArgumentf(t *testing.T) {
err := ToInvalidArgumentf(errors.New("something happened"), "key: %s", "value")
ok := IsInvalidArgument(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrInvalidArgument, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion not_found.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func NotFoundBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrNotFound)
}

func ToNotFound(parent error, format string, a ...interface{}) error {
func ToNotFound(parent error, KVs ...interface{}) error {
return oops.Code(ErrNotFound).With(KVs...).Wrap(parent)
}

func ToNotFoundf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrNotFound).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions not_found_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestNotFoundf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToNotFoundf(t *testing.T) {
err := ToNotFoundf(errors.New("something happened"), "key: %s", "value")
ok := IsNotFound(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrNotFound, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion permission_denied.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func PermissionDeniedBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrPermissionDenied)
}

func ToPermissionDenied(parent error, format string, a ...interface{}) error {
func ToPermissionDenied(parent error, KVs ...interface{}) error {
return oops.Code(ErrPermissionDenied).With(KVs...).Wrap(parent)
}

func ToPermissionDeniedf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrPermissionDenied).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions permission_denied_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestPermissionDeniedf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToPermissionDeniedf(t *testing.T) {
err := ToPermissionDeniedf(errors.New("something happened"), "key: %s", "value")
ok := IsPermissionDenied(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrPermissionDenied, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion resource_exhausted.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func ResourceExhaustedBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrResourceExhausted)
}

func ToResourceExhausted(parent error, format string, a ...interface{}) error {
func ToResourceExhausted(parent error, KVs ...interface{}) error {
return oops.Code(ErrResourceExhausted).With(KVs...).Wrap(parent)
}

func ToResourceExhaustedf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrResourceExhausted).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions resource_exhausted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestResourceExhaustedf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToResourceExhaustedf(t *testing.T) {
err := ToResourceExhaustedf(errors.New("something happened"), "key: %s", "value")
ok := IsResourceExhausted(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrResourceExhausted, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion unauthenticated.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func UnauthenticatedBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrUnauthenticated)
}

func ToUnauthenticated(parent error, format string, a ...interface{}) error {
func ToUnauthenticated(parent error, KVs ...interface{}) error {
return oops.Code(ErrUnauthenticated).With(KVs...).Wrap(parent)
}

func ToUnauthenticatedf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrUnauthenticated).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions unauthenticated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestUnauthenticatedf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToUnauthenticatedf(t *testing.T) {
err := ToUnauthenticatedf(errors.New("something happened"), "key: %s", "value")
ok := IsUnauthenticated(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnauthenticated, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion unavailable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func UnavailableBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrUnavailable)
}

func ToUnavailable(parent error, format string, a ...interface{}) error {
func ToUnavailable(parent error, KVs ...interface{}) error {
return oops.Code(ErrUnavailable).With(KVs...).Wrap(parent)
}

func ToUnavailablef(parent error, format string, a ...interface{}) error {
return oops.Code(ErrUnavailable).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions unavailable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestUnavailablef(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToUnavailablef(t *testing.T) {
err := ToUnavailablef(errors.New("something happened"), "key: %s", "value")
ok := IsUnavailable(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnavailable, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion unimplemented.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func UnimplementedBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrUnimplemented)
}

func ToUnimplemented(parent error, format string, a ...interface{}) error {
func ToUnimplemented(parent error, KVs ...interface{}) error {
return oops.Code(ErrUnimplemented).With(KVs...).Wrap(parent)
}

func ToUnimplementedf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrUnimplemented).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions unimplemented_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestUnimplementedf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToUnimplementedf(t *testing.T) {
err := ToUnimplementedf(errors.New("something happened"), "key: %s", "value")
ok := IsUnimplemented(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnimplemented, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}
6 changes: 5 additions & 1 deletion unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func UnknownBuilder() oops.OopsErrorBuilder {
return oops.Code(ErrUnknown)
}

func ToUnknown(parent error, format string, a ...interface{}) error {
func ToUnknown(parent error, KVs ...interface{}) error {
return oops.Code(ErrUnknown).With(KVs...).Wrap(parent)
}

func ToUnknownf(parent error, format string, a ...interface{}) error {
return oops.Code(ErrUnknown).Wrapf(parent, format, a...)
}

Expand Down
13 changes: 13 additions & 0 deletions unknown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ func TestUnknownf(t *testing.T) {

assert.Equal(t, err.Error(), "something happened: reason")
}

func TestToUnknownf(t *testing.T) {
err := ToUnknownf(errors.New("something happened"), "key: %s", "value")
ok := IsUnknown(err)
assert.True(t, ok)

var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnknown, e.Code())

assert.Equal(t, err.Error(), "key: value: something happened")
}

0 comments on commit ee17edb

Please sign in to comment.