|
1 | 1 | import express from 'express'
|
2 | 2 |
|
3 | 3 | import searchVersions from '../../lib/search/versions.js'
|
| 4 | +import FailBot from '../../lib/failbot.js' |
4 | 5 | import languages from '../../lib/languages.js'
|
5 | 6 | import { allVersions } from '../../lib/all-versions.js'
|
6 | 7 | import { defaultCacheControl } from '../cache-control.js'
|
@@ -234,20 +235,42 @@ router.get(
|
234 | 235 | notConfiguredMiddleware,
|
235 | 236 | catchMiddlewareError(async function search(req, res, next) {
|
236 | 237 | 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 }) |
238 | 240 |
|
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 | + } |
247 | 249 |
|
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 | + } |
251 | 274 | })
|
252 | 275 | )
|
253 | 276 |
|
|
0 commit comments