github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/errmon/belt.go (about) 1 // Copyright 2022 Meta Platforms, Inc. and affiliates. 2 // 3 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 // 5 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 // 7 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 // 9 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 // 11 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 13 package errmon 14 15 import ( 16 "github.com/facebookincubator/go-belt" 17 loggerimpl "github.com/facebookincubator/go-belt/tool/experimental/errmon/implementation/logger" 18 "github.com/facebookincubator/go-belt/tool/logger" 19 ) 20 21 // Default is an overridable function which returns the default ErrorMonitor. 22 // 23 // This function is called to get an ErrorMonitor, when functions FromBelt and FromCtx 24 // are used on a scope, where an ErrorMonitor is not set. 25 var Default = func(belt *belt.Belt) ErrorMonitor { 26 loggerIface := logger.FromBelt(belt) 27 if loggerIface != nil { 28 return loggerimpl.New(loggerIface) 29 } 30 31 return loggerimpl.Default() 32 } 33 34 // FromBelt returns an ErrorMonitor from a given Belt if one is set; 35 // and returns the default one, if not. 36 func FromBelt(belt *belt.Belt) ErrorMonitor { 37 errMonIface := belt.Tools().GetByID(ToolID) 38 if errMonIface == nil { 39 return Default(belt) 40 } 41 42 return errMonIface.(ErrorMonitor) 43 } 44 45 // BeltWithErrorMonitor returns an Belt with the ErrorMonitor set. 46 func BeltWithErrorMonitor(belt *belt.Belt, errMon ErrorMonitor) *belt.Belt { 47 return belt.WithTool(ToolID, errMon) 48 } 49 50 // ObserveErrorBelt calls ObserveError method of an ErrorMonitor in the given Belt. 51 // 52 // If one at any moment in the code has an error they want to report about if it is not nil, 53 // they can just use this function. 54 // 55 // For example: 56 // 57 // _, err := writer.Write(b) 58 // errmon.ObserveErrorBelt(belt, err) 59 func ObserveErrorBelt(belt *belt.Belt, err error) *Event { 60 return FromBelt(belt).ObserveError(belt, err) 61 } 62 63 // ObserveRecoverBelt calls ObserveRecover method of an ErrorMonitor in the given context. 64 // 65 // If one at any moment in the code has a potential panic they want to report about, 66 // they can just use this function. 67 // 68 // For example one may add this to a beginning of a function: 69 // 70 // defer func() { errmon.ObserveRecoverBelt(belt, recover()) }() 71 // 72 // See also: https://go.dev/ref/spec#Handling_panics 73 func ObserveRecoverBelt(belt *belt.Belt, recoverResult any) *Event { 74 return FromBelt(belt).ObserveRecover(belt, recoverResult) 75 }