Skip to content

Commit c20bb46

Browse files
committed
production warn mitigation
1 parent 44997bb commit c20bb46

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const asyncHooks = require('async_hooks');
2+
const { isProduction } = require('./lib');
23
const { create: createHooks } = require('./hooks');
34
const { ExecutionContextErrors } = require('./constants');
45

@@ -13,6 +14,18 @@ asyncHooks.createHook(
1314
createHooks(executionContextMap)
1415
).enable();
1516

17+
/**
18+
* Handles execution context error, throws when none production
19+
* @param code
20+
*/
21+
const handleError = (code) => {
22+
if (!isProduction()) {
23+
throw code;
24+
}
25+
26+
console.warn(code);
27+
};
28+
1629
/**
1730
* The Execution Context API
1831
*/
@@ -27,7 +40,7 @@ const Context = {
2740
const asyncId = asyncHooks.executionAsyncId();
2841

2942
// Creation is allowed once per execution context
30-
if (executionContextMap.has(asyncId)) throw ExecutionContextErrors.CONTEXT_ALREADY_DECLARED;
43+
if (executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_ALREADY_DECLARED);
3144

3245
executionContextMap.set(asyncId, {
3346
context: { ...initialContext, executionId: asyncId },
@@ -42,7 +55,7 @@ const Context = {
4255
update: (update = {}) => {
4356
const asyncId = asyncHooks.executionAsyncId();
4457

45-
if (!executionContextMap.has(asyncId)) throw ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS;
58+
if (!executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS);
4659

4760
const contextData = executionContextMap.get(asyncId);
4861

@@ -60,7 +73,7 @@ const Context = {
6073
*/
6174
get: () => {
6275
const asyncId = asyncHooks.executionAsyncId();
63-
if (!executionContextMap.has(asyncId)) throw ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS;
76+
if (!executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS);
6477

6578
const { context = {}, ref } = executionContextMap.get(asyncId);
6679
if (ref) {

0 commit comments

Comments
 (0)