github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/errorutil/catch.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package errorutil
    12  
    13  import (
    14  	"runtime"
    15  
    16  	"github.com/cockroachdb/errors"
    17  )
    18  
    19  // ShouldCatch is used for catching errors thrown as panics. Its argument is the
    20  // object returned by recover(); it succeeds if the object is an error. If the
    21  // error is a runtime.Error, it is converted to an internal error (see
    22  // errors.AssertionFailedf).
    23  func ShouldCatch(obj interface{}) (ok bool, err error) {
    24  	err, ok = obj.(error)
    25  	if ok {
    26  		if errors.HasInterface(err, (*runtime.Error)(nil)) {
    27  			// Convert runtime errors to internal errors, which display the stack and
    28  			// get reported to Sentry.
    29  			err = errors.HandleAsAssertionFailure(err)
    30  		}
    31  	}
    32  	return ok, err
    33  }