Skip to content

Commit 932e6a5

Browse files
committed
Fix InitError logic in ChildProcess kernel code
1 parent 8a25bc5 commit 932e6a5

3 files changed

Lines changed: 116 additions & 61 deletions

File tree

integration-tests/child-process/src/Main.gren

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ init env =
6060
|> Task.executeCmd
6161
}
6262

63+
Just "NotFound" ->
64+
{ model = {}
65+
, command =
66+
{ (ChildProcess.defaultRunOptions) | shell = ChildProcess.NoShell }
67+
|> ChildProcess.run cpPerm "foobar3" []
68+
|> Task.andThen (\result -> Stream.Log.bytes env.stdout result.stdout)
69+
|> Task.onError
70+
(\err ->
71+
when err is
72+
ChildProcess.InitError inerr ->
73+
if inerr.errorCode == "ENOENT" then
74+
Stream.Log.line env.stdout "Process Not Found"
75+
else
76+
Stream.Log.line env.stdout inerr.errorCode
77+
78+
ChildProcess.ProgramError _ ->
79+
Stream.Log.line env.stdout "Program Error"
80+
)
81+
|> Task.execute
82+
}
83+
6384
_ ->
6485
{ model = {}
6586
, command =

integration-tests/child-process/test/requests.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ describe("ChildProcess", () => {
1919
.stdout(process.version)
2020
.notStderr(/DeprecationWarning/);
2121
});
22+
23+
it("Program not found", async () => {
24+
await runner()
25+
.cwd(baseDir)
26+
.fork("app", ["NotFound"], {})
27+
.stdout("Process Not Found");
28+
});
2229
});

src/Gren/Kernel/ChildProcess.js

Lines changed: 88 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,69 +22,83 @@ var _ChildProcess_run = function (options) {
2222
var workingDir = options.__$workingDirectory;
2323
var env = options.__$environmentVariables;
2424
var shell = options.__$shell;
25-
var cmd = [options.__$program].concat(options.__$arguments).join(" ");
26-
27-
var subProc = childProcess.exec(
28-
cmd,
29-
{
30-
encoding: "buffer",
31-
timeout: options.__$runDuration,
32-
cwd: _ChildProcess_handleCwd(workingDir),
33-
env: _ChildProcess_handleEnv(env),
34-
timeout: options.__$runDuration,
35-
maxBuffer: options.__$maximumBytesWrittenToStreams,
36-
shell: _ChildProcess_handleShell(shell),
37-
},
38-
function (err, stdout, stderr) {
39-
if (err == null) {
25+
26+
var cmdOptions = {
27+
encoding: "buffer",
28+
timeout: options.__$runDuration,
29+
cwd: _ChildProcess_handleCwd(workingDir),
30+
env: _ChildProcess_handleEnv(env),
31+
timeout: options.__$runDuration,
32+
maxBuffer: options.__$maximumBytesWrittenToStreams,
33+
shell: _ChildProcess_handleShell(shell),
34+
};
35+
36+
function cmdCallback(err, stdout, stderr) {
37+
if (err == null) {
38+
callback(
39+
__Scheduler_succeed({
40+
__$stdout: new DataView(
41+
stdout.buffer,
42+
stdout.byteOffset,
43+
stdout.byteLength,
44+
),
45+
__$stderr: new DataView(
46+
stderr.buffer,
47+
stderr.byteOffset,
48+
stderr.byteLength,
49+
),
50+
}),
51+
);
52+
} else {
53+
if (typeof err.errno === "undefined") {
54+
// errno only exists on system errors, the program was run
4055
callback(
41-
__Scheduler_succeed({
42-
__$stdout: new DataView(
43-
stdout.buffer,
44-
stdout.byteOffset,
45-
stdout.byteLength,
46-
),
47-
__$stderr: new DataView(
48-
stderr.buffer,
49-
stderr.byteOffset,
50-
stderr.byteLength,
51-
),
52-
}),
56+
__Scheduler_fail(
57+
__ChildProcess_ProgramError({
58+
__$exitCode: err.code,
59+
__$stdout: new DataView(
60+
stdout.buffer,
61+
stdout.byteOffset,
62+
stdout.byteLength,
63+
),
64+
__$stderr: new DataView(
65+
stderr.buffer,
66+
stderr.byteOffset,
67+
stderr.byteLength,
68+
),
69+
}),
70+
),
5371
);
5472
} else {
55-
if (typeof err.errno === "undefined") {
56-
// errno only exists on system errors, the program was run
57-
callback(
58-
__Scheduler_fail(
59-
__ChildProcess_ProgramError({
60-
__$exitCode: err.code,
61-
__$stdout: new DataView(
62-
stdout.buffer,
63-
stdout.byteOffset,
64-
stdout.byteLength,
65-
),
66-
__$stderr: new DataView(
67-
stderr.buffer,
68-
stderr.byteOffset,
69-
stderr.byteLength,
70-
),
71-
}),
72-
),
73-
);
74-
} else {
75-
callback(
76-
__Scheduler_fail(
77-
__ChildProcess_InitError({
78-
__program: err.path,
79-
__arguments: err.spawnargs,
80-
__errorCode: err.code,
81-
}),
82-
),
83-
);
84-
}
73+
callback(
74+
__Scheduler_fail(
75+
__ChildProcess_InitError({
76+
__$program: err.path,
77+
__$arguments: err.spawnargs,
78+
__$errorCode: err.code,
79+
}),
80+
),
81+
);
8582
}
86-
},
87-
);
83+
}
84+
}
85+
86+
var subProc;
87+
88+
if (cmdOptions.shell) {
89+
subProc = childProcess.execFile(
90+
[options.__$program].concat(options.__$arguments).join(" "),
91+
cmdOptions,
92+
cmdCallback,
93+
);
94+
} else {
95+
subProc = childProcess.execFile(
96+
options.__$program,
97+
options.__$arguments,
98+
cmdOptions,
99+
cmdCallback,
100+
);
101+
}
88102

89103
return () => {
90104
subProc.kill();
@@ -94,12 +108,25 @@ var _ChildProcess_run = function (options) {
94108

95109
var _ChildProcess_spawn = F3(function (sendInitToApp, sendExitToApp, options) {
96110
return __Scheduler_binding(function (callback) {
97-
var subproc = _ChildProcess_getSubProc(options);
111+
var subproc;
112+
try {
113+
subproc = _ChildProcess_getSubProc(options);
114+
} catch (e) {
115+
callback(
116+
__Scheduler_succeed(
117+
__Scheduler_rawSpawn(
118+
sendExitToApp(typeof code.errno === "undefined" ? -1 : code.errno),
119+
),
120+
),
121+
);
122+
123+
return;
124+
}
98125

99126
var proc = __Scheduler_rawSpawn(
100127
sendInitToApp({
101128
__$processId: __Scheduler_rawSpawn(
102-
__Scheduler_binding(function (callback) {
129+
__Scheduler_binding(function () {
103130
return function () {
104131
subproc.kill();
105132
};

0 commit comments

Comments
 (0)