Skip to content

Commit 8fbb3d0

Browse files
author
Peter Bengtsson
authored
don't let 4xx errors propagate up from es-search (github#30495)
1 parent 54991f7 commit 8fbb3d0

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

middleware/api/search.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express'
22

33
import searchVersions from '../../lib/search/versions.js'
4+
import FailBot from '../../lib/failbot.js'
45
import languages from '../../lib/languages.js'
56
import { allVersions } from '../../lib/all-versions.js'
67
import { defaultCacheControl } from '../cache-control.js'
@@ -234,20 +235,42 @@ router.get(
234235
notConfiguredMiddleware,
235236
catchMiddlewareError(async function search(req, res, next) {
236237
const { indexName, query, page, size, debug, sort } = req.search
237-
const { meta, hits } = await getSearchResults({ indexName, query, page, size, debug, sort })
238+
try {
239+
const { meta, hits } = await getSearchResults({ indexName, query, page, size, debug, sort })
238240

239-
if (process.env.NODE_ENV !== 'development') {
240-
// The assumption, at the moment is that searches are never distinguished
241-
// differently depending on a cookie or a request header.
242-
// So the only distinguishing key is the request URL.
243-
// Because of that, it's safe to allow the reverse proxy (a.k.a the CDN)
244-
// cache and hold on to this.
245-
defaultCacheControl(res)
246-
}
241+
if (process.env.NODE_ENV !== 'development') {
242+
// The assumption, at the moment is that searches are never distinguished
243+
// differently depending on a cookie or a request header.
244+
// So the only distinguishing key is the request URL.
245+
// Because of that, it's safe to allow the reverse proxy (a.k.a the CDN)
246+
// cache and hold on to this.
247+
defaultCacheControl(res)
248+
}
247249

248-
// The v1 version of the output matches perfectly what comes out
249-
// of the getSearchResults() function.
250-
res.status(200).json({ meta, hits })
250+
// The v1 version of the output matches perfectly what comes out
251+
// of the getSearchResults() function.
252+
res.status(200).json({ meta, hits })
253+
} catch (error) {
254+
// If getSearchResult() throws an error that might be 404 inside
255+
// elasticsearch, if we don't capture that here, it will propgate
256+
// to the next middleware.
257+
if (process.env.NODE_ENV === 'development') {
258+
console.error('Error calling getSearchResults()', error)
259+
} else {
260+
await Promise.all(
261+
FailBot.report(error, {
262+
url: req.url,
263+
indexName,
264+
query,
265+
page,
266+
size,
267+
debug,
268+
sort,
269+
})
270+
)
271+
}
272+
res.status(500).send(error.message)
273+
}
251274
})
252275
)
253276

0 commit comments

Comments
 (0)