Skip to content

Commit

Permalink
Handle server-side mutation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Aug 30, 2024
1 parent cff28b0 commit 8f7ee0b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
19 changes: 19 additions & 0 deletions pages/setup/account.page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ describe('getServerSideProps', () => {
);
});

it('swallows server-side mutation errors', async () => {
const accountListOptions = {
accountLists: {
nodes: [{ id: 'account-list-1' }],
},
};
query.mockResolvedValue({
data: accountListOptions,
});
mutate.mockRejectedValue(new Error('Failed'));

await expect(getServerSideProps(context)).resolves.toEqual({
props: {
accountListOptions,
session,
},
});
});

it('does not set an account list as the default when there are multiple', async () => {
const accountListOptions = {
accountLists: {
Expand Down
41 changes: 24 additions & 17 deletions pages/setup/account.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,32 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async (
if (accountListOptions.accountLists.nodes.length === 1) {
// The user has exactly one account list, so set it as the default and go to preferences
const defaultAccountListId = accountListOptions.accountLists.nodes[0].id;
await ssrClient.mutate<
UpdateUserDefaultAccountMutation,
UpdateUserDefaultAccountMutationVariables
>({
mutation: UpdateUserDefaultAccountDocument,
variables: {
input: {
attributes: {
defaultAccountList: defaultAccountListId,
try {
await ssrClient.mutate<
UpdateUserDefaultAccountMutation,
UpdateUserDefaultAccountMutationVariables
>({
mutation: UpdateUserDefaultAccountDocument,
variables: {
input: {
attributes: {
defaultAccountList: defaultAccountListId,
},
},
},
},
});
return {
redirect: {
destination: `/accountLists/${defaultAccountListId}/settings/preferences`,
permanent: false,
},
};
});
return {
redirect: {
destination: `/accountLists/${defaultAccountListId}/settings/preferences`,
permanent: false,
},
};
} catch {
// If setting the account list failed, silently swallow the error and let
// the user view the page. If the error is persistent, the mutation will
// fail there when they try to choose a default account list, and they
// will at least get an error message.
}
}

return {
Expand Down

0 comments on commit 8f7ee0b

Please sign in to comment.