Skip to content

Commit eee4978

Browse files
committed
added error handling for authorization when server may not be reachable
1 parent 85961a8 commit eee4978

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

.changeset/young-rocks-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/cli-kit': patch
3+
---
4+
5+
Add error handling when the authorization service is not reachable

packages/cli-kit/src/private/node/session/device-authorization.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ describe('requestDeviceAuthorization', () => {
6363
})
6464
expect(got).toEqual(dataExpected)
6565
})
66+
67+
test('when the response is not valid JSON, throw an error', async () => {
68+
// Given
69+
const response = new Response('not valid JSON')
70+
vi.mocked(shopifyFetch).mockResolvedValue(response)
71+
vi.mocked(identityFqdn).mockResolvedValue('fqdn.com')
72+
vi.mocked(clientId).mockReturnValue('clientId')
73+
74+
// When/Then
75+
await expect(requestDeviceAuthorization(['scope1', 'scope2'])).rejects.toThrow(
76+
'Received unexpected response. This may be a service outage. Please try again later.',
77+
)
78+
})
79+
80+
test('when the response is empty, throw an error', async () => {
81+
// Given
82+
const response = new Response('')
83+
vi.mocked(shopifyFetch).mockResolvedValue(response)
84+
vi.mocked(identityFqdn).mockResolvedValue('fqdn.com')
85+
vi.mocked(clientId).mockReturnValue('clientId')
86+
87+
// When/Then
88+
await expect(requestDeviceAuthorization(['scope1', 'scope2'])).rejects.toThrow(
89+
'Received unexpected response. This may be a service outage. Please try again later.',
90+
)
91+
})
6692
})
6793

6894
describe('pollForDeviceAuthorization', () => {

packages/cli-kit/src/private/node/session/device-authorization.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ export async function requestDeviceAuthorization(scopes: string[]): Promise<Devi
4141
})
4242

4343
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44-
const jsonResult: any = await response.json()
44+
let jsonResult: any
45+
try {
46+
jsonResult = await response.json()
47+
} catch (error) {
48+
throw new BugError('Received unexpected response. This may be a service outage. Please try again later.')
49+
}
4550

4651
outputDebug(outputContent`Received device authorization code: ${outputToken.json(jsonResult)}`)
4752
if (!jsonResult.device_code || !jsonResult.verification_uri_complete) {

0 commit comments

Comments
 (0)