File size: 1,696 Bytes
aec3094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { NotFoundError } from './not-found.error';

export const webhookNotFoundErrorMessage = ({
	path,
	httpMethod,
	webhookMethods,
}: {
	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.`;
	}
};

export class WebhookNotFoundError extends NotFoundError {
	constructor(
		{
			path,
			httpMethod,
			webhookMethods,
		}: {
			path: string;
			httpMethod?: string;
			webhookMethods?: string[];
		},
		{ hint }: { hint: 'default' | 'production' } = { hint: 'default' },
	) {
		const errorMsg = webhookNotFoundErrorMessage({ path, httpMethod, webhookMethods });

		let hintMsg = '';
		if (!webhookMethods?.length) {
			hintMsg =
				hint === 'default'
					? "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)"
					: "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)";
		}

		super(errorMsg, hintMsg);
	}
}