Skip to content

Return error from NewGTU7 instead of panicking#20

Merged
rustyeddy merged 6 commits intogtu7from
copilot/sub-pr-16-yet-again
Jan 17, 2026
Merged

Return error from NewGTU7 instead of panicking#20
rustyeddy merged 6 commits intogtu7from
copilot/sub-pr-16-yet-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 16, 2026

The NewGTU7 constructor panics when serial initialization fails, violating Go error handling conventions and making the API unsafe.

Changes

  • Changed NewGTU7 signature to return (*GTU7, error)
  • Return error on OpenSerial failure instead of panicking
  • Updated test to handle error return
  • Added test coverage for error case using mock SerialFactory

Example

// Before: panic on failure
gps := NewGTU7(GTU7Config{Serial: cfg})

// After: caller handles error
gps, err := NewGTU7(GTU7Config{Serial: cfg})
if err != nil {
    return fmt.Errorf("failed to initialize GPS: %w", err)
}

Testing

  • TestGTU7_PrefersRMCOverVTG validates success path with Reader injection
  • TestNewGTU7_OpenSerialError validates error propagation when OpenSerial fails
  • ✅ All sensor tests pass

Aligns with error handling pattern used in other constructors like NewADS1115.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 16, 2026 17:40
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Gtu7 sensor and serial drivers implementation based on feedback Return error from NewGTU7 instead of panicking Jan 16, 2026
Copilot AI requested a review from rustyeddy January 16, 2026 17:42
@rustyeddy rustyeddy marked this pull request as ready for review January 16, 2026 17:51
Copilot AI review requested due to automatic review settings January 16, 2026 17:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors the NewGTU7 constructor to follow Go error handling conventions by returning an error instead of panicking when serial initialization fails.

Changes:

  • Modified NewGTU7 to return (*GTU7, error) instead of *GTU7
  • Replaced panic on OpenSerial failure with proper error propagation
  • Updated test to handle the new error return value
  • Cleaned up unused dependencies from go.mod and go.sum

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
sensors/gtu7.go Changed function signature to return error and replaced panic with error return
sensors/gtu7_test.go Updated test to handle error return from NewGTU7
go.mod Removed unused dependencies and moved golang.org/x/sys to direct dependencies
go.sum Removed checksums for unused dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sensors/gtu7_test.go
Co-authored-by: rustyeddy <2903425+rustyeddy@users.noreply.github.com>
@rustyeddy rustyeddy merged commit cadbc9a into gtu7 Jan 17, 2026
@rustyeddy rustyeddy deleted the copilot/sub-pr-16-yet-again branch January 17, 2026 16:27
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.

3 participants