Skip to content

Commit e8e5cd3

Browse files
committed
chore: better documentation for ToExceptionCode
1 parent d27ed9a commit e8e5cd3

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ Development tools:
243243

244244
2022-09-09 v1.0.0
245245

246-
V1 released with the following depreciated identifiers removed:
246+
V1 release has the following depreciated identifiers removed:
247247

248-
- The `Server` interface is removed, use `ServerCloser` instead.
248+
- The `Server` interface is removed. Use `ServerCloser` instead.
249249
- Public global variable `DebugOut` is changed to private. Use `SetDebugOut(w io.Writer)` instead for thread safety.
250250
- Type alias `type ProtocalHandler = ProtocolHandler` removed.
251251
- Function redirect from `GetRTUBidirectionSizeFromHeader` to `GetRTUBidirectionalSizeFromHeader` removed.

modbus.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,20 @@ func (e ExceptionCode) Error() string {
241241
return fmt.Sprintf("ExceptionCode:0x%02X", byte(e))
242242
}
243243

244-
// ToExceptionCode turns an error into an ExceptionCode (to send in PDU), best
244+
// ToExceptionCode turns an error into an ExceptionCode (to send in PDU). Best
245245
// effort with EcServerDeviceFailure as fail back.
246+
//
247+
// - If the error is a ExceptionCode or warped ExceptionCode,
248+
// the original ExceptionCode is returned.
249+
// - IF the error is ErrFcNotSupported or warped ErrFcNotSupported,
250+
// EcIllegalFunction is returned.
251+
// - For all other cases, EcServerDeviceFailure is returned.
246252
func ToExceptionCode(err error) ExceptionCode {
247253
if err == nil {
248254
debugf("ToExceptionCode: unexpected covert nil error to ExceptionCode")
255+
return EcServerDeviceFailure
249256
}
250-
var e ExceptionCode
251-
ok := errors.As(err, &e)
252-
if ok {
257+
if e := ExceptionCode(0); errors.As(err, &e) {
253258
return e
254259
}
255260
if errors.Is(err, ErrFcNotSupported) {

0 commit comments

Comments
 (0)