@@ -19,43 +19,32 @@ router.post('/', async function postEvents (req, res, next) {
1919 return res . status ( 400 ) . json ( { } )
2020 }
2121
22- // Don't depend on Hydro on local development
23- if ( isDev && ! req . hydro . maySend ( ) ) {
24- return res . status ( 200 ) . json ( { } )
25- }
26-
27- try {
28- const hydroRes = await req . hydro . publish (
22+ if ( req . hydro . maySend ( ) ) {
23+ // intentionally don't await this async request
24+ // so that the http response afterwards is sent immediately
25+ req . hydro . publish (
2926 req . hydro . schemas [ fields . type ] ,
3027 omit ( fields , OMIT_FIELDS )
31- )
32-
33- if ( ! hydroRes . ok ) {
34- const err = new Error ( 'Hydro request failed' )
35- err . status = hydroRes . status
36- err . path = fields . path
37-
38- await FailBot . report ( err , {
39- path : fields . path ,
40- hydroStatus : hydroRes . status ,
41- hydroText : await hydroRes . text ( )
42- } )
43-
44- throw err
45- }
46-
47- if ( res . headersSent ) {
48- throw new Error ( 'Cannot send http response: Hydro publish succeeded, but took too long.' )
49- }
50-
51- return res . status ( 201 ) . json ( fields )
52- } catch ( err ) {
53- if ( isDev ) console . error ( err )
54-
55- if ( ! res . headersSent ) {
56- return res . status ( 502 ) . json ( { } )
57- }
28+ ) . then ( async ( hydroRes ) => {
29+ if ( ! hydroRes . ok ) {
30+ const err = new Error ( 'Hydro request failed' )
31+ err . status = hydroRes . status
32+ err . path = fields . path
33+
34+ await FailBot . report ( err , {
35+ path : fields . path ,
36+ hydroStatus : hydroRes . status ,
37+ hydroText : await hydroRes . text ( )
38+ } )
39+
40+ throw err
41+ }
42+ } ) . catch ( ( e ) => {
43+ if ( isDev ) console . error ( e )
44+ } ) ;
5845 }
46+
47+ return res . status ( 201 ) . json ( fields )
5948} )
6049
6150module . exports = router
0 commit comments