Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking nil contexts in fctx #33

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
checking nil context in fctx
  • Loading branch information
semihbkgr committed Jun 10, 2023
commit 1d5e48a53fd6f89ccf533f1bbc0bbe603b8cd101
6 changes: 3 additions & 3 deletions fault.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func Wrap(err error, w ...Wrapper) error {
return nil
}

// the passed err might already have a location if it's a fault.New error, or it might not if it's another type of
// The passed err might already have a location if it's a fault. New error, or it might not if it's another type of
semihbkgr marked this conversation as resolved.
Show resolved Hide resolved
// error like one from the standard library. Wrapping it in a container with an empty location ensures that the
// location will be reset when we flatten the error chain. If the error is a fault.New error, it will itself be
// wrapped in a container which will have a location
// location will be reset when we flatten the error chain. If the error is a fault. New error, it will itself be
// wrapped in a container which will have a location.
if _, ok := err.(*container); !ok {
err = &container{
cause: err,
Expand Down
28 changes: 17 additions & 11 deletions fctx/fctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (e *withContext) String() string { return e.Error() }
// "post_id", postID,
// )
func WithMeta(ctx context.Context, kv ...string) context.Context {
if ctx == nil {
return nil
}

data := make(map[string]string)

// overwrite any existing context metadata
Expand Down Expand Up @@ -90,8 +94,8 @@ func WithMeta(ctx context.Context, kv ...string) context.Context {
// "role", "admin")
// }
func Wrap(err error, ctx context.Context, kv ...string) error {
if err == nil {
return nil
if err == nil || ctx == nil {
return err
}

meta := make(map[string]string)
Expand All @@ -107,17 +111,15 @@ func Wrap(err error, ctx context.Context, kv ...string) error {
}

l := len(kv)
if l >= 2 {
if l%2 != 0 {
l -= 1 // don't error on odd number of args
}
if l%2 != 0 {
l -= 1 // don't error on odd number of args
}

for i := 0; i < l; i += 2 {
k := kv[i]
v := kv[i+1]
for i := 0; i < l; i += 2 {
k := kv[i]
v := kv[i+1]

meta[k] = v
}
meta[k] = v
}

return &withContext{err, meta}
Expand Down Expand Up @@ -205,6 +207,10 @@ func Unwrap(err error) map[string]string {
//
// Which will flatten out the KV metadata from the context into the log entry.
func GetMeta(ctx context.Context) map[string]string {
if ctx == nil {
return nil
}

meta, ok := ctx.Value(contextKey{}).(map[string]string)
if !ok {
return nil
Expand Down