Skip to content

Commit

Permalink
Revert "Log throttling errors on webhook registration"
Browse files Browse the repository at this point in the history
This reverts commit 7888756.
  • Loading branch information
rezaansyed committed Jan 17, 2024
1 parent c0c44e1 commit 4683a0e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,3 @@ export const HTTP_WEBHOOK_CREATE_ERROR_RESPONSE = {
},
},
};

export const HTTP_WEBHOOK_THROTTLING_ERROR_RESPONSE = {
errors: [
{
message: 'Throttled',
extensions: {
code: 'THROTTLED',
},
},
],
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DeliveryMethod, GraphqlQueryError, Session} from '@shopify/shopify-api';
import {DeliveryMethod, Session} from '@shopify/shopify-api';

import {shopifyApp} from '../../..';
import {
Expand Down Expand Up @@ -110,101 +110,4 @@ describe('Webhook registration', () => {
NOT_A_VALID_TOPIC: [expect.objectContaining({success: false})],
});
});

// eslint-disable-next-line no-warning-comments
// TODO: Remove tests once we have a better solution to parallel afterAuth calls
it('logs throttling errors', async () => {
// GIVEN
const shopify = shopifyApp(
testConfig({
webhooks: {
NOT_A_VALID_TOPIC: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: '/webhooks',
},
},
}),
);
const session = new Session({
id: `offline_${TEST_SHOP}`,
shop: TEST_SHOP,
isOnline: false,
state: 'test',
accessToken: 'totally_real_token',
});

await mockExternalRequests(
{
request: new Request(GRAPHQL_URL, {
method: 'POST',
body: 'webhookSubscriptions',
}),
response: new Response(
JSON.stringify(mockResponses.EMPTY_WEBHOOK_RESPONSE),
),
},
{
request: new Request(GRAPHQL_URL, {
method: 'POST',
body: 'webhookSubscriptionCreate',
}),
response: new Response(
JSON.stringify(mockResponses.HTTP_WEBHOOK_THROTTLING_ERROR_RESPONSE),
),
},
);

// WHEN
const results = await shopify.registerWebhooks({session});

// THEN
expect(results).toBeUndefined();
});

it('throws other errors', async () => {
// GIVEN
const shopify = shopifyApp(
testConfig({
webhooks: {
NOT_A_VALID_TOPIC: {
deliveryMethod: DeliveryMethod.Http,
callbackUrl: '/webhooks',
},
},
}),
);
const session = new Session({
id: `offline_${TEST_SHOP}`,
shop: TEST_SHOP,
isOnline: false,
state: 'test',
accessToken: 'totally_real_token',
});

await mockExternalRequests(
{
request: new Request(GRAPHQL_URL, {
method: 'POST',
body: 'webhookSubscriptions',
}),
response: new Response(
JSON.stringify(mockResponses.EMPTY_WEBHOOK_RESPONSE),
),
},
{
request: new Request(GRAPHQL_URL, {
method: 'POST',
body: 'webhookSubscriptionCreate',
}),
response: new Response(
JSON.stringify({errors: [{extensions: {code: 'FAILED_REQUEST'}}]}),
),
},
);

// THEN
expect(shopify.registerWebhooks({session})).rejects.toThrowError(
GraphqlQueryError,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,26 @@ import type {RegisterWebhooksOptions} from './types';

export function registerWebhooksFactory({api, logger}: BasicParams) {
return async function registerWebhooks({session}: RegisterWebhooksOptions) {
return api.webhooks
.register({session})
.then((response) => {
Object.entries(response).forEach(([topic, topicResults]) => {
topicResults.forEach(({success, ...rest}) => {
if (success) {
logger.debug('Registered webhook', {
topic,
shop: session.shop,
operation: rest.operation,
});
} else {
logger.error('Failed to register webhook', {
topic,
shop: session.shop,
result: JSON.stringify(rest.result),
});
}
});
return api.webhooks.register({session}).then((response) => {
Object.entries(response).forEach(([topic, topicResults]) => {
topicResults.forEach(({success, ...rest}) => {
if (success) {
logger.debug('Registered webhook', {
topic,
shop: session.shop,
operation: rest.operation,
});
} else {
logger.error('Failed to register webhook', {
topic,
shop: session.shop,
result: JSON.stringify(rest.result),
});
}
});

return response;
})
.catch((error) => {
if (
error.response?.errors?.find(
(responseError: {extensions: {code: string}}) =>
responseError?.extensions?.code === 'THROTTLED',
)
) {
logger.error('Failed to register webhooks', {
shop: session.shop,
error: JSON.stringify(error),
});
} else {
throw error;
}
});

return response;
});
};
}
2 changes: 1 addition & 1 deletion packages/shopify-app-remix/src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ interface JSONArray extends Array<JSONValue> {}

type RegisterWebhooks = (
options: RegisterWebhooksOptions,
) => Promise<RegisterReturn | void>;
) => Promise<RegisterReturn>;

export enum LoginErrorType {
MissingShop = 'MISSING_SHOP',
Expand Down

0 comments on commit 4683a0e

Please sign in to comment.