Skip to content

Commit

Permalink
Merge pull request #711 from thepolicylab-projectportals/fix-lunr-crash
Browse files Browse the repository at this point in the history
fix: catch error and display message for parsing errors
  • Loading branch information
hetd54 authored Dec 4, 2023
2 parents 34872bc + afacd93 commit d88a5dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const invalidSearches = async ({ canvasElement }) => {

await userEvent.clear(searchInput)

await userEvent.type(searchInput, "____", {
await userEvent.type(searchInput, "--", {
delay: 100,
})
}
Expand Down
26 changes: 19 additions & 7 deletions packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ export const SiteSearch: FunctionComponent<SearchProps> = ({
}: SearchProps) => {
const [searchQuery, setSearchQuery] = useState([])
const [queryResults, setQueryResults] = useState([])
const [errorMessage, setErrorMessage] = useState("")

useEffect(() => {
if (searchQuery.length > 0) {
let searchResults = index.search(searchQuery)
let results = []
searchResults.forEach(function (result) {
results.push(db[result.ref])
})
setQueryResults(searchResults)
// catch the error, and display an error message to help user
try {
let searchResults = index.search(searchQuery)
setQueryResults(searchResults)
setErrorMessage("")
} catch (error) {
if (error instanceof lunr.QueryParseError) {
setErrorMessage(error.message)
return
} else {
throw error
}
}
} else {
setQueryResults([])
setErrorMessage("")
}
}, [searchQuery])

Expand All @@ -39,7 +48,10 @@ export const SiteSearch: FunctionComponent<SearchProps> = ({
placeholder={"Type to search pages..."}
onChange={(e) => setSearchQuery(e.target.value)}
/>
<div className=" pt-2">
{errorMessage && (
<p className="px-4 text-red text-tag font-bold">{errorMessage}</p>
)}
<div className="pt-2">
Number of found pages:
{queryResults.length}
</div>
Expand Down

0 comments on commit d88a5dc

Please sign in to comment.