Skip to content

Commit

Permalink
fix(core): Better error message in Webhook node when using the POST m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
michael-radency authored May 2, 2023
1 parent 5364a2d commit a0dd17e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { ExternalHooks } from '@/ExternalHooks';
import { whereClause } from './UserManagement/UserManagementHelper';
import { WorkflowsService } from './workflows/workflows.services';
import { START_NODES } from './constants';
import { webhookNotFoundErrorMessage } from './utils';

const WEBHOOK_PROD_UNREGISTERED_HINT =
"The workflow must be active for a production URL to run successfully. You can activate the workflow using the toggle in the top-right of the editor. Note that unlike test URL calls, production URL calls aren't shown on the canvas (only in the executions list)";
Expand Down Expand Up @@ -221,7 +222,7 @@ export class ActiveWorkflowRunner {
if (dynamicWebhooks === undefined || dynamicWebhooks.length === 0) {
// The requested webhook is not registered
throw new ResponseHelper.NotFoundError(
`The requested webhook "${httpMethod} ${path}" is not registered.`,
webhookNotFoundErrorMessage(path, httpMethod),
WEBHOOK_PROD_UNREGISTERED_HINT,
);
}
Expand All @@ -247,7 +248,7 @@ export class ActiveWorkflowRunner {
});
if (webhook === null) {
throw new ResponseHelper.NotFoundError(
`The requested webhook "${httpMethod} ${path}" is not registered.`,
webhookNotFoundErrorMessage(path, httpMethod),
WEBHOOK_PROD_UNREGISTERED_HINT,
);
}
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/TestWebhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { IResponseCallbackData, IWorkflowDb } from '@/Interfaces';
import { Push } from '@/push';
import * as ResponseHelper from '@/ResponseHelper';
import * as WebhookHelpers from '@/WebhookHelpers';
import { webhookNotFoundErrorMessage } from './utils';

const WEBHOOK_TEST_UNREGISTERED_HINT =
"Click the 'Execute workflow' button on the canvas, then try again. (In test mode, the webhook only works for one call after you click this button)";
Expand Down Expand Up @@ -69,8 +70,9 @@ export class TestWebhooks {
webhookData = activeWebhooks.get(httpMethod, pathElements.join('/'), webhookId);
if (webhookData === undefined) {
// The requested webhook is not registered
const methods = await this.getWebhookMethods(path);
throw new ResponseHelper.NotFoundError(
`The requested webhook "${httpMethod} ${path}" is not registered.`,
webhookNotFoundErrorMessage(path, httpMethod, methods),
WEBHOOK_TEST_UNREGISTERED_HINT,
);
}
Expand All @@ -95,8 +97,9 @@ export class TestWebhooks {
// TODO: Clean that duplication up one day and improve code generally
if (testWebhookData[webhookKey] === undefined) {
// The requested webhook is not registered
const methods = await this.getWebhookMethods(path);
throw new ResponseHelper.NotFoundError(
`The requested webhook "${httpMethod} ${path}" is not registered.`,
webhookNotFoundErrorMessage(path, httpMethod, methods),
WEBHOOK_TEST_UNREGISTERED_HINT,
);
}
Expand Down Expand Up @@ -160,7 +163,7 @@ export class TestWebhooks {
if (!webhookMethods.length) {
// The requested webhook is not registered
throw new ResponseHelper.NotFoundError(
`The requested webhook "${path}" is not registered.`,
webhookNotFoundErrorMessage(path),
WEBHOOK_TEST_UNREGISTERED_HINT,
);
}
Expand Down
28 changes: 28 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,31 @@ export const separate = <T>(array: T[], test: (element: T) => boolean) => {

return [pass, fail];
};

export const webhookNotFoundErrorMessage = (
path: string,
httpMethod?: string,
webhookMethods?: string[],
) => {
let webhookPath = path;

if (httpMethod) {
webhookPath = `${httpMethod} ${webhookPath}`;
}

if (webhookMethods?.length && httpMethod) {
let methods = '';

if (webhookMethods.length === 1) {
methods = webhookMethods[0];
} else {
const lastMethod = webhookMethods.pop();

methods = `${webhookMethods.join(', ')} or ${lastMethod as string}`;
}

return `This webhook is not registered for ${httpMethod} requests. Did you mean to make a ${methods} request?`;
} else {
return `The requested webhook "${webhookPath}" is not registered.`;
}
};
35 changes: 35 additions & 0 deletions packages/cli/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { webhookNotFoundErrorMessage } from '../../src/utils';

describe('utils test webhookNotFoundErrorMessage ', () => {
it('should return a message with path and method', () => {
const message = webhookNotFoundErrorMessage('webhook12345', 'GET');

expect(message).toEqual('The requested webhook "GET webhook12345" is not registered.');
});
it('should return a message with path', () => {
const message = webhookNotFoundErrorMessage('webhook12345');

expect(message).toEqual('The requested webhook "webhook12345" is not registered.');
});
it('should return a message with method with tip', () => {
const message = webhookNotFoundErrorMessage('webhook12345', 'POST', ['GET', 'PUT']);

expect(message).toEqual(
'This webhook is not registered for POST requests. Did you mean to make a GET or PUT request?',
);
});
it('should return a message with method with tip', () => {
const message = webhookNotFoundErrorMessage('webhook12345', 'POST', ['PUT']);

expect(message).toEqual(
'This webhook is not registered for POST requests. Did you mean to make a PUT request?',
);
});
it('should return a message with method with tip', () => {
const message = webhookNotFoundErrorMessage('webhook12345', 'POST', ['GET', 'PUT', 'DELETE']);

expect(message).toEqual(
'This webhook is not registered for POST requests. Did you mean to make a GET, PUT or DELETE request?',
);
});
});

0 comments on commit a0dd17e

Please sign in to comment.