Skip to content

fix: search service graceful degradation on ES unavailability (#1235) - #2

Open
Dev-Odun-oss wants to merge 1 commit into
mainfrom
fix/search-service-graceful-degradation-1235
Open

fix: search service graceful degradation on ES unavailability (#1235)#2
Dev-Odun-oss wants to merge 1 commit into
mainfrom
fix/search-service-graceful-degradation-1235

Conversation

@Dev-Odun-oss

Copy link
Copy Markdown
Owner

Summary

Wraps all public search methods with connectivity-error handling so that a temporarily unreachable Elasticsearch cluster returns empty/degraded results instead of propagating unhandled 500s.

Changes

  • services/searchService.js
    • isConnectivityError() — identifies ConnectionError, NoLivingConnectionsError, TimeoutError from @elastic/elasticsearch
    • search() — catches connectivity errors, logs warn, returns { hits: [], total: 0, facets: {...}, durationMs }
    • suggest() — returns [] on connectivity errors
    • indexReward(), indexCampaign(), indexUser(), deleteDocument() — resolve silently on connectivity errors
    • ResponseError (bad queries / mapping mismatch) still propagates normally
    • Added _setClient() test helper for reliable mock injection without depending on module-level mock hoisting
  • tests/searchService.test.js — 8 unit tests covering all acceptance criteria

Acceptance Criteria

  • searchRewards returns { hits: [], total: 0 } when ES is unreachable
  • Error is logged at warn level (not error) for transient connectivity issues
  • Unit test mocks ES client to throw ConnectionError and verifies degraded response
  • Actual ResponseError for bad queries still propagates normally
  • Existing search functionality unaffected when ES is reachable (happy-path test)

Testing

npx vitest run tests/searchService.test.js
Test Files  1 passed (1)
Tests       8 passed (8)
Duration    ~500ms

Closes Nova-reward#1235

…eward#1235)

searchService.js now degrades gracefully when Elasticsearch is unreachable:
- Added isConnectivityError() to identify ConnectionError,
  NoLivingConnectionsError, and TimeoutError from @elastic/elasticsearch
- search() catches connectivity errors, logs a warn, and returns
  { hits: [], total: 0, facets: {...}, durationMs } instead of propagating 500
- suggest() returns [] on connectivity errors
- indexReward(), indexCampaign(), indexUser(), deleteDocument() silently
  continue (log warn, return undefined) on connectivity errors
- ResponseError (bad query / mapping mismatch) still propagates normally
- Added _setClient() test helper for reliable mock injection in unit tests

Tests (tests/searchService.test.js):
- ConnectionError → degraded result { hits: [], total: 0 }
- NoLivingConnectionsError → degraded result
- ConnectionError does NOT throw (resolves to degraded shape)
- ResponseError propagates (not swallowed)
- suggest() returns [] on ConnectionError
- Happy path: real data returned when ES is healthy
- indexReward() resolves without throwing when ES is unreachable
- deleteDocument() resolves without throwing when ES is unreachable

Closes Nova-reward#1235
@github-actions

Copy link
Copy Markdown

🦀 Cargo Dependency Vulnerability Scan

✅ No vulnerabilities found in Rust dependencies.

@milah-247

Copy link
Copy Markdown

Looks good to me! Approved.

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.

[Backend] Add Graceful Degradation to searchService When Elasticsearch Is Unreachable

2 participants