~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/idmclient/params/error.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package params
 
5
 
 
6
import (
 
7
        "fmt"
 
8
)
 
9
 
 
10
// ErrorCode holds the class of an error in machine-readable format.
 
11
// It is also an error in its own right.
 
12
type ErrorCode string
 
13
 
 
14
func (code ErrorCode) Error() string {
 
15
        return string(code)
 
16
}
 
17
 
 
18
func (code ErrorCode) ErrorCode() ErrorCode {
 
19
        return code
 
20
}
 
21
 
 
22
const (
 
23
        ErrNotFound             ErrorCode = "not found"
 
24
        ErrForbidden            ErrorCode = "forbidden"
 
25
        ErrBadRequest           ErrorCode = "bad request"
 
26
        ErrUnauthorized         ErrorCode = "unauthorized"
 
27
        ErrAlreadyExists        ErrorCode = "already exists"
 
28
        ErrNoAdminCredsProvided ErrorCode = "no admin credentials provided"
 
29
        ErrMethodNotAllowed     ErrorCode = "method not allowed"
 
30
        ErrServiceUnavailable   ErrorCode = "service unavailable"
 
31
)
 
32
 
 
33
// Error represents an error - it is returned for any response that fails.
 
34
type Error struct {
 
35
        Message string    `json:"message,omitempty"`
 
36
        Code    ErrorCode `json:"code,omitempty"`
 
37
}
 
38
 
 
39
// NewError returns a new *Error with the given error code
 
40
// and message.
 
41
func NewError(code ErrorCode, f string, a ...interface{}) error {
 
42
        return &Error{
 
43
                Message: fmt.Sprintf(f, a...),
 
44
                Code:    code,
 
45
        }
 
46
}
 
47
 
 
48
// Error implements error.Error.
 
49
func (e *Error) Error() string {
 
50
        return e.Message
 
51
}
 
52
 
 
53
// ErrorCode holds the class of the error in machine readable format.
 
54
func (e *Error) ErrorCode() string {
 
55
        return e.Code.Error()
 
56
}
 
57
 
 
58
// Cause implements errgo.Causer.Cause.
 
59
func (e *Error) Cause() error {
 
60
        if e.Code != "" {
 
61
                return e.Code
 
62
        }
 
63
        return nil
 
64
}