Skip to content

Commit fe80f4a

Browse files
author
Peter Bengtsson
authored
suppress es-search if ELASTICSEARCH_URL isn't configured (github#30096)
* suppress es-search if ELASTICSEARCH_URL isn't configured * simplify * simple 500 error instead
1 parent 84c5c95 commit fe80f4a

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

components/search/SearchError.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export function SearchError({ error }: Props) {
1313

1414
return (
1515
<div>
16-
{' '}
1716
<Flash variant="danger" sx={{ margin: '3rem' }}>
1817
{t('search_error')}
1918
<br />

middleware/api/es-search.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Client } from '@elastic/elasticsearch'
22

3-
const ELASTICSEARCH_URL = process.env.ELASTICSEARCH_URL || 'http://localhost:9200'
3+
// The reason this is exported is so the middleware endpoints can
4+
// find out if the environment variable has been set (and is truthy)
5+
// before attempting to call the main function in this file.
6+
export const ELASTICSEARCH_URL = process.env.ELASTICSEARCH_URL
47

58
const isDevMode = process.env.NODE_ENV !== 'production'
69

middleware/api/search.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import languages from '../../lib/languages.js'
55
import { allVersions } from '../../lib/all-versions.js'
66
import { cacheControlFactory } from '../cache-control.js'
77
import catchMiddlewareError from '../catch-middleware-error.js'
8-
import { getSearchResults } from './es-search.js'
8+
import { getSearchResults, ELASTICSEARCH_URL } from './es-search.js'
99

1010
// Used by the legacy search
1111
const versions = new Set(Object.values(searchVersions))
@@ -51,8 +51,28 @@ function convertLegacyVersionName(version) {
5151
return legacyEnterpriseServerVersions[version] || version
5252
}
5353

54+
function notConfiguredMiddleware(req, res, next) {
55+
if (!ELASTICSEARCH_URL) {
56+
if (process.env.NODE_ENV === 'production') {
57+
// Temporarily, this is OKish. The Docs Engineering team is
58+
// currently working on setting up an Elasticsearch cloud
59+
// instance that we can use. We don't currently have that,
60+
// but this code is running in production. We just don't want
61+
// to unnecessarily throw errors when it's actually a known thing.
62+
return res.status(500).send('ELASTICSEARCH_URL not been set up yet')
63+
}
64+
throw new Error(
65+
'process.env.ELASTICSEARCH_URL is not set. ' +
66+
"If you're working on this locally, add `ELASTICSEARCH_URL=http://localhost:9200` in your .env file"
67+
)
68+
}
69+
70+
return next()
71+
}
72+
5473
router.get(
5574
'/legacy',
75+
notConfiguredMiddleware,
5676
catchMiddlewareError(async function legacySearch(req, res, next) {
5777
const { query, version, language, filters, limit: limit_ } = req.query
5878
if (filters) {
@@ -196,6 +216,7 @@ const validationMiddleware = (req, res, next) => {
196216
router.get(
197217
'/v1',
198218
validationMiddleware,
219+
notConfiguredMiddleware,
199220
catchMiddlewareError(async function search(req, res, next) {
200221
const { indexName, query, page, size, debug, sort } = req.search
201222
const { meta, hits } = await getSearchResults({ indexName, query, page, size, debug, sort })

pages/search.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ export default function Page({ mainContext }: Props) {
2121
}
2222

2323
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
24+
// The dedicated search results page depends on a working Elasticsearch,
25+
// ultimately, but unfortunately, that might not be up and running.
26+
// So if that's the case, which might be true in production (Aug 2022)
27+
// or on an engineers local development, we basically pretend the
28+
// page doesn't exist.
29+
if (!process.env.ELASTICSEARCH_URL) {
30+
return { notFound: true }
31+
}
32+
2433
const req = context.req as any
2534
const res = context.res as any
2635

0 commit comments

Comments
 (0)