From 038f64cbc78f880fe7cd667ef4b853cc0ef560f7 Mon Sep 17 00:00:00 2001 From: James Meng Date: Wed, 30 Oct 2024 15:59:10 -0700 Subject: [PATCH 1/3] Add debug logs for storefront session errors --- .../theme-environment/storefront-session.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts index 1ec68e338d..91a78c2595 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts @@ -2,6 +2,7 @@ import {parseCookies, serializeCookies} from './cookies.js' import {defaultHeaders} from './storefront-utils.js' import {fetch} from '@shopify/cli-kit/node/http' import {AbortError} from '@shopify/cli-kit/node/error' +import {outputDebug} from '@shopify/cli-kit/node/output' export async function isStorefrontPasswordProtected(storeURL: string): Promise { const response = await fetch(prependHttps(storeURL), { @@ -104,6 +105,13 @@ async function sessionEssentialCookie(storeUrl: string, themeId: string, headers const setCookies = response.headers.raw()['set-cookie'] ?? [] const shopifyEssential = getCookie(setCookies, '_shopify_essential') + if (!shopifyEssential) { + outputDebug( + `Failed to obtain _shopify_essential cookie.\n + -Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}`, + ) + } + return shopifyEssential } @@ -128,6 +136,14 @@ async function enrichSessionWithStorefrontPassword( const setCookies = response.headers.raw()['set-cookie'] ?? [] const storefrontDigest = getCookie(setCookies, 'storefront_digest') + const requestId = response.headers.get('x-request-id') ?? 'unknown' + + if (!storefrontDigest) { + outputDebug( + `Failed to obtain storefront_digest cookie.\n + -Request ID: ${requestId}`, + ) + } return storefrontDigest } From 6b0286d54201b622cd1c4c26537f3b4cebdf4f74 Mon Sep 17 00:00:00 2001 From: James Meng Date: Thu, 31 Oct 2024 16:08:09 -0700 Subject: [PATCH 2/3] Add response body to log --- .../cli/utilities/theme-environment/storefront-session.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts index 91a78c2595..40748a12a3 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.ts @@ -108,7 +108,8 @@ async function sessionEssentialCookie(storeUrl: string, themeId: string, headers if (!shopifyEssential) { outputDebug( `Failed to obtain _shopify_essential cookie.\n - -Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}`, + -Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n + -Body: ${await response.text()}`, ) } @@ -136,12 +137,12 @@ async function enrichSessionWithStorefrontPassword( const setCookies = response.headers.raw()['set-cookie'] ?? [] const storefrontDigest = getCookie(setCookies, 'storefront_digest') - const requestId = response.headers.get('x-request-id') ?? 'unknown' if (!storefrontDigest) { outputDebug( `Failed to obtain storefront_digest cookie.\n - -Request ID: ${requestId}`, + -Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n + -Body: ${await response.text()}`, ) } From dc14fd90efdea1cc8df48a84cdd42905dcc13a41 Mon Sep 17 00:00:00 2001 From: James Meng Date: Thu, 31 Oct 2024 16:20:31 -0700 Subject: [PATCH 3/3] Update test mocks --- .../theme-environment/storefront-session.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts b/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts index 45bb5add6a..5d00ac6a27 100644 --- a/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts +++ b/packages/theme/src/cli/utilities/theme-environment/storefront-session.test.ts @@ -80,12 +80,14 @@ describe('Storefront API', () => { response({ status: 200, headers: {'set-cookie': ''}, + text: () => Promise.resolve(''), }), ) .mockResolvedValueOnce( response({ status: 200, headers: {'set-cookie': 'storefront_digest=digest-value; path=/; HttpOnly'}, + text: () => Promise.resolve(''), }), ) @@ -105,9 +107,15 @@ describe('Storefront API', () => { response({ status: 200, headers: {'set-cookie': '_shopify_essential=:AABBCCDDEEFFGGHH==123:; path=/; HttpOnly'}, + text: () => Promise.resolve(''), + }), + ) + .mockResolvedValueOnce( + response({ + status: 401, + text: () => Promise.resolve(''), }), ) - .mockResolvedValueOnce(response({status: 401})) // When const cookies = getStorefrontSessionCookies('https://example-store.myshopify.com', '123456', 'wrongpassword') @@ -121,7 +129,7 @@ describe('Storefront API', () => { // Tests rely on this function because the 'packages/theme' package cannot // directly access node-fetch and they use: new Response('OK', {status: 200}) - function response(mock: {status: number; headers?: {[key: string]: string}}) { + function response(mock: {status: number; headers?: {[key: string]: string}; text?: () => Promise}) { const setCookieHeader = (mock.headers ?? {})['set-cookie'] ?? '' const setCookieArray = [setCookieHeader]