Skip to content
Myr

custom-throw.js

This file

Path

test/verbose/custom-throw.js

104 lines · 5.6 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';3import {assertRejects} from '../helpers/assert.js';

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

line 4why, one step back
4import {execa} from '../../index.js';

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 5–11why, one step back
5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
6import {foobarString} from '../helpers/input.js';7import {runVerboseSubprocess} from '../helpers/verbose.js';8import {earlyErrorOptions, earlyErrorOptionsSync} from '../helpers/early-error.js';910setFixtureDirectory();11

TweaksSindre Sorhusbefore adoption698a1b8

line 12why, one step back
12const testCommandThrowPropagate = async (type, options, isSync) => {

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 13–19why, one step back
13 const {nestedResult} = await runVerboseSubprocess({
14 isSync,15 type,16 optionsFixture: 'custom-throw.js',17 errorMessage: foobarString,18 ...options,19 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 20–21why, one step back
20 assert.ok(nestedResult instanceof Error);
21 assert.equal(nestedResult.message, foobarString);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 22–23why, one step back
22};
23

TweaksSindre Sorhusbefore adoption698a1b8

lines 24–35why, one step back
24test('Propagate verbose exception in "verbose" function, "command"', () => testCommandThrowPropagate('command', {}, false));
25test('Propagate verbose exception in "verbose" function, "error"', () => testCommandThrowPropagate('error', {}, false));26test('Propagate verbose exception in "verbose" function, "duration"', () => testCommandThrowPropagate('duration', {}, false));27test('Propagate verbose exception in "verbose" function, "command", spawn error', () => testCommandThrowPropagate('command', earlyErrorOptions, false));28test('Propagate verbose exception in "verbose" function, "error", spawn error', () => testCommandThrowPropagate('error', earlyErrorOptions, false));29test('Propagate verbose exception in "verbose" function, "duration", spawn error', () => testCommandThrowPropagate('duration', earlyErrorOptions, false));30test('Propagate verbose exception in "verbose" function, "command", sync', () => testCommandThrowPropagate('command', {}, true));31test('Propagate verbose exception in "verbose" function, "error", sync', () => testCommandThrowPropagate('error', {}, true));32test('Propagate verbose exception in "verbose" function, "duration", sync', () => testCommandThrowPropagate('duration', {}, true));33test('Propagate verbose exception in "verbose" function, "command", spawn error, sync', () => testCommandThrowPropagate('command', earlyErrorOptionsSync, true));34test('Propagate verbose exception in "verbose" function, "error", spawn error, sync', () => testCommandThrowPropagate('error', earlyErrorOptionsSync, true));35test('Propagate verbose exception in "verbose" function, "duration", spawn error, sync', () => testCommandThrowPropagate('duration', earlyErrorOptionsSync, true));

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 36why, one step back
36

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37const testCommandThrowHandle = async (type, isSync) => {

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 38–43why, one step back
38 const {nestedResult} = await runVerboseSubprocess({
39 isSync,40 type,41 optionsFixture: 'custom-throw.js',42 errorMessage: foobarString,43 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 44–47why, one step back
44 assert.ok(nestedResult instanceof Error);
45 assert.ok(nestedResult.stack.startsWith(isSync ? 'ExecaSyncError' : 'ExecaError'));46 assert.ok(nestedResult.cause instanceof Error);47 assert.equal(nestedResult.cause.message, foobarString);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 48–49why, one step back
48};
49

TweaksSindre Sorhusbefore adoption698a1b8

lines 50–52why, one step back
50test('Handle exceptions in "verbose" function, "output"', () => testCommandThrowHandle('output', false));
51test('Handle exceptions in "verbose" function, "ipc"', () => testCommandThrowHandle('ipc', false));52test('Handle exceptions in "verbose" function, "output", sync', () => testCommandThrowHandle('output', true));

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

line 53why, one step back
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54const testCommandThrowWrap = async (type, options, isSync) => {

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 55–61why, one step back
55 const {nestedResult} = await runVerboseSubprocess({
56 isSync,57 type,58 optionsFixture: 'custom-throw.js',59 errorMessage: foobarString,60 ...options,61 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 62–65why, one step back
62 assert.ok(nestedResult instanceof Error);
63 assert.ok(nestedResult.stack.startsWith(isSync ? 'ExecaSyncError' : 'ExecaError'));64 assert.ok(nestedResult.cause instanceof Error);65 assert.notEqual(nestedResult.cause.message, foobarString);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 66–67why, one step back
66};
67

TweaksSindre Sorhusbefore adoption698a1b8

lines 68–70why, one step back
68test('Propagate wrapped exception in "verbose" function, "output", spawn error', () => testCommandThrowWrap('output', earlyErrorOptions, false));
69test('Propagate wrapped exception in "verbose" function, "ipc", spawn error', () => testCommandThrowWrap('ipc', earlyErrorOptions, false));70test('Propagate wrapped exception in "verbose" function, "output", spawn error, sync', () => testCommandThrowWrap('output', earlyErrorOptionsSync, true));

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

lines 71–73why, one step back
71
72// A throwing `verbose` function is a logging error: it must not discard IPC messages which have already been buffered73const getThrowingVerbose = (type, thrownValue = new Error(foobarString)) => ({

TweaksSindre Sorhusbefore adoption698a1b8

line 74why, one step back
74 verbose(line, verboseObject) {

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

lines 75–80why, one step back
75 if (verboseObject.type === type) {
76 throw thrownValue;77 }78 },79});80

TweaksSindre Sorhusbefore adoption698a1b8

lines 81–84why, one step back
81test('IPC output is kept when the "verbose" function throws, "ipc"', async () => {
82 const {cause, ipcOutput} = await assertRejects(execa('ipc-send-twice.js', {ipc: true, ...getThrowingVerbose('ipc')}));83 assert.equal(cause.message, foobarString);84 assert.deepEqual(ipcOutput, ['foo', 'bar']);

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

lines 85–87why, one step back
85});
8687// A `verbose` function might throw a non-Error, or a falsy value. Neither must be mistaken for no error at all

TweaksSindre Sorhusbefore adoption698a1b8

lines 88–91why, one step back
88test('A non-Error thrown by the "verbose" function is propagated, "output"', async () => {
89 const {cause, stdout} = await assertRejects(execa('noop.js', [foobarString], getThrowingVerbose('output', foobarString)));90 assert.equal(cause, foobarString);91 assert.equal(stdout, undefined);

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

lines 92–93why, one step back
92});
93

TweaksSindre Sorhusbefore adoption698a1b8

lines 94–97why, one step back
94test('A null value thrown by the "verbose" function is propagated, "output"', async () => {
95 const {cause, stdout} = await assertRejects(execa('noop.js', [foobarString], getThrowingVerbose('output', null)));96 assert.equal(cause, null);97 assert.equal(stdout, undefined);

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

lines 98–99why, one step back
98});
99

TweaksSindre Sorhusbefore adoption698a1b8

lines 100–103why, one step back
100test('IPC output is kept when the "verbose" function throws a falsy value, "ipc"', async () => {
101 const {cause, ipcOutput} = await assertRejects(execa('ipc-send-twice.js', {ipc: true, ...getThrowingVerbose('ipc', 0)}));102 assert.equal(cause, 0);103 assert.deepEqual(ipcOutput, ['foo', 'bar']);

Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b

line 104why, one step back
104});