Select Git revision
-
Jan Semmelink authoredJan Semmelink authored
error.go 5.80 KiB
package errors
import (
"fmt"
"path"
"strconv"
"strings"
)
//CustomError implements the following interfaces:
// error
// github.com/pkg/errors: Cause
type CustomError struct {
code int
message string
caller Caller
cause error
}
//implement interface error:
func (err CustomError) Error() string {
return err.Formatted(FormattingOptions{Causes: false})
}
func Is(e1, e2 error) bool {
if e1WithIs, ok := e1.(ErrorWithIs); ok {
return e1WithIs.Is(e2)
}
return e1.Error() == e2.Error()
}
//Is() compares the message string of this or any cause to match the specified error message
func (err CustomError) Is(specificError error) bool {
if err.message == specificError.Error() {
return true
}
if err.cause != nil {
if causeWithIs, ok := err.cause.(ErrorWithIs); ok {
return causeWithIs.Is(specificError)
}
}
return false
}
//implement github.com/pkg/errors: Cause
func (err CustomError) Cause() error {
return err.cause
}
func HTTPCode(err error) int {
if errWithCode, ok := err.(ErrorWithCause); ok {
return errWithCode.Code()
}
return 0
}
func (err CustomError) Code() int {
//find http error code - returning the smallest code in the stack of causes (excluding code==0)
code := err.code
if err.cause != nil {
if causeWithCode, ok := err.cause.(ErrorWithCause); ok {
causeCode := causeWithCode.Code()
if code == 0 || (causeCode != 0 && causeCode < code) {
code = causeCode
}
}
}
return code
}