From dcd73d6ce8b35ed370c8f4ad9fd89f664dc6ed44 Mon Sep 17 00:00:00 2001 From: Gabe <7622243+decentralgabe@users.noreply.github.com> Date: Tue, 9 Apr 2024 20:06:03 -0700 Subject: [PATCH] context loggers (#523) * context loggers * update go version --- .github/workflows/ci.yml | 4 ++-- .github/workflows/golangci-lint.yml | 2 +- CONTRIBUTING.md | 4 ++-- README.md | 2 +- sd-jwt/README.md | 2 +- util/errors.go | 33 +++++++++++++++++++++++++++++ 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 314b2bb6..57e73c16 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.22.1 + go-version: 1.22.2 - name: Install Mage run: go install github.com/magefile/mage @@ -38,7 +38,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.22.1 + go-version: 1.22.2 - name: Install Mage run: go install github.com/magefile/mage diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index f6e01e19..a578ea44 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -16,7 +16,7 @@ jobs: steps: - uses: actions/setup-go@v3 with: - go-version: 1.22.1 + go-version: 1.22.2 - uses: actions/checkout@v3 - name: golangci-lint uses: golangci/golangci-lint-action@v3 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index eb5dfc38..38efa5ea 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,7 +12,7 @@ When you're ready you may: | Requirement | Tested Version | Installation Instructions | |-------------|----------------|--------------------------------------------------------| -| Go | 1.22.1 | [go.dev](https://go.dev/doc/tutorial/compile-install) | +| Go | 1.22.2 | [go.dev](https://go.dev/doc/tutorial/compile-install) | | Mage | 1.13.0-6 | [magefile.org](https://magefile.org/) | ### Go @@ -23,7 +23,7 @@ You may verify your `go` installation via the terminal: ``` $> go version -go version go1.22.1 darwin/amd64 +go version go1.22.2 darwin/amd64 ``` If you do not have go, we recommend installing it by: diff --git a/README.md b/README.md index d192d5dc..a1cacffc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ⚠️ This repository is not actively being maintained. For a go SDK please look at [web5-go](https://github.com/TBD54566975/web5-go/). ⚠️ [![godoc ssi-sdk](https://img.shields.io/badge/godoc-ssi--sdk-blue)](https://pkg.go.dev/github.com/TBD54566975/ssi-sdk) -[![go version 1.22.1](https://img.shields.io/badge/go_version-1.22.1-brightgreen)](https://golang.org/) +[![go version 1.22.2](https://img.shields.io/badge/go_version-1.22.2-brightgreen)](https://golang.org/) [![Go Report Card A+](https://goreportcard.com/badge/github.com/TBD54566975/ssi-sdk)](https://goreportcard.com/report/github.com/TBD54566975/ssi-sdk) [![license Apache 2](https://img.shields.io/badge/license-Apache%202-black)](https://github.com/TBD54566975/ssi-sdk/blob/main/LICENSE) [![issues](https://img.shields.io/github/issues/TBD54566975/ssi-sdk)](https://github.com/TBD54566975/ssi-sdk/issues) diff --git a/sd-jwt/README.md b/sd-jwt/README.md index 81a26f3b..ad6cf54e 100644 --- a/sd-jwt/README.md +++ b/sd-jwt/README.md @@ -1,5 +1,5 @@ [![godoc ssi-sdk](https://img.shields.io/badge/godoc-ssi--sdk-blue)](https://pkg.go.dev/github.com/TBD54566975/ssi-sdk/sd-jwt) -[![go version 1.22.1](https://img.shields.io/badge/go_version-1.22.1-brightgreen)](https://golang.org/) +[![go version 1.22.2](https://img.shields.io/badge/go_version-1.22.2-brightgreen)](https://golang.org/) [![Go Report Card A+](https://goreportcard.com/badge/github.com/TBD54566975/ssi-sdk/sd-jwt)](https://goreportcard.com/report/github.com/TBD54566975/ssi-sdk/sd-jwt) [![license Apache 2](https://img.shields.io/badge/license-Apache%202-black)](https://github.com/TBD54566975/ssi-sdk/blob/main/LICENSE) diff --git a/util/errors.go b/util/errors.go index 74574ae5..ed00d664 100644 --- a/util/errors.go +++ b/util/errors.go @@ -1,6 +1,7 @@ package util import ( + "context" "fmt" "strings" @@ -23,6 +24,12 @@ func LoggingError(err error) error { return err } +// LoggingCtxError is a utility to combine logging an error, and returning and error +func LoggingCtxError(ctx context.Context, err error) error { + logrus.WithContext(ctx).WithError(err).Error() + return err +} + // LoggingNewError is a utility to create an error from a message, log it, and return it as an error func LoggingNewError(msg string) error { err := errors.New(msg) @@ -30,11 +37,23 @@ func LoggingNewError(msg string) error { return err } +// LoggingCtxNewError is a utility to create an error from a message, log it, and return it as an error +func LoggingCtxNewError(ctx context.Context, msg string) error { + err := errors.New(msg) + logrus.WithContext(ctx).WithError(err).Error() + return err +} + // LoggingNewErrorf is a utility to create an error from a formatted message, log it, and return it as an error func LoggingNewErrorf(msg string, args ...any) error { return LoggingNewError(fmt.Sprintf(msg, args...)) } +// LoggingCtxNewErrorf is a utility to create an error from a formatted message, log it, and return it as an error +func LoggingCtxNewErrorf(ctx context.Context, msg string, args ...any) error { + return LoggingCtxNewError(ctx, fmt.Sprintf(msg, args...)) +} + // LoggingErrorMsg is a utility to combine logging an error, and returning and error with a message func LoggingErrorMsg(err error, msg string) error { logrus.WithError(err).Error(SanitizeLog(msg)) @@ -44,11 +63,25 @@ func LoggingErrorMsg(err error, msg string) error { return errors.Wrap(err, msg) } +// LoggingCtxErrorMsg is a utility to combine logging an error, and returning and error with a message +func LoggingCtxErrorMsg(ctx context.Context, err error, msg string) error { + logrus.WithContext(ctx).WithError(err).Error(SanitizeLog(msg)) + if err == nil { + return errors.New(msg) + } + return errors.Wrap(err, msg) +} + // LoggingErrorMsgf is a utility to combine logging an error, and returning and error with a formatted message func LoggingErrorMsgf(err error, msg string, args ...any) error { return LoggingErrorMsg(err, fmt.Sprintf(msg, args...)) } +// LoggingCtxErrorMsgf is a utility to combine logging an error, and returning and error with a formatted message +func LoggingCtxErrorMsgf(ctx context.Context, err error, msg string, args ...any) error { + return LoggingCtxErrorMsg(ctx, err, fmt.Sprintf(msg, args...)) +} + // SanitizeLog prevents certain classes of injection attacks before logging // https://codeql.github.com/codeql-query-help/go/go-log-injection/ func SanitizeLog(log string) string {