-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
71 lines (58 loc) · 1.9 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package avsm
import "fmt"
// ErrorCode is the type for package-specific error codes. This is used
// within the Error struct, which allows you to programatically determine
// the error cause.
type ErrorCode uint
func (e ErrorCode) String() string {
switch e {
case ErrorVehicleNotInitialized:
return "VehicleNotInitialized"
case ErrorTransitionNotPermitted:
return "TransitionNotPermitted"
case ErrorRolePermissionDenied:
return "RolePermissionDenied"
case ErrorStateUndefined:
return "StateUndefined"
default:
return "Unknown"
}
}
const (
// ErrorUnknown is the default value
ErrorUnknown ErrorCode = iota
// ErrorVehicleNotInitialized is an error returned when actions are taken on
// a vehicle before it has been initialized.
ErrorVehicleNotInitialized
// ErrorTransitionNotPermitted is the error returned when trying to
// transition to an invalid state.
ErrorTransitionNotPermitted
// ErrorRolePermissionDenied is the error returned when trying to
// transition to a valid state but user dont have valid permission
ErrorRolePermissionDenied
// ErrorStateUndefined is the error returned when the requested state is
// not defined within the machine.
ErrorStateUndefined
)
// Error is the struct representing internal errors.
// This implements the error interface
type Error struct {
message string
code ErrorCode
}
// newErrorStruct uses messge and code to create an *Error struct. The *Error
// struct implements the 'error' interface, so it should be able to be used
// wherever 'error' is expected.
func newErrorStruct(message string, code ErrorCode) *Error {
return &Error{
message: message,
code: code,
}
}
// Message returns the error message.
func (e *Error) Message() string { return e.message }
// Code returns the error code.
func (e *Error) Code() ErrorCode { return e.code }
func (e *Error) Error() string {
return fmt.Sprintf("%s (%d): %s", e.code, e.code, e.message)
}