handle search with colon (#25128)
* handle search with colon * nicer 400 errors * split up by words first to avoid dangerous regex * code comment
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import express from 'express'
|
||||
import libLanguages from '../lib/languages.js'
|
||||
import searchVersions from '../lib/search/versions.js'
|
||||
import loadLunrResults from '../lib/search/lunr-search.js'
|
||||
import loadLunrResults, { QueryTermError } from '../lib/search/lunr-search.js'
|
||||
import { cacheControlFactory } from './cache-control.js'
|
||||
|
||||
const languages = new Set(Object.keys(libLanguages))
|
||||
@@ -13,8 +13,11 @@ const noCacheControl = cacheControlFactory(0)
|
||||
router.get('/', async function getSearch(req, res, next) {
|
||||
const { query, version, language, filters, limit: limit_ } = req.query
|
||||
const limit = Math.min(parseInt(limit_, 10) || 10, 100)
|
||||
if (!versions.has(version) || !languages.has(language)) {
|
||||
return res.status(400).json([])
|
||||
if (!versions.has(version)) {
|
||||
return res.status(400).json({ error: 'Unrecognized version' })
|
||||
}
|
||||
if (!languages.has(language)) {
|
||||
return res.status(400).json({ error: 'Unrecognized language' })
|
||||
}
|
||||
if (!query || !limit) {
|
||||
return res.status(200).json([])
|
||||
@@ -38,6 +41,11 @@ router.get('/', async function getSearch(req, res, next) {
|
||||
return res.status(200).json(results)
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof QueryTermError) {
|
||||
// Handled as an not entirely unexpected potential error
|
||||
return res.status(400).json({ error: err.toString() })
|
||||
}
|
||||
|
||||
console.error(err)
|
||||
// Only reply if the headers have not been sent and the request was not aborted...
|
||||
if (!res.headersSent && !req.aborted) {
|
||||
|
||||
Reference in New Issue
Block a user