Skip to content

Commit

Permalink
chore: better documentation for ToExceptionCode
Browse files Browse the repository at this point in the history
  • Loading branch information
xiegeo committed Sep 10, 2022
1 parent d27ed9a commit e8e5cd3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ Development tools:

2022-09-09 v1.0.0

V1 released with the following depreciated identifiers removed:
V1 release has the following depreciated identifiers removed:

- The `Server` interface is removed, use `ServerCloser` instead.
- The `Server` interface is removed. Use `ServerCloser` instead.
- Public global variable `DebugOut` is changed to private. Use `SetDebugOut(w io.Writer)` instead for thread safety.
- Type alias `type ProtocalHandler = ProtocolHandler` removed.
- Function redirect from `GetRTUBidirectionSizeFromHeader` to `GetRTUBidirectionalSizeFromHeader` removed.
Expand Down
13 changes: 9 additions & 4 deletions modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,20 @@ func (e ExceptionCode) Error() string {
return fmt.Sprintf("ExceptionCode:0x%02X", byte(e))
}

// ToExceptionCode turns an error into an ExceptionCode (to send in PDU), best
// ToExceptionCode turns an error into an ExceptionCode (to send in PDU). Best
// effort with EcServerDeviceFailure as fail back.
//
// - If the error is a ExceptionCode or warped ExceptionCode,
// the original ExceptionCode is returned.
// - IF the error is ErrFcNotSupported or warped ErrFcNotSupported,
// EcIllegalFunction is returned.
// - For all other cases, EcServerDeviceFailure is returned.
func ToExceptionCode(err error) ExceptionCode {
if err == nil {
debugf("ToExceptionCode: unexpected covert nil error to ExceptionCode")
return EcServerDeviceFailure
}
var e ExceptionCode
ok := errors.As(err, &e)
if ok {
if e := ExceptionCode(0); errors.As(err, &e) {
return e
}
if errors.Is(err, ErrFcNotSupported) {
Expand Down

0 comments on commit e8e5cd3

Please sign in to comment.