Skip to content

fix: add error handling for WiFi.scanNetworks() in /scan endpoint#9

Open
avineet4 wants to merge 1 commit intolocalrice:mainfrom
avineet4:fix/wifi-scan-error-handling
Open

fix: add error handling for WiFi.scanNetworks() in /scan endpoint#9
avineet4 wants to merge 1 commit intolocalrice:mainfrom
avineet4:fix/wifi-scan-error-handling

Conversation

@avineet4
Copy link

@avineet4 avineet4 commented Oct 18, 2025

Description

This PR fixes missing error handling in the /scan endpoint when WiFi.scanNetworks() fails.

Problem

  • WiFi.scanNetworks() can return negative values on error (e.g., -1 for WIFI_SCAN_FAILED, -2 for WIFI_SCAN_RUNNING)
  • The code doesn't check for negative values before iterating through results
  • When scan fails, client receives empty array [] with 200 OK status, masking the error
  • No memory cleanup (WiFi.scanDelete()) is performed
  • Serial output doesn't indicate scan failure, making debugging difficult

Solution

  • Added error checking for networks < 0
  • Return 500 error with JSON error response when scan fails
  • Added WiFi.scanDelete() to prevent memory leak
  • Improved serial output to indicate scan success/failure
  • Proper error reporting to client instead of masking errors

Changes Made

  • Modified /scan endpoint in src/EasyWiFi.cpp (lines 300-326)
  • Added error handling with appropriate HTTP status codes
  • Added memory cleanup for scan results
  • Improved logging for better debugging

Testing

  • ✅ Code compiles without errors
  • ✅ No linting issues introduced
  • Manual testing recommended: Force scan failure by testing during WiFi busy state

Impact

  • Low severity but improves user experience
  • Users now see proper error messages instead of empty network lists
  • Prevents potential memory leaks
  • Makes debugging connectivity issues easier

Fixes #10

- Check for negative return values from WiFi.scanNetworks()
- Return 500 error with JSON error response when scan fails
- Add WiFi.scanDelete() to prevent memory leak
- Improve serial output to indicate scan success/failure
- Fixes issue where scan errors were masked by empty array response

Fixes potential crashes and improves debugging experience when
WiFi scan operations fail due to hardware issues or interference.
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.

Bug: WiFi Scan Error Handling Missing in /scan Endpoint

1 participant