Skip to content

fix(resources): throw descriptive error when JSON:API data is empty or absent - #33

Merged
asachs01 merged 1 commit into
mainfrom
fix/guard-empty-jsonapi-data
Aug 11, 2026
Merged

fix(resources): throw descriptive error when JSON:API data is empty or absent#33
asachs01 merged 1 commit into
mainfrom
fix/guard-empty-jsonapi-data

Conversation

@asachs01

@asachs01 asachs01 commented Aug 11, 2026

Copy link
Copy Markdown
Member

Defect

All 13 single-resource get* methods across src/resources/ followed this pattern:

const data = Array.isArray(response.data) ? response.data[0] : response.data;
return { id: data.id, ...data.attributes } as any;

When the API returns { data: [] } (data[0] is undefined) or a body with no data key at all, the next line crashes with an anonymous, undiagnosable TypeError: Cannot read properties of undefined (reading 'id') instead of a clear error. The comment in http.ts (lines 96-100) already documented this exact failure mode being triggered by content-type fallbacks returning {}.

Part of the fleet sweep for unchecked response-envelope access, following wyre-technology/halopsa-mcp#76 / wyre-technology/node-halopsa#59.

Fix

Added a shared unwrapJsonApiData<T>(response, context) helper to src/json-api-mapper.ts (mirroring node-halopsa's centralized unwrapSingle) that throws a descriptive AuvikErrorAuvik returned no <entity> for <id> (response data was empty or missing) — when the entity is missing, and routed all 13 sites through it:

  • alerts.tsgetHistory
  • billing.tsgetUsageDevice
  • inventory-configuration.tsget
  • inventory-device.tsgetInfo, getDetails, getWarranty, getLifecycle
  • inventory-entity.tsgetNote, getAudit
  • inventory-interface.tsgetInfo
  • inventory-network.tsgetInfo, getDetails
  • tenants.tsget

No behavior change for valid { data: {...} } and { data: [{...}] } shapes.

Test plan

TDD: tests were written first and confirmed failing on the unguarded code (26 failures, all TypeError: Cannot read properties of undefined), then the fix turned them green.

  • New table-driven suite tests/resources/unwrap-empty-data.test.ts covering all 13 methods x 4 shapes (52 tests): { data: [] } throws descriptive AuvikError, {} throws descriptive AuvikError, { data: {...} } resolves, { data: [{...}] } resolves.
  • Baseline before: 9 files / 45 tests passing. After: 10 files / 97 tests passing (45 + 52).
  • npm run lint (tsc --noEmit) clean, npm run build (tsup incl. DTS) green.

View with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is enabled.

…r absent

All 13 single-resource get* methods unwrapped response.data with
'Array.isArray(response.data) ? response.data[0] : response.data' and then
immediately read data.id, so an empty array or a body without data crashed
with an anonymous 'TypeError: Cannot read properties of undefined
(reading id)'. Route all 13 sites through a shared unwrapJsonApiData()
helper in json-api-mapper.ts that throws a descriptive AuvikError
('Auvik returned no <entity> for <id>') instead.

Part of the fleet sweep for unchecked response-envelope access, following
wyre-technology/halopsa-mcp#76 and wyre-technology/node-halopsa#59
(mirrors node-halopsa's centralized unwrapSingle).
@asachs01
asachs01 merged commit 62cf835 into main Aug 11, 2026
3 checks passed
@asachs01
asachs01 deleted the fix/guard-empty-jsonapi-data branch August 11, 2026 20:10
github-actions Bot pushed a commit that referenced this pull request Aug 11, 2026
## [1.3.1](v1.3.0...v1.3.1) (2026-08-11)

### Bug Fixes

* **resources:** throw descriptive error when JSON:API data is empty or absent ([#33](#33)) ([62cf835](62cf835)), closes [wyre-technology/halopsa-mcp#76](wyre-technology/halopsa-mcp#76) [wyre-technology/node-halopsa#59](wyre-technology/node-halopsa#59)
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant