Skip to content

Commit 7ec8050

Browse files
fix(address-form): Update error message for address form (#3909)
1 parent e00cf1f commit 7ec8050

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/Address/AddressCard.test.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,12 @@ describe('AddressCard', () => {
380380
})
381381

382382
describe('when there is an error in the form', () => {
383-
it('renders the error', async () => {
383+
it('renders the error when the error is an instance of Error', async () => {
384384
const { user } = setup()
385385
const randomError = 'not a valid address'
386386
mocks.useUpdateBillingAddress.mockReturnValue({
387387
mutate: vi.fn(),
388-
error: randomError,
388+
error: new Error(randomError),
389389
})
390390
render(
391391
<AddressCard
@@ -398,7 +398,36 @@ describe('AddressCard', () => {
398398

399399
await user.click(screen.getByTestId('edit-address'))
400400

401-
expect(screen.getByText(randomError)).toBeInTheDocument()
401+
expect(
402+
screen.getByText((content, _element) => content.includes(randomError))
403+
).toBeInTheDocument()
404+
})
405+
406+
it('renders the error when the error is an instance of BillingApiError', async () => {
407+
const { user } = setup()
408+
const stripeError = 'Your card has expired'
409+
mocks.useUpdateBillingAddress.mockReturnValue({
410+
mutate: vi.fn(),
411+
error: {
412+
data: {
413+
detail: stripeError,
414+
},
415+
},
416+
})
417+
render(
418+
<AddressCard
419+
subscriptionDetail={subscriptionDetail}
420+
provider="gh"
421+
owner="codecov"
422+
/>,
423+
{ wrapper }
424+
)
425+
426+
await user.click(screen.getByTestId('edit-address'))
427+
428+
expect(
429+
screen.getByText((content, _element) => content.includes(stripeError))
430+
).toBeInTheDocument()
402431
})
403432
})
404433

src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/Address/AddressForm.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import cs from 'classnames'
33
import { z } from 'zod'
44

55
import { AddressSchema } from 'services/account/useAccountDetails'
6-
import { useUpdateBillingAddress } from 'services/account/useUpdateBillingAddress'
6+
import {
7+
BillingApiError,
8+
useUpdateBillingAddress,
9+
} from 'services/account/useUpdateBillingAddress'
710
import { Theme, useThemeContext } from 'shared/ThemeContext'
811
import Button from 'ui/Button'
912

@@ -50,7 +53,6 @@ function AddressForm({
5053
mutate: updateAddress,
5154
isLoading,
5255
error,
53-
reset,
5456
} = useUpdateBillingAddress({
5557
provider,
5658
owner,
@@ -70,8 +72,6 @@ function AddressForm({
7072
}
7173
}
7274

73-
const showError = error && !reset
74-
7575
return (
7676
<form onSubmit={submit} aria-label="form">
7777
<div className={cs('flex flex-col gap-3')}>
@@ -95,7 +95,9 @@ function AddressForm({
9595
},
9696
}}
9797
/>
98-
<p className="mt-1 text-ds-primary-red">{showError && error}</p>
98+
<p className="mt-1 text-ds-primary-red">
99+
{error && getErrorMessage(error)}
100+
</p>
99101
</div>
100102
<div className="flex gap-1">
101103
<Button
@@ -123,4 +125,23 @@ function AddressForm({
123125
)
124126
}
125127

128+
export const getErrorMessage = (
129+
error: Error | BillingApiError
130+
): string | undefined => {
131+
if (!error) return undefined
132+
133+
if (error instanceof Error) {
134+
if (error.message) {
135+
return `Could not save billing address: ${error.message}`
136+
}
137+
return 'Could not save billing address. Please contact support at [email protected] for assistance.'
138+
}
139+
140+
if (error.data?.detail) {
141+
return `Could not save billing address: ${error.data.detail}`
142+
}
143+
144+
return 'Could not save billing address due to an unknown error. Please contact support at [email protected] for assistance.'
145+
}
146+
126147
export default AddressForm

src/services/account/useUpdateBillingAddress.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface useUpdateBillingAddressParams {
99

1010
interface useUpdateBillingAddressReturn {
1111
reset: () => void
12-
error: null | Error
12+
error: null | Error | BillingApiError
1313
isLoading: boolean
1414
mutate: (variables: any, data: any) => void
1515
data: undefined | unknown
@@ -29,6 +29,12 @@ interface AddressInfo {
2929
address: Address
3030
}
3131

32+
export interface BillingApiError {
33+
data: {
34+
detail: string
35+
}
36+
}
37+
3238
export function useUpdateBillingAddress({
3339
provider,
3440
owner,

0 commit comments

Comments
 (0)