Skip to content

Commit cefdb7b

Browse files
committed
Fix edge case reporting issue
1 parent 042b661 commit cefdb7b

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎packages/create-apostrophe/src/core/steps/admin-user.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,22 @@ export async function addAdminUser(
5757
input: `${password ?? ''}\n`
5858
}
5959
);
60-
if (!cp.error && cp.code === 0) {
60+
if (cp.error) {
61+
throw new StageError(STAGE, {
62+
code: cp.error.code === 'ENOENT' ? 'node_missing' : 'node_spawn_failed',
63+
cause: cp.error
64+
});
65+
}
66+
if (cp.code === 0) {
6167
return 'updated';
6268
}
69+
throw new StageError(STAGE, {
70+
code: 'admin_user_failed',
71+
cause: new Error(
72+
`@apostrophecms/user:change-password exited with code ${cp.code} ` +
73+
'(recovery from duplicate @apostrophecms/user:add)'
74+
)
75+
});
6376
}
6477

6578
throw new StageError(STAGE, {

‎packages/create-apostrophe/test/core/admin-user.test.js‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('core/steps/admin-user', function () {
128128
assert.deepEqual(calls[0].args[1], '@apostrophecms/user:add');
129129
});
130130

131-
it('fallback fails too → original admin_user_failed propagates', async function () {
131+
it('fallback non-zero exit → admin_user_failed attributed to change-password', async function () {
132132
const run = async (command, args) => ({
133133
code: 1,
134134
stdout: '',
@@ -146,6 +146,42 @@ describe('core/steps/admin-user', function () {
146146
}, { run }),
147147
(err) => {
148148
assert.equal(err.errorCode, 'admin_user_failed');
149+
// The cause must point at the failed recovery, not the duplicate
150+
// user:add that triggered it.
151+
assert.match(err.cause.message, /user:change-password exited with code 1/);
152+
return true;
153+
}
154+
);
155+
});
156+
157+
it('fallback spawn error (ENOENT) → node_missing, not admin_user_failed', async function () {
158+
const run = async (command, args) => {
159+
if (args[1] === '@apostrophecms/user:add') {
160+
return {
161+
code: 1,
162+
stdout: '',
163+
stderr: 'E11000 duplicate key error index: username_1',
164+
error: null
165+
};
166+
}
167+
const err = new Error('spawn ENOENT');
168+
err.code = 'ENOENT';
169+
return {
170+
code: null,
171+
stdout: '',
172+
stderr: '',
173+
error: err
174+
};
175+
};
176+
177+
await assert.rejects(
178+
() => addAdminUser({
179+
appRoot: '/p',
180+
username: 'admin',
181+
password: 'x'
182+
}, { run }),
183+
(err) => {
184+
assert.equal(err.errorCode, 'node_missing');
149185
return true;
150186
}
151187
);

0 commit comments

Comments
 (0)