github.com/newrelic/go-agent@v3.26.0+incompatible/errors.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package newrelic
     5  
     6  import "github.com/newrelic/go-agent/internal"
     7  
     8  // StackTracer can be implemented by errors to provide a stack trace when using
     9  // Transaction.NoticeError.
    10  type StackTracer interface {
    11  	StackTrace() []uintptr
    12  }
    13  
    14  // ErrorClasser can be implemented by errors to provide a custom class when
    15  // using Transaction.NoticeError.
    16  type ErrorClasser interface {
    17  	ErrorClass() string
    18  }
    19  
    20  // ErrorAttributer can be implemented by errors to provide extra context when
    21  // using Transaction.NoticeError.
    22  type ErrorAttributer interface {
    23  	ErrorAttributes() map[string]interface{}
    24  }
    25  
    26  // Error is an error that implements ErrorClasser, ErrorAttributer, and
    27  // StackTracer.  Use it with Transaction.NoticeError to directly control error
    28  // message, class, stacktrace, and attributes.
    29  type Error struct {
    30  	// Message is the error message which will be returned by the Error()
    31  	// method.
    32  	Message string
    33  	// Class indicates how the error may be aggregated.
    34  	Class string
    35  	// Attributes are attached to traced errors and error events for
    36  	// additional context.  These attributes are validated just like those
    37  	// added to `Transaction.AddAttribute`.
    38  	Attributes map[string]interface{}
    39  	// Stack is the stack trace.  Assign this field using NewStackTrace,
    40  	// or leave it nil to indicate that Transaction.NoticeError should
    41  	// generate one.
    42  	Stack []uintptr
    43  }
    44  
    45  // NewStackTrace generates a stack trace which can be assigned to the Error
    46  // struct's Stack field or returned by an error that implements the ErrorClasser
    47  // interface.
    48  func NewStackTrace() []uintptr {
    49  	st := internal.GetStackTrace()
    50  	return []uintptr(st)
    51  }
    52  
    53  func (e Error) Error() string { return e.Message }
    54  
    55  // ErrorClass implements the ErrorClasser interface.
    56  func (e Error) ErrorClass() string { return e.Class }
    57  
    58  // ErrorAttributes implements the ErrorAttributes interface.
    59  func (e Error) ErrorAttributes() map[string]interface{} { return e.Attributes }
    60  
    61  // StackTrace implements the StackTracer interface.
    62  func (e Error) StackTrace() []uintptr { return e.Stack }