Skip to content

Catches decode/processing failures in sensor ingest#732

Open
devleo10 wants to merge 2 commits intojlab-sensing:mainfrom
devleo10:fix/498-sensor-decode-error-handling
Open

Catches decode/processing failures in sensor ingest#732
devleo10 wants to merge 2 commits intojlab-sensing:mainfrom
devleo10:fix/498-sensor-decode-error-handling

Conversation

@devleo10
Copy link
Copy Markdown
Contributor

@devleo10 devleo10 commented Mar 25, 2026

Summary

  • Fixes Fail nicely when failing to decode measurements #498 by handling sensor measurement decode/processing failures gracefully and returning 400 instead of bubbling unhandled exceptions as 500.
  • Hardens sensor endpoints to return explicit 400 for unsupported Content-Type values (instead of raising ValueError).
  • Adds regression tests for malformed binary, garbage payloads, empty payload, and unsupported content-type paths to prevent future regressions.

What Changed

  • backend/api/resources/util.py

    • Wrapped decode_measurement(...) in guarded error handling.
    • Wrapped process_measurement_dict(...) path to prevent unhandled exceptions from reaching Flask.
    • Added structured logging for failure paths.
    • Fixed co2 branch to append Photoresistivity insertion result consistently.
    • Added explicit unsupported-measurement fallback response (501) for unknown measurement types.
  • backend/api/resources/sensor_data.py

    • Replaced error-raising content-type branch with explicit 400 response.
    • Uses request.mimetype so application/json; charset=utf-8 is handled correctly.
  • backend/api/resources/sensor_data_json.py

    • Same content-type hardening as binary endpoint (400 instead of exception path).
    • Uses request.mimetype for robust content-type matching.
  • backend/tests/test_sensor.py

    • Added tests:
      • invalid binary returns 400
      • garbage ascii payload returns 400
      • empty body returns 400
      • unsupported content type on /api/sensor/ returns 400
      • unsupported content type on /api/sensor_json/ returns 400

Why

Issue #498 showed decode failures surfacing as unhandled exceptions in production logs (/api/sensor/), causing internal server errors.
This PR ensures malformed client payloads are treated as client errors (400) with clear response messaging and test coverage.

Validation

  • Manual check:
    • POST /api/sensor/ with invalid octet-stream payload now returns 400 with decode/processing error message.
  • Automated tests:

Risk / Impact

  • Low risk: changes are localized to sensor request handling and tests.
  • Behavior change is intentional and aligns with API expectations:
    • malformed requests → 400
    • unknown measurement type → 501
  • No new third-party dependencies introduced.

…ed bad content-type paths with explicit 400 responses, and adds regression tests for invalid/empty payloads and unsupported content types.
@devleo10 devleo10 changed the title Catches decode/processing failures in sensor ingest, replaces unhandled bad content-type paths with explicit 400 responses, and adds regression tests for invalid/empty payloads and unsupported content types. Catches decode/processing failures in sensor ingest Mar 25, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 61.03896% with 30 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
backend/api/resources/util.py 35.89% 25 Missing ⚠️
backend/api/resources/sensor_data_json.py 58.33% 5 Missing ⚠️
Flag Coverage Δ
backend 71.14% <61.03%> (+0.53%) ⬆️
frontend 52.18% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
backend/api/resources/sensor_data.py 57.40% <100.00%> (+30.99%) ⬆️
backend/tests/test_sensor.py 100.00% <100.00%> (ø)
backend/api/resources/sensor_data_json.py 57.57% <58.33%> (+19.64%) ⬆️
backend/api/resources/util.py 49.04% <35.89%> (-3.72%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@devleo10
Copy link
Copy Markdown
Contributor Author

Before the fix

image (1)

After the fix

IMG-20260326-WA0033

@devleo10
Copy link
Copy Markdown
Contributor Author

@jmadden173 @aleclevy Lemme know if this looks good.

@jmadden173 jmadden173 added the awaiting-review For PRs that have been looked at and waiting for a review. label Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review For PRs that have been looked at and waiting for a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fail nicely when failing to decode measurements

2 participants