◐ Shell
clean mode source ↗

process: add optional detail to process emitWarning · nodejs/node@dd20e68

@@ -4,30 +4,48 @@

4455

const common = require('../common');

66

const assert = require('assert');

7-

const util = require('util');

7+8+

const testMsg = 'A Warning';

9+

const testCode = 'CODE001';

10+

const testDetail = 'Some detail';

11+

const testType = 'CustomWarning';

812913

process.on('warning', common.mustCall((warning) => {

1014

assert(warning);

1115

assert(/^(Warning|CustomWarning)/.test(warning.name));

12-

assert.strictEqual(warning.message, 'A Warning');

13-

if (warning.code) assert.strictEqual(warning.code, 'CODE001');

14-

}, 8));

15-16-

process.emitWarning('A Warning');

17-

process.emitWarning('A Warning', 'CustomWarning');

18-

process.emitWarning('A Warning', CustomWarning);

19-

process.emitWarning('A Warning', 'CustomWarning', CustomWarning);

20-

process.emitWarning('A Warning', 'CustomWarning', 'CODE001');

21-22-

function CustomWarning() {

23-

Error.call(this);

24-

this.name = 'CustomWarning';

25-

this.message = 'A Warning';

26-

this.code = 'CODE001';

27-

Error.captureStackTrace(this, CustomWarning);

16+

assert.strictEqual(warning.message, testMsg);

17+

if (warning.code) assert.strictEqual(warning.code, testCode);

18+

if (warning.detail) assert.strictEqual(warning.detail, testDetail);

19+

}, 15));

20+21+

class CustomWarning extends Error {

22+

constructor() {

23+

super();

24+

this.name = testType;

25+

this.message = testMsg;

26+

this.code = testCode;

27+

Error.captureStackTrace(this, CustomWarning);

28+

}

2829

}

29-

util.inherits(CustomWarning, Error);

30-

process.emitWarning(new CustomWarning());

30+31+

[

32+

[testMsg],

33+

[testMsg, testType],

34+

[testMsg, CustomWarning],

35+

[testMsg, testType, CustomWarning],

36+

[testMsg, testType, testCode],

37+

[testMsg, { type: testType }],

38+

[testMsg, { type: testType, code: testCode }],

39+

[testMsg, { type: testType, code: testCode, detail: testDetail }],

40+

[new CustomWarning()],

41+

// detail will be ignored for the following. No errors thrown

42+

[testMsg, { type: testType, code: testCode, detail: true }],

43+

[testMsg, { type: testType, code: testCode, detail: [] }],

44+

[testMsg, { type: testType, code: testCode, detail: null }],

45+

[testMsg, { type: testType, code: testCode, detail: 1 }]

46+

].forEach((i) => {

47+

assert.doesNotThrow(() => process.emitWarning.apply(null, i));

48+

});

31493250

const warningNoToString = new CustomWarning();

3351

warningNoToString.toString = null;

@@ -47,7 +65,6 @@ assert.throws(() => process.emitWarning(1), expectedError);

4765

assert.throws(() => process.emitWarning({}), expectedError);

4866

assert.throws(() => process.emitWarning(true), expectedError);

4967

assert.throws(() => process.emitWarning([]), expectedError);

50-

assert.throws(() => process.emitWarning('', {}), expectedError);

5168

assert.throws(() => process.emitWarning('', '', {}), expectedError);

5269

assert.throws(() => process.emitWarning('', 1), expectedError);

5370

assert.throws(() => process.emitWarning('', '', 1), expectedError);