Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JUJU-759] Change error types to use new ConstError #54

Merged
merged 5 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package errors
import (
"fmt"
"reflect"
"runtime"
)

// Err holds a description of an error along with information about
Expand All @@ -33,6 +32,43 @@ type Err struct {
line int
}

// Locationer is an interface that represents a certain class of errors that
// contain the location information from where they were raised.
type Locationer interface {
barrettj12 marked this conversation as resolved.
Show resolved Hide resolved
// Location returns the path-qualified function name where the error was
// created and the line number
Location() (function string, line int)
}

// locationError is the internal implementation of the Locationer interface.
type locationError struct {
error

// function is the package path-qualified function name where the
// error was created.
function string

// line is the line number the error was created on inside of function
line int
}

// newLocationError constructs a new Locationer error from the supplied error
// with the location set to callDepth in the stack.
func newLocationError(err error, callDepth int) *locationError {
le := &locationError{error: err}
le.function, le.line = getLocation(callDepth + 1)
return le
}

// *locationError implements Locationer.Location interface
func (l *locationError) Location() (string, int) {
return l.function, l.line
}

func (l *locationError) Unwrap() error {
return l.error
}

// NewErr is used to return an Err for the purpose of embedding in other
// structures. The location is not specified, and needs to be set with a call
// to SetLocation.
Expand Down Expand Up @@ -160,14 +196,7 @@ func (unformatter) Format() { /* break the fmt.Formatter interface */ }
// SetLocation records the package path-qualified function name of the error at
// callDepth stack frames above the call.
func (e *Err) SetLocation(callDepth int) {
rpc := make([]uintptr, 1)
n := runtime.Callers(callDepth+2, rpc[:])
if n < 1 {
return
}
frame, _ := runtime.CallersFrames(rpc).Next()
e.function = frame.Function
e.line = frame.Line
e.function, e.line = getLocation(callDepth + 1)
}

// StackTrace returns one string for each location recorded in the stack of
Expand Down
Loading