Skip to content

Commit d2ea61a

Browse files
jorgemoyamatthewvolk
authored andcommitted
fix: show readable error when project creation errors with 422 (#2938)
1 parent ace36eb commit d2ea61a

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

packages/catalyst/src/cli/commands/project.spec.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ describe('project create', () => {
181181

182182
promptMock.mockRestore();
183183
});
184+
185+
test('propagates 422 validation error', async () => {
186+
server.use(
187+
http.post('https://:apiHost/stores/:storeHash/v3/infrastructure/projects', () =>
188+
HttpResponse.json({}, { status: 422 }),
189+
),
190+
);
191+
192+
const promptMock = vi.spyOn(consola, 'prompt').mockResolvedValue('bad name');
193+
194+
await expect(
195+
program.parseAsync([
196+
'node',
197+
'catalyst',
198+
'project',
199+
'create',
200+
'--store-hash',
201+
storeHash,
202+
'--access-token',
203+
accessToken,
204+
]),
205+
).rejects.toThrow(
206+
"The project name you entered doesn't meet the requirements. It must be 3–32 characters long and use only letters, numbers, hyphens (-), underscores (_), and periods (.)",
207+
);
208+
209+
promptMock.mockRestore();
210+
});
184211
});
185212

186213
describe('project list', () => {
@@ -428,6 +455,62 @@ describe('project link', () => {
428455
consolaPromptMock.mockRestore();
429456
});
430457

458+
test('errors when create project returns 422 validation error', async () => {
459+
server.use(
460+
http.post('https://:apiHost/stores/:storeHash/v3/infrastructure/projects', () =>
461+
HttpResponse.json({}, { status: 422 }),
462+
),
463+
);
464+
465+
const consolaPromptMock = vi
466+
.spyOn(consola, 'prompt')
467+
.mockImplementationOnce(async (message, opts) => {
468+
expect(message).toContain(
469+
'Select a project or create a new project (Press <enter> to select).',
470+
);
471+
472+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
473+
const options = (opts as { options: Array<{ label: string; value: string }> }).options;
474+
475+
expect(options).toHaveLength(3);
476+
expect(options[0]).toMatchObject({ label: 'Project One', value: projectUuid1 });
477+
expect(options[1]).toMatchObject({
478+
label: 'Project Two',
479+
value: projectUuid2,
480+
});
481+
expect(options[2]).toMatchObject({ label: 'Create a new project', value: 'create' });
482+
483+
return new Promise((resolve) => resolve('create'));
484+
})
485+
.mockImplementationOnce(async (message) => {
486+
expect(message).toBe('Enter a name for the new project:');
487+
488+
return new Promise((resolve) => resolve('bad name'));
489+
});
490+
491+
await expect(
492+
program.parseAsync([
493+
'node',
494+
'catalyst',
495+
'project',
496+
'link',
497+
'--store-hash',
498+
storeHash,
499+
'--access-token',
500+
accessToken,
501+
]),
502+
).rejects.toThrow(
503+
"The project name you entered doesn't meet the requirements. It must be 3–32 characters long and use only letters, numbers, hyphens (-), underscores (_), and periods (.)",
504+
);
505+
506+
expect(mockIdentify).toHaveBeenCalledWith(storeHash);
507+
508+
expect(consola.start).toHaveBeenCalledWith('Fetching projects...');
509+
expect(consola.success).toHaveBeenCalledWith('Projects fetched.');
510+
511+
consolaPromptMock.mockRestore();
512+
});
513+
431514
test('errors when infrastructure projects API is not found', async () => {
432515
server.use(
433516
http.get('https://:apiHost/stores/:storeHash/v3/infrastructure/projects', () =>

packages/catalyst/src/cli/lib/project.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ export async function createProject(
9595
);
9696
}
9797

98+
if (response.status === 422) {
99+
throw new Error(
100+
"The project name you entered doesn't meet the requirements. It must be 3–32 characters long and use only letters, numbers, hyphens (-), underscores (_), and periods (.)",
101+
);
102+
}
103+
98104
if (!response.ok) {
99105
throw new Error(`Failed to create project: ${response.statusText}`);
100106
}

0 commit comments

Comments
 (0)