refactor(core): Expand error message for unknown webhook request (#15178)

This commit is contained in:
Iván Ovejero
2025-05-07 14:45:57 +02:00
committed by GitHub
parent 715127fa87
commit c6ceee2bee
3 changed files with 25 additions and 4 deletions

View File

@@ -50,4 +50,8 @@ export class WebhookEntity {
get isDynamic() {
return this.webhookPath.split('/').some((s) => s.startsWith(':'));
}
display() {
return `${this.method} ${this.webhookPath}`;
}
}

View File

@@ -3,6 +3,7 @@ import type express from 'express';
import { Logger } from 'n8n-core';
import { ensureError, type IHttpRequestMethods } from 'n8n-workflow';
import { WebhookNotFoundError } from '@/errors/response-errors/webhook-not-found.error';
import * as ResponseHelper from '@/response-helper';
import type {
IWebhookManager,
@@ -10,6 +11,8 @@ import type {
WebhookRequest,
} from '@/webhooks/webhook.types';
import { WebhookService } from './webhook.service';
const WEBHOOK_METHODS: IHttpRequestMethods[] = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT'];
class WebhookRequestHandler {
@@ -56,10 +59,20 @@ class WebhookRequestHandler {
}
} catch (e) {
const error = ensureError(e);
Container.get(Logger).debug(
const logger = Container.get(Logger);
if (e instanceof WebhookNotFoundError) {
const currentlyRegistered = await Container.get(WebhookService).findAll();
logger.error(`Received request for unknown webhook: ${e.message}`, {
currentlyRegistered: currentlyRegistered.map((w) => w.display()),
});
} else {
logger.error(
`Error in handling webhook request ${req.method} ${req.path}: ${error.message}`,
{ stacktrace: error.stack },
);
}
return ResponseHelper.sendErrorResponse(res, error);
}

View File

@@ -38,6 +38,10 @@ export class WebhookService {
void this.cacheService.setMany(allWebhooks.map((w) => [w.cacheKey, w]));
}
async findAll() {
return await this.webhookRepository.find();
}
private async findCached(method: Method, path: string) {
const cacheKey = `webhook:${method}-${path}`;