better error handling
This commit is contained in:
parent
aa3986cb76
commit
02f03b41bf
|
@ -184,14 +184,14 @@ const FileListing: FC<{ query?: ParsedUrlQuery }> = ({ query }) => {
|
|||
console.log(error)
|
||||
|
||||
// If error includes 403 which means the user has not completed initial setup, redirect to OAuth page
|
||||
if (error.message.includes('403')) {
|
||||
if (error.status === 403) {
|
||||
router.push('/onedrive-vercel-index-oauth/step-1')
|
||||
return <div></div>
|
||||
}
|
||||
|
||||
return (
|
||||
<PreviewContainer>
|
||||
{error.message.includes('401') ? <Auth redirect={path} /> : <FourOhFour errorMsg={error.message} />}
|
||||
{error.status === 401 ? <Auth redirect={path} /> : <FourOhFour errorMsg={JSON.stringify(error.message)} />}
|
||||
</PreviewContainer>
|
||||
)
|
||||
}
|
||||
|
|
|
@ -178,6 +178,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
// Querying current path identity (file or folder) and follow up query childrens in folder
|
||||
// console.log(accessToken)
|
||||
|
||||
try {
|
||||
const { data: identityData } = await axios.get(requestUrl, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
params: {
|
||||
|
@ -201,7 +202,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
})
|
||||
|
||||
// Extract next page token from full @odata.nextLink
|
||||
const nextPage = folderData['@odata.nextLink'] ? folderData['@odata.nextLink'].match(/&\$skiptoken=(.+)/i)[1] : null
|
||||
const nextPage = folderData['@odata.nextLink']
|
||||
? folderData['@odata.nextLink'].match(/&\$skiptoken=(.+)/i)[1]
|
||||
: null
|
||||
|
||||
// Return paging token if specified
|
||||
if (nextPage) {
|
||||
|
@ -213,4 +216,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
}
|
||||
res.status(200).json({ file: identityData })
|
||||
return
|
||||
} catch (error: any) {
|
||||
res.status(error.response.status).json({ error: error.response.data })
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,14 +4,18 @@ import useSWR, { cache, Key, useSWRInfinite } from 'swr'
|
|||
import { getStoredToken } from './protectedRouteHandler'
|
||||
|
||||
// Common axios fetch function for use with useSWR
|
||||
export function fetcher(url: string, token?: string): Promise<any> {
|
||||
return token
|
||||
? axios
|
||||
.get(url, {
|
||||
export async function fetcher(url: string, token?: string): Promise<any> {
|
||||
try {
|
||||
return (
|
||||
await (token
|
||||
? axios.get(url, {
|
||||
headers: { 'od-protected-token': token },
|
||||
})
|
||||
.then(res => res.data)
|
||||
: axios.get(url).then(res => res.data)
|
||||
: axios.get(url))
|
||||
).data
|
||||
} catch (err: any) {
|
||||
throw { status: err.response.status, message: err.response.data }
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Use stale SWR instead of revalidating on each request. Not ideal for this scenario but have to do
|
||||
|
|
Loading…
Reference in a new issue