
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
go.wamod.dev/warning
warning
package provides mechanisms for capturing diagnostics using context.Context
.
It allows to easily capture warning without modifying existing function signatures.
warning are captured using a Collector
, which is attached to the context.Context
.
The package offers the following functionalities:
To use this package in your Go project, you can import it using:
import "go.wamod.dev/warning"
To start capturing warning create a new Collector
:
collector := warning.NewCollector()
defer collector.Close() // close before exiting
Attach this collector to your context:
ctx := warning.Attach(context.Background(), collector)
Then, you can write warning to the context:
warning.Warnf(ctx, "this is a warning")
Finally, capture all of warning back from the collector:
wrrs, err := warning.ReadAll(collector)
This example demonstrates how to use the Filter
function to filter warning:
// filter warnings
ctx = warning.Filter(ctx, func(wrr warning.Warning) bool {
return !strings.HasPrefix(wrr.Warn(), "ignore:")
})
// This warning will be captured
warning.Warnf(ctx, "capture: warning 1")
// This warning will be ignored
warning.Warnf(ctx, "ignore: warning 2")
Transform each written warning.
ctx = warning.Map(ctx, func(wrr warning.Warning) warning.Warning {
return warning.New(strings.ToUpper(wrr.Warn()))
})
// This warning will be transformed to "WARNING 1"
warning.Warnf(ctx, "warning 1")
Combine all written warning into a single warning.
// create a custom warning
type multiWarn struct {
details []string
}
func (w *multiWarn) Warn() string {
return strings.Join(w.details, ", ")
}
// reduce warnings
ctx, flush := warning.Reduce(ctx, func(acc *multiWarn*, wrr warning.Warning) *multiWarn {
if acc == nil {
acc = new(multiWarn)
}
acc.details = append(acc.details, wrr.Warn())
return acc
})
// flush on exit
defer flush()
// Write warnings
warning.Warnf(ctx, "warning 1")
warning.Warnf(ctx, "warning 2")
warning.Warnf(ctx, "warning 3")
// The captured warning will be:
// &multiWarn{"warning 1", "warning 2", "warning 3"}
It does not modify the warning or the context but is useful for side effects like logging.
ctx = warning.Tap(ctx, func(wrr warning.Warning) {
slog.Warn(wrr.Warn())
})
// Now every new warning will be logged using `slog`
warning.Warnf(ctx, "this is a warning")
warning.Warnf(ctx, "this is another warning")
Thank you for your interest in contributing to the warning
Go library! We welcome and appreciate any contributions, whether they be bug reports, feature requests, or code changes.
If you've found a bug, please create an issue describing the problem, including any relevant error messages and a minimal reproduction of the issue.
warning
is licensed under the MIT License.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.