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:
s-christoffandJim authored and GitHub committed 2021-07-29 20:52:56 -04:00
1 parent 02ff2db66e
commit 6b78108ecf
227 files changed
+3945 -3736

No files matched your search

+80 -14
View File
@@ -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.