File tree Expand file tree Collapse file tree 4 files changed +23
-5
lines changed Expand file tree Collapse file tree 4 files changed +23
-5
lines changed Original file line number Diff line number Diff line change 4
4
PluginHandler ,
5
5
PluginParameters ,
6
6
} from '../types' ;
7
- import { post } from '../utils' ;
7
+ import { post , TimeoutError } from '../utils' ;
8
8
9
9
function parseHeaders ( headers : unknown ) : Record < string , string > {
10
10
try {
@@ -107,20 +107,30 @@ export const handler: PluginHandler = async (
107
107
responseData : response . data ,
108
108
requestContext : {
109
109
headers,
110
- timeout : 3000 ,
110
+ timeout : parameters . timeout || 3000 ,
111
111
} ,
112
112
} ;
113
113
} catch ( e : any ) {
114
114
error = e ;
115
115
delete error . stack ;
116
116
117
+ const isTimeoutError = e instanceof TimeoutError ;
118
+
119
+ const responseData = ! isTimeoutError && e . response ?. body ;
120
+ const responseDataContentType = e . response ?. headers ?. get ( 'content-type' ) ;
121
+
117
122
data = {
118
123
explanation : `Webhook error: ${ e . message } ` ,
119
124
webhookUrl : parameters . webhookURL || 'No URL provided' ,
120
125
requestContext : {
121
126
headers : parameters . headers || { } ,
122
- timeout : 3000 ,
127
+ timeout : parameters . timeout || 3000 ,
123
128
} ,
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
+ } ) ,
124
134
} ;
125
135
}
126
136
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ export interface ErrorResponse {
9
9
status : number ;
10
10
statusText : string ;
11
11
body : string ;
12
+ headers ?: Headers ;
12
13
}
13
14
14
15
export class HttpError extends Error {
@@ -21,7 +22,7 @@ export class HttpError extends Error {
21
22
}
22
23
}
23
24
24
- class TimeoutError extends Error {
25
+ export class TimeoutError extends Error {
25
26
url : string ;
26
27
timeout : number ;
27
28
method : string ;
@@ -222,6 +223,7 @@ export async function post<T = any>(
222
223
status : response . status ,
223
224
statusText : response . statusText ,
224
225
body : errorBody ,
226
+ headers : response . headers ,
225
227
} ;
226
228
227
229
throw new HttpError (
Original file line number Diff line number Diff line change @@ -306,6 +306,8 @@ export class HooksManager {
306
306
transformed : result . transformed || false ,
307
307
created_at : createdAt ,
308
308
log : result . log || null ,
309
+ fail_on_error :
310
+ ( check . parameters as Record < string , any > ) ?. failOnError || false ,
309
311
} ;
310
312
} catch ( err : any ) {
311
313
console . error ( `Error executing check "${ check . id } ":` , err ) ;
@@ -390,7 +392,10 @@ export class HooksManager {
390
392
}
391
393
392
394
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
+ ) ,
394
399
id : hook . id ,
395
400
transformed : checkResults . some ( ( result ) => result . transformed ) ,
396
401
checks : checkResults ,
Original file line number Diff line number Diff line change @@ -80,6 +80,7 @@ export interface GuardrailCheckResult {
80
80
} ;
81
81
} ;
82
82
log ?: any ;
83
+ fail_on_error ?: boolean ;
83
84
}
84
85
85
86
export interface GuardrailResult {
You can’t perform that action at this time.
0 commit comments