From fe0dfe76d3463c2e7ff0050db7995659bf82454e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 12 Oct 2016 16:17:33 +0200 Subject: [PATCH] Improved PortError Removed useless "err" field. Added "causedBy" field to wrap nested error. --- serial.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/serial.go b/serial.go index 612e447..6fb2ef2 100644 --- a/serial.go +++ b/serial.go @@ -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