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