Skip to content

Commit 29c13cc

Browse files
authored
Merge pull request #1167 from b4s36t4/fix/webhook-plugin-errors
fix: return error response for webhook non-timeout error
2 parents cccd150 + 1559b5c commit 29c13cc

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

plugins/default/webhook.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
PluginHandler,
55
PluginParameters,
66
} from '../types';
7-
import { post } from '../utils';
7+
import { post, TimeoutError } from '../utils';
88

99
function parseHeaders(headers: unknown): Record<string, string> {
1010
try {
@@ -107,20 +107,30 @@ export const handler: PluginHandler = async (
107107
responseData: response.data,
108108
requestContext: {
109109
headers,
110-
timeout: 3000,
110+
timeout: parameters.timeout || 3000,
111111
},
112112
};
113113
} catch (e: any) {
114114
error = e;
115115
delete error.stack;
116116

117+
const isTimeoutError = e instanceof TimeoutError;
118+
119+
const responseData = !isTimeoutError && e.response?.body;
120+
const responseDataContentType = e.response?.headers?.get('content-type');
121+
117122
data = {
118123
explanation: `Webhook error: ${e.message}`,
119124
webhookUrl: parameters.webhookURL || 'No URL provided',
120125
requestContext: {
121126
headers: parameters.headers || {},
122-
timeout: 3000,
127+
timeout: parameters.timeout || 3000,
123128
},
129+
// return response body if it's not a ok response and not a timeout error
130+
...(responseData &&
131+
responseDataContentType === 'application/json' && {
132+
responseData: JSON.parse(responseData),
133+
}),
124134
};
125135
}
126136

plugins/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ErrorResponse {
99
status: number;
1010
statusText: string;
1111
body: string;
12+
headers?: Headers;
1213
}
1314

1415
export class HttpError extends Error {
@@ -21,7 +22,7 @@ export class HttpError extends Error {
2122
}
2223
}
2324

24-
class TimeoutError extends Error {
25+
export class TimeoutError extends Error {
2526
url: string;
2627
timeout: number;
2728
method: string;
@@ -222,6 +223,7 @@ export async function post<T = any>(
222223
status: response.status,
223224
statusText: response.statusText,
224225
body: errorBody,
226+
headers: response.headers,
225227
};
226228

227229
throw new HttpError(

src/middlewares/hooks/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ export class HooksManager {
306306
transformed: result.transformed || false,
307307
created_at: createdAt,
308308
log: result.log || null,
309+
fail_on_error:
310+
(check.parameters as Record<string, any>)?.failOnError || false,
309311
};
310312
} catch (err: any) {
311313
console.error(`Error executing check "${check.id}":`, err);
@@ -390,7 +392,10 @@ export class HooksManager {
390392
}
391393

392394
hookResult = {
393-
verdict: checkResults.every((result) => result.verdict || result.error),
395+
// if guardrail has error, make verdict false else do the normal check
396+
verdict: checkResults.every(
397+
(result) => result.verdict || (result.error && !result.fail_on_error)
398+
),
394399
id: hook.id,
395400
transformed: checkResults.some((result) => result.transformed),
396401
checks: checkResults,

src/middlewares/hooks/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export interface GuardrailCheckResult {
8080
};
8181
};
8282
log?: any;
83+
fail_on_error?: boolean;
8384
}
8485

8586
export interface GuardrailResult {

0 commit comments

Comments
 (0)