-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance DOI Search Error Handling #450
Changes from all commits
f485ff9
96e019b
91167e3
f3f3380
91221c1
7bcddd9
9647510
ad7957a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react' | ||
import Alert from 'react-bootstrap/Alert' | ||
|
||
export default function NoResults() { | ||
return ( | ||
<Alert variant="warning">No results found.</Alert> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,30 +60,33 @@ function convertToQueryData(json: any): QueryData { | |
} | ||
|
||
export async function fetchDoisFacets(variables: QueryVar) { | ||
try { | ||
const options = { | ||
method: 'GET', | ||
headers: { accept: 'application/vnd.api+json' } | ||
} | ||
const searchParams = buildDoiSearchParams(variables) | ||
|
||
const res = await fetch( | ||
`${process.env.NEXT_PUBLIC_API_URL}/dois?${searchParams.toString()}`, | ||
options | ||
) | ||
const json = await res.json() | ||
|
||
const data = convertToQueryData(json) | ||
return { data } | ||
} catch (error) { | ||
return { error } | ||
const options = { | ||
method: 'GET', | ||
headers: { accept: 'application/vnd.api+json' } | ||
} | ||
const searchParams = buildDoiSearchParams(variables) | ||
|
||
const res = await fetch( | ||
`${process.env.NEXT_PUBLIC_API_URL}/dois?${searchParams.toString()}`, | ||
options | ||
) | ||
const json = await res.json() | ||
if(!res.ok) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to do the error handling this way on all the REST fetches? @jrhoads There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. We had been catching the error. React/Tanstack query expects an error to be thrown. Otherwise it never populates the error state. |
||
const errorMessage = json?.errors?.title || `Request for facets failed with status: ${res.status}`; | ||
throw new Error(errorMessage); | ||
} | ||
|
||
const data = convertToQueryData(json) | ||
return { data } | ||
} | ||
|
||
export function useSearchDoiFacetsQuery(variables: QueryVar) { | ||
// eslint-disable-next-line no-unused-vars | ||
const { cursor, ...vars } = variables | ||
const { isPending, data, error } = useQuery({ queryKey: ['doiSearch', vars, 'facets'], queryFn: async () => fetchDoisFacets(variables) }) | ||
const { isPending, data, error } = useQuery({ | ||
queryKey: ['doiSearch', vars, 'facets'], | ||
queryFn: async () => fetchDoisFacets(variables), | ||
}) | ||
|
||
return { loading: isPending, data: data?.data, error } | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason we may want to keep the granularity of splitting the error out from no results and showing the error component? @jrhoads
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we ever want to in the future, we can split this up again.