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
@@ -318,6 +318,8 @@ func newSendCtx(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
var sendCtx context.Context
|
||||
var sendCancel context.CancelFunc
|
||||
switch {
|
||||
case ctx == nil:
|
||||
return context.Background(), nil
|
||||
case ctx.Err() == context.Canceled:
|
||||
sendCtx, sendCancel = context.WithTimeout(context.Background(), cancelledSendTimeout)
|
||||
info, ok := RequestInfoFromContext(ctx)
|
||||
|
||||
@@ -2,10 +2,9 @@ package event
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
)
|
||||
|
||||
// serializedWriter uses a mutext to serializes all writes to its io.Writer
|
||||
@@ -18,13 +17,13 @@ type serializedWriter struct {
|
||||
func (s *serializedWriter) Write(p []byte) (int, error) {
|
||||
const op = "event.(serializedWriter).Write"
|
||||
if s == nil {
|
||||
return 0, errors.New(errors.InvalidParameter, op, "missing serialized writer")
|
||||
return 0, fmt.Errorf("%s: missing serialized writer: %w", op, ErrInvalidParameter)
|
||||
}
|
||||
if s.l == nil {
|
||||
return 0, errors.New(errors.InvalidParameter, op, "missing lock")
|
||||
return 0, fmt.Errorf("%s: missing lock: %w", op, ErrInvalidParameter)
|
||||
}
|
||||
if s.w == nil {
|
||||
return 0, errors.New(errors.InvalidParameter, op, "missing writer")
|
||||
return 0, fmt.Errorf("%s: missing writer: %w", op, ErrInvalidParameter)
|
||||
}
|
||||
|
||||
s.l.Lock()
|
||||
|
||||
@@ -1,32 +1,35 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var errTestWriteFailed = errors.New("bad write")
|
||||
|
||||
type testBadWriter struct{}
|
||||
|
||||
func (b *testBadWriter) Write(p []byte) (int, error) {
|
||||
const op = "event.(testBadWriter).Write"
|
||||
return 0, errors.New(errors.Internal, op, "write failed")
|
||||
return 0, fmt.Errorf("%s: write failed: %w", op, errTestWriteFailed)
|
||||
}
|
||||
|
||||
func TestSerializedWriter_Write(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
s *serializedWriter
|
||||
wantErrMatch *errors.Template
|
||||
wantErrIs error
|
||||
wantErrContains string
|
||||
}{
|
||||
{
|
||||
name: "missing-serializedWriter",
|
||||
wantErrMatch: errors.T(errors.InvalidParameter),
|
||||
wantErrIs: ErrInvalidParameter,
|
||||
wantErrContains: "missing serialized writer",
|
||||
},
|
||||
{
|
||||
@@ -34,7 +37,7 @@ func TestSerializedWriter_Write(t *testing.T) {
|
||||
s: &serializedWriter{
|
||||
l: new(sync.Mutex),
|
||||
},
|
||||
wantErrMatch: errors.T(errors.InvalidParameter),
|
||||
wantErrIs: ErrInvalidParameter,
|
||||
wantErrContains: "missing writer",
|
||||
},
|
||||
{
|
||||
@@ -42,7 +45,7 @@ func TestSerializedWriter_Write(t *testing.T) {
|
||||
s: &serializedWriter{
|
||||
w: os.Stderr,
|
||||
},
|
||||
wantErrMatch: errors.T(errors.InvalidParameter),
|
||||
wantErrIs: ErrInvalidParameter,
|
||||
wantErrContains: "missing lock",
|
||||
},
|
||||
{
|
||||
@@ -51,7 +54,7 @@ func TestSerializedWriter_Write(t *testing.T) {
|
||||
w: &testBadWriter{},
|
||||
l: new(sync.Mutex),
|
||||
},
|
||||
wantErrMatch: errors.T(errors.Internal),
|
||||
wantErrIs: errTestWriteFailed,
|
||||
wantErrContains: "write failed",
|
||||
},
|
||||
}
|
||||
@@ -60,10 +63,10 @@ func TestSerializedWriter_Write(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
n, err := tt.s.Write([]byte("fido"))
|
||||
if tt.wantErrMatch != nil {
|
||||
if tt.wantErrIs != nil {
|
||||
require.Error(err)
|
||||
require.Empty(n)
|
||||
assert.Truef(errors.Match(tt.wantErrMatch, err), "got %q and wanted %q", tt.wantErrMatch.Code, err)
|
||||
assert.ErrorIs(err, tt.wantErrIs)
|
||||
if tt.wantErrContains != "" {
|
||||
assert.Contains(err.Error(), tt.wantErrContains)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user