GeezerVison is showing github.com — console: improve inspectOptions validation · nodejs/node@272ddb1
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/console/constructor.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,13 +91,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { |
91 | 91 | if (typeof colorMode !== 'boolean' && colorMode !== 'auto') |
92 | 92 | throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode); |
93 | 93 |
|
94 | | - if (inspectOptions) { |
| 94 | + if (typeof inspectOptions === 'object' && inspectOptions !== null) { |
95 | 95 | if (inspectOptions.colors !== undefined && |
96 | 96 | options.colorMode !== undefined) { |
97 | 97 | throw new ERR_INCOMPATIBLE_OPTION_PAIR( |
98 | 98 | 'inspectOptions.color', 'colorMode'); |
99 | 99 | } |
100 | 100 | optionsMap.set(this, inspectOptions); |
| 101 | + } else if (inspectOptions !== undefined) { |
| 102 | + throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'object', inspectOptions); |
101 | 103 | } |
102 | 104 |
|
103 | 105 | // Bind the prototype functions to this Console instance |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-console-instance.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -128,3 +128,21 @@ out.write = err.write = (d) => {}; |
128 | 128 | assert.throws(() => c2.warn('foo'), /^Error: err$/); |
129 | 129 | assert.throws(() => c2.dir('foo'), /^Error: out$/); |
130 | 130 | } |
| 131 | + |
| 132 | +// Console constructor throws if inspectOptions is not an object. |
| 133 | +[null, true, false, 'foo', 5, Symbol()].forEach((inspectOptions) => { |
| 134 | + assert.throws( |
| 135 | + () => { |
| 136 | + new Console({ |
| 137 | + stdout: out, |
| 138 | + stderr: err, |
| 139 | + inspectOptions |
| 140 | + }); |
| 141 | + }, |
| 142 | + { |
| 143 | + message: 'The "inspectOptions" argument must be of type object.' + |
| 144 | + ` Received type ${typeof inspectOptions}`, |
| 145 | + code: 'ERR_INVALID_ARG_TYPE' |
| 146 | + } |
| 147 | + ); |
| 148 | +}); |