Skip to content

fix: add non-JSON response handling in HttpProvider and empty content validation#11

Open
knqiufan wants to merge 1 commit intoob-labs:mainfrom
knqiufan:fix/http-json-safety-and-empty-content-validation
Open

fix: add non-JSON response handling in HttpProvider and empty content validation#11
knqiufan wants to merge 1 commit intoob-labs:mainfrom
knqiufan:fix/http-json-safety-and-empty-content-validation

Conversation

@knqiufan
Copy link
Copy Markdown

@knqiufan knqiufan commented Apr 3, 2026

Summary

  • Bug [Enhancement]: TypeScript 完整复刻 PowerMem #4 — HttpProvider non-JSON response handling: HttpProvider.request() called res.json() without safety checks; when the server returned HTML (e.g. 502 Bad Gateway) or plain text, callers received a raw SyntaxError instead of a meaningful API error. Now checks Content-Type header first, catches JSON parse failures, and throws PowerMemAPIError with status code and truncated response body (max 200 chars). Matches Python pattern in rerank/generic.py (try json() → except ValueError → response.text).
  • Bug refactor: restructure to match Python powermem + full feature replication #5 — Empty content validation: add({content: ''}) and update(id, {content: ' '}) silently generated empty memories, wasting storage and embedding API calls. Now validates at multiple layers (NativeProvider + Memory facade) using !content || !content.trim(), matching Python's defensive not content or not content.strip() guards in memory.py. Also replaces throw new Error with throw new PowerMemError('...', 'NOT_FOUND') in update().

Changed files

File Change
src/core/http-provider.ts Add Content-Type check, JSON parse try-catch, meaningful error messages
src/core/native-provider.ts Add validateContent(), validate in add()/update(), use PowerMemError
src/core/memory.ts Add fast-fail empty content validation in facade add()/update()
tests/unit/core/http-provider.test.ts New: 9 test cases for JSON/non-JSON/connection error handling
tests/regression/edge-cases.test.ts Add 7 empty content validation test cases

Test plan

  • All 355 tests pass across 33 test files (0 failures)
  • HttpProvider tests cover: valid JSON, HTML response, plain text, missing Content-Type, malformed JSON, success=false, connection error, 404 handling, body truncation
  • Empty content tests cover: empty string add, whitespace-only add, empty string update, whitespace-only update, metadata-only update (no false positive), batch with empty item, NOT_FOUND error code

… validation

- HttpProvider.request(): check Content-Type before parsing, catch JSON
  parse errors, and return meaningful PowerMemAPIError with status code
  and body excerpt instead of raw SyntaxError
- NativeProvider.add(): reject empty/whitespace-only content with
  PowerMemError (code MEMORY_VALIDATION_ERROR)
- NativeProvider.update(): reject empty content and use PowerMemError
  (code NOT_FOUND) instead of generic Error for missing memory
- Memory facade: add fast-fail validation in add() and update()
- Add HttpProvider unit tests (9 cases) and empty content validation
  regression tests (7 cases)

Port parity with Python powermem error handling patterns:
  - rerank/generic.py: try json(), fallback to response.text
  - memory.py: not content or not content.strip() guards

Made-with: Cursor
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.

1 participant