Skip to content

Commit b0490df

Browse files
authored
Merge pull request #5989 from Shopify/auth-error-handling
added error handling for authorization when backend server may not be reachable
2 parents 6067118 + e32a659 commit b0490df

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-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 from the authorization service. If this issue persists, please contact support at https://help.shopify.com',
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 from the authorization service. If this issue persists, please contact support at https://help.shopify.com',
90+
)
91+
})
6692
})
6793

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

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ 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(
49+
'Received unexpected response from the authorization service. If this issue persists, please contact support at https://help.shopify.com',
50+
)
51+
}
4552

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

0 commit comments

Comments
 (0)