Skip to content

Commit 9d62b41

Browse files
committed
handle any status code & error case
1 parent 03f1fff commit 9d62b41

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/plugins/event_dispatcher/index.node.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018, 2020-2021, Optimizely
2+
* Copyright 2016-2018, 2020-2021, 2024 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,15 +55,19 @@ export const dispatchEvent = function(
5555

5656
const requestCallback = function(response?: { statusCode: number }): void {
5757
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
58-
reqWrapper.req?.destroy();
5958
callback(response);
6059
}
60+
reqWrapper.req?.destroy();
61+
reqWrapper.req = undefined;
6162
};
6263

6364
reqWrapper.req = (parsedUrl.protocol === 'http:' ? http : https)
6465
.request(requestOptions, requestCallback as (res: http.IncomingMessage) => void);
6566
// Add no-op error listener to prevent this from throwing
66-
reqWrapper.req.on('error', function() {});
67+
reqWrapper.req.on('error', function() {
68+
reqWrapper.req?.destroy();
69+
reqWrapper.req = undefined;
70+
});
6771
reqWrapper.req.write(dataString);
6872
reqWrapper.req.end();
6973
return reqWrapper.req;

0 commit comments

Comments
 (0)