Skip to content

Commit

Permalink
fix: catch error and display message for parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hetd54 committed Nov 27, 2023
1 parent 37e171f commit 1c4ded7
Show file tree
Hide file tree
Showing 2 changed files with 18 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
24 changes: 17 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,8 @@ export const SiteSearch: FunctionComponent<SearchProps> = ({
placeholder={"Type to search pages..."}
onChange={(e) => setSearchQuery(e.target.value)}
/>
<div className=" pt-2">
<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 1c4ded7

Please sign in to comment.