~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/state/api/params/apierror.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package params
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        "launchpad.net/juju-core/rpc"
 
10
)
 
11
 
 
12
// Error is the type of error returned by any call to the state API
 
13
type Error struct {
 
14
        Message string
 
15
        Code    string
 
16
}
 
17
 
 
18
func (e *Error) Error() string {
 
19
        return e.Message
 
20
}
 
21
 
 
22
func (e *Error) ErrorCode() string {
 
23
        return e.Code
 
24
}
 
25
 
 
26
var _ rpc.ErrorCoder = (*Error)(nil)
 
27
 
 
28
// GoString implements fmt.GoStringer.  It means that a *Error shows its
 
29
// contents correctly when printed with %#v.
 
30
func (e Error) GoString() string {
 
31
        return fmt.Sprintf("&params.Error{%q, %q}", e.Code, e.Message)
 
32
}
 
33
 
 
34
// The Code constants hold error codes for some kinds of error.
 
35
const (
 
36
        CodeNotFound            = "not found"
 
37
        CodeUnauthorized        = "unauthorized access"
 
38
        CodeCannotEnterScope    = "cannot enter scope"
 
39
        CodeCannotEnterScopeYet = "cannot enter scope yet"
 
40
        CodeExcessiveContention = "excessive contention"
 
41
        CodeUnitHasSubordinates = "unit has subordinates"
 
42
        CodeNotAssigned         = "not assigned"
 
43
        CodeStopped             = "stopped"
 
44
        CodeHasAssignedUnits    = "machine has assigned units"
 
45
)
 
46
 
 
47
// ErrCode returns the error code associated with
 
48
// the given error, or the empty string if there
 
49
// is none.
 
50
func ErrCode(err error) string {
 
51
        if err, _ := err.(rpc.ErrorCoder); err != nil {
 
52
                return err.ErrorCode()
 
53
        }
 
54
        return ""
 
55
}
 
56
 
 
57
// clientError maps errors returned from an RPC call into local errors with
 
58
// appropriate values.
 
59
func ClientError(err error) error {
 
60
        rerr, ok := err.(*rpc.RequestError)
 
61
        if !ok {
 
62
                return err
 
63
        }
 
64
        // We use our own error type rather than rpc.ServerError
 
65
        // because we don't want the code or the "server error" prefix
 
66
        // within the error message. Also, it's best not to make clients
 
67
        // know that we're using the rpc package.
 
68
        return &Error{
 
69
                Message: rerr.Message,
 
70
                Code:    rerr.Code,
 
71
        }
 
72
}