launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/errors/errors.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package errors
     5  
     6  import (
     7  	"fmt"
     8  	"launchpad.net/errgo/errors"
     9  )
    10  
    11  // errorWrapper defines a way to encapsulate an error inside another error.
    12  type errorWrapper struct {
    13  	// Err is the underlying error.
    14  	Err error
    15  	Msg string
    16  }
    17  
    18  // Error implements the error interface.
    19  func (e *errorWrapper) Error() string {
    20  	if e.Msg != "" || e.Err == nil {
    21  		if e.Err != nil {
    22  			return fmt.Sprintf("%s: %v", e.Msg, e.Err.Error())
    23  		}
    24  		return e.Msg
    25  	}
    26  	return e.Err.Error()
    27  }
    28  
    29  // notFoundError records an error when something has not been found.
    30  type notFoundError struct {
    31  	*errorWrapper
    32  }
    33  
    34  // IsNotFoundError is satisfied by errors created by this package representing
    35  // resources that can't be found.
    36  func IsNotFoundError(err error) bool {
    37  	_, ok := errors.Cause(err).(notFoundError)
    38  	return ok
    39  }
    40  
    41  // NotFoundf returns a error which satisfies IsNotFoundError().
    42  // The message for the error is made up from the given
    43  // arguments formatted as with fmt.Sprintf, with the
    44  // string " not found" appended.
    45  func NotFoundf(format string, args ...interface{}) error {
    46  	return notFoundError{
    47  		&errorWrapper{
    48  			Msg: fmt.Sprintf(format+" not found", args...),
    49  		},
    50  	}
    51  }
    52  
    53  // NewNotFoundError returns a new error wrapping err that satisfies
    54  // IsNotFoundError().
    55  func NewNotFoundError(err error, msg string) error {
    56  	return notFoundError{&errorWrapper{Err: err, Msg: msg}}
    57  }
    58  
    59  // unauthorizedError represents the error that an operation is unauthorized.
    60  // Use IsUnauthorized() to determine if the error was related to authorization
    61  // failure.
    62  type unauthorizedError struct {
    63  	*errorWrapper
    64  }
    65  
    66  // IsUnauthorizedError is satisfied by errors created by this package
    67  // representing authorization failures.
    68  func IsUnauthorizedError(err error) bool {
    69  	_, ok := errors.Cause(err).(unauthorizedError)
    70  	return ok
    71  }
    72  
    73  // Unauthorizedf returns an error which satisfies IsUnauthorizedError().
    74  func Unauthorizedf(format string, args ...interface{}) error {
    75  	return unauthorizedError{
    76  		&errorWrapper{
    77  			Msg: fmt.Sprintf(format, args...),
    78  		},
    79  	}
    80  }
    81  
    82  // NewUnauthorizedError returns an error which wraps err and satisfies
    83  // IsUnauthorized().
    84  func NewUnauthorizedError(err error, msg string) error {
    85  	return unauthorizedError{&errorWrapper{Err: err, Msg: msg}}
    86  }
    87  
    88  type notImplementedError struct {
    89  	what string
    90  }
    91  
    92  // NewNotImplementedError returns an error signifying that
    93  // something is not implemented.
    94  func NewNotImplementedError(what string) error {
    95  	return &notImplementedError{what: what}
    96  }
    97  
    98  func (e *notImplementedError) Error() string {
    99  	return e.what + " not implemented"
   100  }
   101  
   102  // IsNotImplementedError reports whether the error
   103  // was created with NewNotImplementedError.
   104  func IsNotImplementedError(err error) bool {
   105  	_, ok := errors.Cause(err).(*notImplementedError)
   106  	return ok
   107  }