-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from storageos/feature/DEV-1911-custom-errors-…
…package Add custom errors package
- Loading branch information
Showing
9 changed files
with
186 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package serror | ||
|
||
//go:generate stringer -type=StorageOSErrorKind error_kind.go | ||
type StorageOSErrorKind int | ||
|
||
// Known error kinds | ||
const ( | ||
UnknownError StorageOSErrorKind = iota | ||
APIUncontactable | ||
InvalidHostConfig | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package serror | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var kindLookupMap map[string]StorageOSErrorKind | ||
|
||
func init() { | ||
kindLookupMap = make(map[string]StorageOSErrorKind) | ||
|
||
// Populate the lookup map with all the known constants | ||
for i := StorageOSErrorKind(0); !strings.HasPrefix(i.String(), "StorageOSErrorKind("); i++ { | ||
kindLookupMap[i.String()] = i | ||
} | ||
} | ||
|
||
func (s *StorageOSErrorKind) UnmarshalJSON(b []byte) error { | ||
str := "" | ||
if err := json.Unmarshal(b, &str); err != nil { | ||
return err | ||
} | ||
|
||
v, ok := kindLookupMap[str] | ||
if !ok { | ||
return fmt.Errorf("Failed to unmarshal ErrorKind %s", s) | ||
} | ||
|
||
*s = v | ||
return nil | ||
} | ||
|
||
func (s *StorageOSErrorKind) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(s.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package serror | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type StorageOSError interface { | ||
// embedding error provides compatibility with standard error handling code | ||
error | ||
|
||
// Encoding/decoding methods to help errors traverse API boundaries | ||
json.Marshaler | ||
json.Unmarshaler | ||
|
||
Err() error // Returns the underlying error that caused this event | ||
String() string // A short string representing the error (for logging etc) | ||
Help() string // A larger string that should provide informative debug instruction to users | ||
Kind() StorageOSErrorKind // A type representing a set of known error conditions, helpful to switch on | ||
Extra() map[string]string // A container for error specific information | ||
|
||
// TODO: should we include callstack traces here? We could have a debug mode for it. | ||
} | ||
|
||
func ErrorKind(err error) StorageOSErrorKind { | ||
if serr, ok := err.(StorageOSError); ok { | ||
return serr.Kind() | ||
} | ||
return UnknownError | ||
} | ||
|
||
func IsStorageOSError(err error) bool { | ||
_, ok := err.(StorageOSError) | ||
return ok | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package serror | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
func NewTypedStorageOSError(kind StorageOSErrorKind, err error, msg string, help string) StorageOSError { | ||
return &typedStorageOSError{ | ||
internal: &internal_TypedStorageOSError{ | ||
ErrorKind: &kind, | ||
Cause: err, | ||
ErrMessage: msg, | ||
HelpMessage: help, | ||
}, | ||
} | ||
} | ||
|
||
func NewUntypedStorageOSError(err error, msg string, help string) StorageOSError { | ||
var kind StorageOSErrorKind = UnknownError | ||
|
||
return &typedStorageOSError{ | ||
internal: &internal_TypedStorageOSError{ | ||
ErrorKind: &kind, | ||
Cause: err, | ||
ErrMessage: msg, | ||
HelpMessage: help, | ||
}, | ||
} | ||
} | ||
|
||
type internal_TypedStorageOSError struct { | ||
ErrorKind *StorageOSErrorKind `json:"error_kind"` | ||
Cause error `json:"caused_by"` | ||
ErrMessage string `json:"error_message"` | ||
HelpMessage string `json:"help_message"` | ||
ExtraMap map[string]string `json:"extra"` | ||
} | ||
|
||
type typedStorageOSError struct { | ||
internal *internal_TypedStorageOSError | ||
} | ||
|
||
func (t *typedStorageOSError) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(t.internal) | ||
} | ||
|
||
func (t *typedStorageOSError) UnmarshalJSON(d []byte) error { | ||
internal := &internal_TypedStorageOSError{} | ||
|
||
err := json.Unmarshal(d, internal) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.internal = internal | ||
return nil | ||
} | ||
|
||
func (t *typedStorageOSError) Error() string { return t.String() } | ||
func (t *typedStorageOSError) Err() error { return t.internal.Cause } | ||
func (t *typedStorageOSError) String() string { return t.internal.ErrMessage } | ||
func (t *typedStorageOSError) Help() string { return t.internal.HelpMessage } | ||
func (t *typedStorageOSError) Kind() StorageOSErrorKind { return *t.internal.ErrorKind } | ||
func (t *typedStorageOSError) Extra() map[string]string { return t.internal.ExtraMap } |