
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
github.com/runbed/warnings
warnings
package provides mechanisms for capturing diagnostics using context.Context
.
It allows to easily capture warnings without modifying existing function signatures.
Warnings 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 "github.com/runbed/warnings"
To start capturing warnings create a new Collector
:
collector := warnings.NewCollector()
defer collector.Close() // close before exiting
Attach this collector to your context:
ctx := warnings.Attach(context.Background(), collector)
Then, you can write warnings to the context:
warnings.Warnf(ctx, "this is a warning")
Finally, capture all of warnings back from the collector:
warns, err := warnings.ReadAll(collector)
This example demonstrates how to use the Filter
function to filter warnings:
// filter warnings
ctx = warnings.Filter(ctx, func(warn warnings.Warning) bool {
return !strings.HasPrefix(warn.Warn(), "ignore:")
})
// This warning will be captured
warnings.Warnf(ctx, "capture: warning 1")
// This warning will be ignored
warnings.Warnf(ctx, "ignore: warning 2")
Transform each written warning.
ctx = warnings.Map(ctx, func(warn warnings.Warning) warnings.Warning {
return warnings.New(strings.ToUpper(warn.Warn()))
})
// This warning will be transformed to "WARNING 1"
warnings.Warnf(ctx, "warning 1")
Combine all written warnings 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 := warnings.Reduce(ctx, func(acc *multiWarn*, warn warnings.Warning) *multiWarn {
if acc == nil {
acc = new(multiWarn)
}
acc.details = append(acc.details, warn.Warn())
})
// flush on exit
defer flush()
// Write warnings
warnings.Warnf(ctx, "warning 1")
warnings.Warnf(ctx, "warning 2")
warnings.Warnf(ctx, "warning 3")
// The captured warning will be:
// &multiWarn{"warning 1", "warning 2", "warning 3"}
It does not modify the warnings or the context but is useful for side effects like logging.
ctx = warnings.Tap(ctx, func(warn warnings.Warning) {
slog.Warn(warn.Warn())
})
// Now every new warning will be logged using `slog`
warnings.Warnf(ctx, "this is a warning")
warnings.Warnf(ctx, "this is another warning")
Thank you for your interest in contributing to the warnings
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 in the GitHub repository describing the problem, including any relevant error messages and a minimal reproduction of the issue.
warnings
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
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.