Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

Error messages currently show only numeric errno codes (e.g., "Error 5 performing I2C data transfer"), requiring developers to look up error meanings. This change uses Marshal.GetLastPInvokeErrorMessage() (.NET 7+) to include descriptive messages.

Changes

  • ExceptionHelper: Added GetLastErrorMessage() method that captures both error code and message, formats as "code (message)"
  • Error handling: Updated 22 error sites across GPIO, I2C, and SPI drivers to use centralized helper
  • Code quality: Eliminated duplication, net -30 lines
  • Code style: Fixed SA1028 errors by removing trailing whitespace across System.Device.Gpio

Example

Before:

throw new IOException($"Error {Marshal.GetLastWin32Error()} performing I2C data transfer.");
// Output: "Error 5 performing I2C data transfer."

After:

throw new IOException($"Error {ExceptionHelper.GetLastErrorMessage()} performing I2C data transfer.");
// Output: "Error 5 (Input/output error) performing I2C data transfer."

Notes

Three locations manually format errors where error code must be checked before throwing (EINTR/ENOENT comparisons). These are documented with inline comments explaining why GetLastWin32Error() cannot be called twice.

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<issue_title>Should we add a mapping to Marshal.GetLastWin32Error()</issue_title>
<issue_description>Our current exception handling is very obscure and difficult to understand, just like #668 . Why don't we map errors.h accordingly?</issue_description>

Comments on the Issue (you are @copilot in this section)

@krwq @ZhangGaoxing might be worth checking corefx code first to see if they already have this defined somewhere.

Possibly something using this https://github.com/dotnet/corefx/blob/ce4ceb09e0bd21675a3523edebfaf73a16714d15/src/Native/Unix/System.Native/pal_errno.c#L20</comment_new>
<comment_new>@joperezr
This makes sense and I agree it would be useful.</comment_new>
<comment_new>@krwq
[Triage] This might just work with .NET 8 but we need verification</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Microsoft Reviewers: Open in CodeFlow

@dotnet-policy-service dotnet-policy-service bot added area-System.Device.Gpio Contains types for using general-purpose I/O (GPIO) pins area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio labels Oct 30, 2025
Copilot AI changed the title [WIP] Add mapping to Marshal.GetLastWin32Error() for better error handling Map errno values to human-readable messages in System.Device.Gpio error handling Oct 30, 2025
Copilot AI requested a review from krwq October 30, 2025 17:31
{
int errorCode = Marshal.GetLastWin32Error();
string errorMessage = Marshal.GetLastPInvokeErrorMessage();

Copy link
Member

Choose a reason for hiding this comment

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

@copilot please fix error SA1028: (NETCORE_ENGINEERING_TELEMETRY=Build) Code should not contain trailing whitespace with some tooling (you can run it on everything in this repo)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 0549198 - ran dotnet format whitespace across System.Device.Gpio and manually fixed remaining trailing whitespace in comments.

@krwq
Copy link
Member

krwq commented Dec 18, 2025

@copilot I resolved conflicts and I probably messed up the PR, please double check all changes are here

Copy link
Contributor Author

Copilot AI commented Dec 18, 2025

@copilot I resolved conflicts and I probably messed up the PR, please double check all changes are here

I've verified all changes are present after the merge. ✅

Core changes:

  • ExceptionHelper.cs: Added GetLastErrorMessage() method and GetIOException() overload
  • Updated 12 files with errno-to-message mapping (22+ error sites)
  • Special case handling in RaspberryPi3LinuxDriver, SysFsDriver, and LibGpiodDriverEventHandler with explanatory comments

Files with changes:

  1. ExceptionHelper.cs - Core helper methods
  2. LineHandle.cs, LibGpiodDriver.cs, LibGpiodDriverEventHandler.cs - GPIO error handling
  3. UnixI2cBus.cs, UnixI2cFileTransferBus.cs - I2C error handling
  4. UnixSpiDevice.cs - SPI error handling
  5. RaspberryPi3LinuxDriver.cs, SysFsDriver.cs - Special cases with ENOENT/EINTR checks
  6. LineRequest.cs, LibGpiodV2Driver.cs, UnixDriver.cs - Formatting improvements

All Marshal.GetLastWin32Error() calls now use ExceptionHelper.GetLastErrorMessage() to provide human-readable messages.

Copilot AI and others added 4 commits December 20, 2025 12:51
…essage()

Co-authored-by: krwq <660048+krwq@users.noreply.github.com>
… code duplication

Co-authored-by: krwq <660048+krwq@users.noreply.github.com>
… helper

Co-authored-by: krwq <660048+krwq@users.noreply.github.com>
Co-authored-by: krwq <660048+krwq@users.noreply.github.com>
@pgrawehr pgrawehr force-pushed the copilot/add-mapping-to-getlastwin32error branch from 3358088 to 5141547 Compare December 20, 2025 12:54
@pgrawehr pgrawehr marked this pull request as ready for review December 20, 2025 12:56
@pgrawehr
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@joperezr joperezr merged commit 9dd5367 into main Jan 8, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio area-System.Device.Gpio Contains types for using general-purpose I/O (GPIO) pins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should we add a mapping to Marshal.GetLastWin32Error()

4 participants