Improved PortError
Removed useless "err" field. Added "causedBy" field to wrap nested error.
This commit is contained in:
19
serial.go
19
serial.go
@@ -74,8 +74,8 @@ const (
|
|||||||
|
|
||||||
// PortError is a platform independent error type for serial ports
|
// PortError is a platform independent error type for serial ports
|
||||||
type PortError struct {
|
type PortError struct {
|
||||||
err string
|
code PortErrorCode
|
||||||
code PortErrorCode
|
causedBy error
|
||||||
}
|
}
|
||||||
|
|
||||||
// PortErrorCode is a code to easily identify the type of error
|
// PortErrorCode is a code to easily identify the type of error
|
||||||
@@ -98,8 +98,8 @@ const (
|
|||||||
ErrorEnumeratingPorts
|
ErrorEnumeratingPorts
|
||||||
)
|
)
|
||||||
|
|
||||||
// Error returns a string explaining the error occurred
|
// EncodedErrorString returns a string explaining the error code
|
||||||
func (e PortError) Error() string {
|
func (e PortError) EncodedErrorString() string {
|
||||||
switch e.code {
|
switch e.code {
|
||||||
case PortBusy:
|
case PortBusy:
|
||||||
return "Serial port busy"
|
return "Serial port busy"
|
||||||
@@ -115,8 +115,17 @@ func (e PortError) Error() string {
|
|||||||
return "Invalid port data bits"
|
return "Invalid port data bits"
|
||||||
case ErrorEnumeratingPorts:
|
case ErrorEnumeratingPorts:
|
||||||
return "Could not enumerate serial ports"
|
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
|
// Code returns an identifier for the kind of error occurred
|
||||||
|
|||||||
Reference in New Issue
Block a user