Skip to content

Commit

Permalink
Error page fix (#188)
Browse files Browse the repository at this point in the history
* fix(error pages): add sentry breadcrumbs and error handling for activity link

* chore(release): 0.0.77

* fix(errors): improve error messages
  • Loading branch information
rahulkeerthi authored Nov 27, 2023
1 parent 7a84f2a commit a28ca1c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hosted-pages",
"version": "0.0.76",
"version": "0.0.77",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down Expand Up @@ -70,4 +70,4 @@
"typescript": "4.7.4",
"vitest": "^0.34.6"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
StartHostedActivitySessionParams,
StartHostedActivitySessionPayload,
} from '../../../types'
import { isNil } from 'lodash'

type Data =
| StartHostedActivitySessionPayload
Expand All @@ -31,37 +32,39 @@ export default async function handler(
}
)

try {
const session = await fetch(environment.orchestrationApiUrl, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
const response = await fetch(environment.orchestrationApiUrl, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
mutation StartHostedActivitySessionViaHostedPagesLink($input: StartHostedActivitySessionViaHostedPagesLinkInput!) {
startHostedActivitySessionViaHostedPagesLink(input: $input) {
session_id
session_url
}
}
`,
variables: {
input: {
hosted_pages_link_id: hostedPagesLinkId,
},
variables: {
input: {
hosted_pages_link_id: hostedPagesLinkId,
},
}),
})
},
}),
})

const session_response = await session.json()
const { data, errors } = await response.json()
if (!isNil(errors) && errors.length > 0) {
res
.status(200)
.json({ error: errors[0].extensions?.data?.message ?? errors[0].message })
return
}

const { session_id, session_url } =
session_response?.data?.startHostedActivitySessionViaHostedPagesLink
const { session_id, session_url } =
data?.startHostedActivitySessionViaHostedPagesLink

res.status(200).json({ sessionId: session_id, sessionUrl: session_url })
} catch (error) {
res.status(500).json({ error })
}
res.status(200).json({ sessionId: session_id, sessionUrl: session_url })
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export default async function handler(
})
const { data, errors } = await response.json()
if (!isNil(errors) && errors.length > 0) {
res.status(200).json({ error: errors[0].extensions?.data.message })
res
.status(200)
.json({ error: errors[0].extensions?.data?.message ?? errors[0].message })
return
}

const sessionUrl = data?.startHostedPathwaySessionFromLink.session_url
Expand Down
15 changes: 13 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const Home: NextPageWithLayout = () => {
category: BreadcrumbCategory.SESSION_CANCEL,
data: session,
})
router.push(session?.cancel_url || 'https://awell.health')
router.push(session?.cancel_url ?? 'https://awell.health')
}

const shouldRedirect =
Expand All @@ -84,7 +84,18 @@ const Home: NextPageWithLayout = () => {
!isNil(session?.cancel_url))

useEffect(() => {
if (isNil(session?.status) || typeof window === 'undefined') {
if (isNil(session?.status)) {
addSentryBreadcrumb({
category: BreadcrumbCategory.GENERIC,
data: { session, message: 'Session status is null' },
})
return
}
if (typeof window === 'undefined') {
addSentryBreadcrumb({
category: BreadcrumbCategory.GENERIC,
data: { session, message: 'Window is undefined' },
})
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { ErrorPage } from '../ErrorPage'
import { LoadingPage } from '../LoadingPage'
import { useTranslation } from 'next-i18next'
import classes from './startHostedActivitySessionFlow.module.css'
import { addSentryBreadcrumb } from '../../services/ErrorReporter'
import { BreadcrumbCategory } from '../../services/ErrorReporter/addSentryBreadcrumb'

const fetcher = (url: string) => fetch(url).then((res) => res.json())

Expand All @@ -21,7 +23,7 @@ export const StartHostedActivitySessionFlow: FC<
const router = useRouter()
const { t } = useTranslation()

const { data, error } = useSWR<StartHostedActivitySessionPayload>(
const { data } = useSWR<StartHostedActivitySessionPayload>(
`/api/startHostedActivitySessionViaHostedPagesLink/${hostedPagesLinkId}`,
fetcher
)
Expand All @@ -32,15 +34,31 @@ export const StartHostedActivitySessionFlow: FC<

useEffect(() => {
if (!isNil(data) && !isNil(data?.sessionUrl)) {
addSentryBreadcrumb({
category: BreadcrumbCategory.NAVIGATION,
data: {
hostedPagesLinkId,
sessionUrl: data?.sessionUrl,
message: 'Redirecting to hosted session',
},
})
const { sessionUrl } = data
window.location.href = sessionUrl
}
}, [data, router])

if (error) {
if (data?.error) {
addSentryBreadcrumb({
category: BreadcrumbCategory.HOSTED_ACTIVITY_ERROR,
data: { hostedPagesLinkId },
})

return (
<div className={classes.container}>
<ErrorPage title={t('link_page.loading_error')} onRetry={retry} />
<ErrorPage
title={`${t('link_page.loading_error')} ${data.error}`}
onRetry={retry}
/>
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isNil } from 'lodash'
import { useRouter } from 'next/router'
import { FC, useEffect, useState } from 'react'
import { FC, useEffect } from 'react'
import useSWR from 'swr'
import { ErrorPage } from '../ErrorPage'
import { LoadingPage } from '../LoadingPage'
Expand All @@ -10,6 +10,8 @@ import {
StartHostedCareflowSessionParams,
StartHostedCareflowSessionPayload,
} from '../../../pages/api/startHostedPathwaySessionFromLink/[hostedPagesLinkId]'
import { addSentryBreadcrumb } from '../../services/ErrorReporter'
import { BreadcrumbCategory } from '../../services/ErrorReporter/addSentryBreadcrumb'

const fetcher = (url: string) => fetch(url).then((res) => res.json())

Expand All @@ -28,15 +30,27 @@ export const StartHostedCareflowSessionFlow: FC<

useEffect(() => {
if (!isNil(data) && !isNil(data?.sessionUrl)) {
addSentryBreadcrumb({
category: BreadcrumbCategory.NAVIGATION,
data: {
hostedPagesLinkId,
sessionUrl: data?.sessionUrl,
message: 'Redirecting to hosted session',
},
})
const { sessionUrl } = data
window.location.href = sessionUrl
}
}, [data, router])

if (data?.error) {
addSentryBreadcrumb({
category: BreadcrumbCategory.HOSTED_PAGES_LINK_ERROR,
data: { hostedPagesLinkId, message: data.error },
})
return (
<div className={classes.container}>
<ErrorPage title={data.error} />
<ErrorPage title={`${t('link_page.loading_error')} ${data.error}`} />
</div>
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/services/ErrorReporter/addSentryBreadcrumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export enum BreadcrumbCategory {
GENERIC = 'GENERIC',
NAVIGATION = 'NAVIGATION',
SLOW_REDIRECT = 'SLOW_REDIRECT',
HOSTED_PAGES_LINK_ERROR = 'HOSTED_PAGES_LINK_ERROR',
HOSTED_ACTIVITY_ERROR = 'HOSTED_ACTIVITY_ERROR',
}

const SentryBreadcrumbMessageDictionary = {
Expand All @@ -25,6 +27,8 @@ const SentryBreadcrumbMessageDictionary = {
[BreadcrumbCategory.GENERIC]: 'Generic event',
[BreadcrumbCategory.NAVIGATION]: 'Navigation',
[BreadcrumbCategory.SLOW_REDIRECT]: 'Slow redirect at session end',
[BreadcrumbCategory.HOSTED_PAGES_LINK_ERROR]: 'Error with hosted pages link',
[BreadcrumbCategory.HOSTED_ACTIVITY_ERROR]: 'Error with hosted activity link',
}

export const addSentryBreadcrumb = ({
Expand Down
1 change: 1 addition & 0 deletions types/StartHostedActivitySessionPayload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type StartHostedActivitySessionPayload = {
sessionId: string
sessionUrl: string
error?: string
}

0 comments on commit a28ca1c

Please sign in to comment.