Skip to content

Conversation

@jonalfarlinga
Copy link

@jonalfarlinga jonalfarlinga commented Oct 9, 2024

Improve Error Handling

This pull request adds error wrapping at each level along with appropriate state dumps for better error tracking on parsing failures. Crucially, adding state dumps for specific debugging issues becomes easier, since all we need to do is modify the error string.

Examples

Current: When receiving an IAm message with a VendorID below 256, the whois-client script ends with an ErrTooShortToMarshalBinary.

Updated:

❯ .\examples.exe whois --broadcast-address ":47808" --remote-address "192.168.1.100:47808"
whois-client.go:66: sent: 810b000801001008
whois-client.go:81: read 20 bytes from 192.168.1.100:47808: 810a001401001000c4020000652205c491002107
whois-client.go:85: error parsing the received message: Parsing BACnet 1000c4020000652205c491002107: failed to unmarshal UnconfirmedIAm - marshal length 21 binary length 20: too short to decode as parameter

The script adds an error trace at each layer. The unmarshal function declares that it is an UnconfirmedIAm, and that the binary length doesn't match the marshal length (since all VendorID's are currently encoded as 16bit).

Current: When receiving a CACK with a string data object, the rp-client ends with an ErrWongObjectCount

Updated:

❯ .\examples.exe rpc --remote-address "192.168.1.100:47808" --instance-id 101 --object-type 8 --property-id 77
rp-client.go:66: sent: 810a001101040005010c0c02000065194d
rp-client.go:73: read 36 bytes from 192.168.1.100:47808: 810a0024010030010c0c02000065194d3e751000554f2d416e616c7973742d546573743f
rp-client.go:85: unmarshalled BVLC: &plumbing.BVLC{Type:0x81, Function:0xa, Length:0x24}
rp-client.go:86: unmarshalled NPDU: &plumbing.NPDU{Version:0x1, Control:0x0, DNET:0x0, DLEN:0x0, Hop:0x0}
rp-client.go:90: couldn't decode the CACK reply: failed to decode CACK - objects count: 8: wrong object count

The script adds an error trace at each layer. The decode function outputs the number of objects detected as context, so we cna see that the parser is detecting too many objects.

References

Uses the github.com/pkg/errors library to wrap errors with new context at each layer.

Resolves #2

@jonalfarlinga jonalfarlinga marked this pull request as ready for review October 9, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve Error handling

1 participant