Improved PortError

Removed useless "err" field.
Added "causedBy" field to wrap nested error.
This commit is contained in:
Cristian Maglie
2016-10-12 16:17:33 +02:00
parent 01692a6f8d
commit fe0dfe76d3

View File

@@ -74,8 +74,8 @@ const (
// PortError is a platform independent error type for serial ports
type PortError struct {
err string
code PortErrorCode
code PortErrorCode
causedBy error
}
// PortErrorCode is a code to easily identify the type of error
@@ -98,8 +98,8 @@ const (
ErrorEnumeratingPorts
)
// Error returns a string explaining the error occurred
func (e PortError) Error() string {
// EncodedErrorString returns a string explaining the error code
func (e PortError) EncodedErrorString() string {
switch e.code {
case PortBusy:
return "Serial port busy"
@@ -115,8 +115,17 @@ func (e PortError) Error() string {
return "Invalid port data bits"
case ErrorEnumeratingPorts:
return "Could not enumerate serial ports"
default:
return "Other error"
}
return e.err
}
// Error returns the complete error code with details on the cause of the error
func (e PortError) Error() string {
if e.causedBy != nil {
return e.EncodedErrorString() + ": " + e.causedBy.Error()
}
return e.EncodedErrorString()
}
// Code returns an identifier for the kind of error occurred