From 21d4686f4fdd519dc87d5b7dcd8560f86faaa0bc Mon Sep 17 00:00:00 2001 From: Koenraad Verheyden Date: Wed, 4 Aug 2021 00:15:54 +0200 Subject: [PATCH] Add ErrorHandlerFunc to simplify creating ErrorHandlers (#2149) * Add ErrorHandlerFunc to simplify creating ErrorHandlers * Add CHANGELOG entry * Apply suggestions from code review Co-authored-by: Tyler Yahn * Add interface assertion Co-authored-by: Tyler Yahn --- CHANGELOG.md | 2 ++ error_handler.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40bb62af2..c8a0e8fd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added +- Added `ErrorHandlerFunc` to use a function as an `"go.opentelemetry.io/otel".ErrorHandler`. (#2149) + ### Changed ### Deprecated diff --git a/error_handler.go b/error_handler.go index a51b2dee4..72fad8541 100644 --- a/error_handler.go +++ b/error_handler.go @@ -25,3 +25,14 @@ type ErrorHandler interface { // DO NOT CHANGE: any modification will not be backwards compatible and // must never be done outside of a new major release. } + +// ErrorHandlerFunc is a convenience adapter to allow the use of a function +// as an ErrorHandler. +type ErrorHandlerFunc func(error) + +var _ ErrorHandler = ErrorHandlerFunc(nil) + +// Handle handles the irremediable error by calling the ErrorHandlerFunc itself. +func (f ErrorHandlerFunc) Handle(err error) { + f(err) +}