123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package errors
- import (
- "fmt"
- "strings"
- )
- // ==================== juju adaptor start ========================
- // Trace just calls AddStack.
- func Trace(err error) error {
- if err == nil {
- return nil
- }
- return AddStack(err)
- }
- // Annotate adds a message and ensures there is a stack trace.
- func Annotate(err error, message string) error {
- if err == nil {
- return nil
- }
- hasStack := HasStack(err)
- err = &withMessage{
- cause: err,
- msg: message,
- causeHasStack: hasStack,
- }
- if hasStack {
- return err
- }
- return &withStack{
- err,
- callers(),
- }
- }
- // Annotatef adds a message and ensures there is a stack trace.
- func Annotatef(err error, format string, args ...interface{}) error {
- if err == nil {
- return nil
- }
- hasStack := HasStack(err)
- err = &withMessage{
- cause: err,
- msg: fmt.Sprintf(format, args...),
- causeHasStack: hasStack,
- }
- if hasStack {
- return err
- }
- return &withStack{
- err,
- callers(),
- }
- }
- var emptyStack stack
- // NewNoStackError creates error without error stack
- // later duplicate trace will no longer generate Stack too.
- func NewNoStackError(msg string) error {
- return &fundamental{
- msg: msg,
- stack: &emptyStack,
- }
- }
- // SuspendStack suspends stack generate for error.
- func SuspendStack(err error) error {
- if err == nil {
- return err
- }
- cleared := clearStack(err)
- if cleared {
- return err
- }
- return &withStack{
- err,
- &emptyStack,
- }
- }
- func clearStack(err error) (cleared bool) {
- switch typedErr := err.(type) {
- case *withMessage:
- return clearStack(typedErr.Cause())
- case *fundamental:
- typedErr.stack = &emptyStack
- return true
- case *withStack:
- typedErr.stack = &emptyStack
- clearStack(typedErr.Cause())
- return true
- default:
- return false
- }
- }
- // ErrorStack will format a stack trace if it is available, otherwise it will be Error()
- // If the error is nil, the empty string is returned
- // Note that this just calls fmt.Sprintf("%+v", err)
- func ErrorStack(err error) string {
- if err == nil {
- return ""
- }
- return fmt.Sprintf("%+v", err)
- }
- // IsNotFound reports whether err was not found error.
- func IsNotFound(err error) bool {
- return strings.Contains(err.Error(), "not found")
- }
- // NotFoundf represents an error with not found message.
- func NotFoundf(format string, args ...interface{}) error {
- return Errorf(format+" not found", args...)
- }
- // BadRequestf represents an error with bad request message.
- func BadRequestf(format string, args ...interface{}) error {
- return Errorf(format+" bad request", args...)
- }
- // NotSupportedf represents an error with not supported message.
- func NotSupportedf(format string, args ...interface{}) error {
- return Errorf(format+" not supported", args...)
- }
- // NotValidf represents an error with not valid message.
- func NotValidf(format string, args ...interface{}) error {
- return Errorf(format+" not valid", args...)
- }
- // IsAlreadyExists reports whether err was already exists error.
- func IsAlreadyExists(err error) bool {
- return strings.Contains(err.Error(), "already exists")
- }
- // AlreadyExistsf represents an error with already exists message.
- func AlreadyExistsf(format string, args ...interface{}) error {
- return Errorf(format+" already exists", args...)
- }
- // ==================== juju adaptor end ========================
|