Skip to content

Commit

Permalink
no log sentry error during maintenance on (#3675)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlebektomas authored Jan 15, 2025
2 parents b5d6fc4 + cce5750 commit 4c72919
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion storefront/pages/_error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ErrorPage.getInitialProps = getServerSidePropsWrapper(({ redisClient, domainConf
err = JSON.stringify({ name: err.name, message: err.message, stack: err.stack, cause: err.cause });
}

if (statusCode !== 404 || isWithErrorDebugging) {
if ((statusCode !== 404 || isWithErrorDebugging) && statusCode !== 503) {
logException({
message: err,
statusCode,
Expand Down
5 changes: 5 additions & 0 deletions storefront/urql/errorExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export const getErrorExchange =
return;
}

const isMaintenance = error.response?.status === 503;
if (isMaintenance) {
return;
}

if (isWithErrorDebugging && operation.kind === 'mutation') {
handleErrorMessagesForMutation(error);

Expand Down
13 changes: 13 additions & 0 deletions storefront/urql/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,24 @@ export const fetcher =
body,
});

const isJsonContentType = result.headers.get('content-type')?.includes('application/json');

if (!isJsonContentType) {
return Promise.resolve(
new Response(JSON.stringify({}), {
statusText: result.statusText,
status: result.status,
headers: { 'Content-Type': 'application/json' },
}),
);
}

const res = await result.json();

if (res.data !== undefined && res.error === undefined) {
await redisClient.set(hash, JSON.stringify(res.data), { EX: ttl });
}

return Promise.resolve(
new Response(JSON.stringify(res), {
statusText: 'OK',
Expand Down
21 changes: 21 additions & 0 deletions storefront/vitest/utils/urql/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ describe('fetcher test', () => {
vi.stubEnv('REDIS_PREFIX', 'TEST_PREFIX');
mockFetch.mockImplementation(() =>
Promise.resolve({
headers: new Headers({
'content-type': 'application/json',
}),
json: () => Promise.resolve({ data: TEST_RESPONSE_BODY }),
}),
);
Expand All @@ -125,6 +128,24 @@ describe('fetcher test', () => {
vi.unstubAllEnvs();
});

test('should handle non-JSON content-type responses', async () => {
(isClientGetter as Mock).mockImplementation(() => false);
mockFetch.mockImplementation(() =>
Promise.resolve({
headers: new Headers({
'content-type': 'text/html',
}),
}),
);

const testFetcher = fetcher(mockRedisClient);
const response = await testFetcher(TEST_URL, REQUEST_WITH_DIRECTIVE);
const responseBody = await response.json();

expect(responseBody).toStrictEqual({});
expect(response.headers.get('content-type')).toBe('application/json');
});

test('using fetcher on an already cached query should get it from Redis', async () => {
mockRedisClientGet.mockImplementation(() => JSON.stringify(TEST_RESPONSE_BODY));
(isClientGetter as Mock).mockImplementation(() => false);
Expand Down

0 comments on commit 4c72919

Please sign in to comment.