@@ -7,27 +7,13 @@ import config from "config";
77const queue = "submission" ;
88const worker = "submit" ;
99
10+ const ERROR_CODE = "SUBMIT_ERROR" ;
11+
1012const logger = pino ( ) ;
1113export const metadata = { queue, worker } ;
1214const REQUEST_TIMEOUT = Number . parseInt ( config . get < string > ( "Submission.requestTimeout" ) ) ;
1315logger . info ( metadata , `REQUEST_TIMEOUT set to ${ REQUEST_TIMEOUT } ` ) ;
1416
15- axios . interceptors . request . use ( ( intercepted ) => {
16- // @ts -ignore
17- intercepted . meta = intercepted . meta ?? { } ;
18- // @ts -ignore
19- intercepted . meta . requestStartedAt = new Date ( ) . getTime ( ) ;
20- return intercepted ;
21- } ) ;
22-
23- axios . interceptors . response . use ( ( intercepted ) => {
24- // @ts -ignore
25- intercepted . config . meta . requestFinishedAt = new Date ( ) . getTime ( ) ;
26- // @ts -ignore
27- intercepted . config . meta . responseTime = intercepted . config . meta . requestFinishedAt - intercepted . config . meta . requestStartedAt ;
28- return intercepted ;
29- } ) ;
30-
3117/**
3218 * When a "submission" event is detected, this worker POSTs the data to `job.data.data.webhook_url`
3319 * The source of this event is the runner, after a user has submitted a form.
@@ -39,6 +25,7 @@ export async function submitHandler(job: Job<SubmitJob>) {
3925 const { data, id } = job ;
4026 const requestBody = data . data ;
4127 const url = data . webhook_url ;
28+
4229 try {
4330 const res = await axios . post ( url , requestBody , {
4431 timeout : REQUEST_TIMEOUT ,
@@ -54,11 +41,45 @@ export async function submitHandler(job: Job<SubmitJob>) {
5441 }
5542 return ;
5643 } catch ( e : any ) {
57- logger . error ( jobLogData , `job: ${ id } failed with ${ e . cause ?? e . message } ` ) ;
44+ logger . error ( jobLogData , `${ ERROR_CODE } to ${ url } job: ${ id } failed with ${ e . cause ?? e . message } ` ) ;
45+
46+ if ( e . response ) {
47+ logger . error ( jobLogData , `${ ERROR_CODE } ${ JSON . stringify ( e . response . data ) } ` ) ;
48+ const { message, name, code, response } = e ;
49+ const { status, data } = response ;
50+ throw {
51+ message,
52+ name,
53+ code,
54+ status,
55+ data,
56+ } ;
57+ }
58+
59+ if ( e . request ) {
60+ logger . error ( jobLogData , `${ ERROR_CODE } to ${ url } request could not be sent, see database for error` ) ;
61+ }
62+
5863 // @ts -ignore
5964 if ( e . cause instanceof AggregateError ) {
6065 throw { errors : e . cause . errors } ;
6166 }
6267 throw e ;
6368 }
6469}
70+
71+ axios . interceptors . request . use ( ( intercepted ) => {
72+ // @ts -ignore
73+ intercepted . meta = intercepted . meta ?? { } ;
74+ // @ts -ignore
75+ intercepted . meta . requestStartedAt = new Date ( ) . getTime ( ) ;
76+ return intercepted ;
77+ } ) ;
78+
79+ axios . interceptors . response . use ( ( intercepted ) => {
80+ // @ts -ignore
81+ intercepted . config . meta . requestFinishedAt = new Date ( ) . getTime ( ) ;
82+ // @ts -ignore
83+ intercepted . config . meta . responseTime = intercepted . config . meta . requestFinishedAt - intercepted . config . meta . requestStartedAt ;
84+ return intercepted ;
85+ } ) ;
0 commit comments