Skip to content

Commit 96c0089

Browse files
committed
mirage: Fix "version not found" errors to 404 status code
1 parent ab74b76 commit 96c0089

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

mirage/route-handlers/crates.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ export function register(server) {
174174

175175
let num = request.params.version_num;
176176
let version = schema.versions.findBy({ crateId: crate.id, num });
177-
if (!version) return { errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] };
177+
if (!version)
178+
return new Response(
179+
404,
180+
{},
181+
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
182+
);
178183

179184
return { meta: { names: [] }, users: [] };
180185
});
@@ -186,7 +191,12 @@ export function register(server) {
186191

187192
let num = request.params.version_num;
188193
let version = schema.versions.findBy({ crateId: crate.id, num });
189-
if (!version) return { errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] };
194+
if (!version)
195+
return new Response(
196+
404,
197+
{},
198+
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
199+
);
190200

191201
return schema.dependencies.where({ versionId: version.id });
192202
});
@@ -196,9 +206,14 @@ export function register(server) {
196206
let crate = schema.crates.findBy({ name });
197207
if (!crate) return notFound();
198208

199-
let versionNum = request.params.version_num;
200-
let version = schema.versions.findBy({ crateId: crate.id, num: versionNum });
201-
if (!version) return { errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${versionNum}\`` }] };
209+
let num = request.params.version_num;
210+
let version = schema.versions.findBy({ crateId: crate.id, num });
211+
if (!version)
212+
return new Response(
213+
404,
214+
{},
215+
{ errors: [{ detail: `crate \`${crate.name}\` does not have a version \`${num}\`` }] },
216+
);
202217

203218
return schema.versionDownloads.where({ versionId: version.id });
204219
});

tests/mirage/crates/versions/authors-test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ module('Mirage | GET /api/v1/crates/:id/:version/authors', function (hooks) {
1515
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
1616
});
1717

18-
test('returns 200 for unknown versions', async function (assert) {
18+
test('returns 404 for unknown versions', async function (assert) {
1919
this.server.create('crate', { name: 'rand' });
2020

2121
let response = await fetch('/api/v1/crates/rand/1.0.0/authors');
22-
// we should probably return 404 for this, but the production API
23-
// currently doesn't do this either
24-
assert.strictEqual(response.status, 200);
22+
assert.strictEqual(response.status, 404);
2523
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `rand` does not have a version `1.0.0`' }] });
2624
});
2725

tests/mirage/crates/versions/dependencies-test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ module('Mirage | GET /api/v1/crates/:id/:version/dependencies', function (hooks)
1515
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
1616
});
1717

18-
test('returns 200 for unknown versions', async function (assert) {
18+
test('returns 404 for unknown versions', async function (assert) {
1919
this.server.create('crate', { name: 'rand' });
2020

2121
let response = await fetch('/api/v1/crates/rand/1.0.0/dependencies');
22-
// we should probably return 404 for this, but the production API
23-
// currently doesn't do this either
24-
assert.strictEqual(response.status, 200);
22+
assert.strictEqual(response.status, 404);
2523
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `rand` does not have a version `1.0.0`' }] });
2624
});
2725

tests/mirage/crates/versions/downloads-test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ module('Mirage | GET /api/v1/crates/:id/:version/downloads', function (hooks) {
1515
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
1616
});
1717

18-
test('returns 200 for unknown versions', async function (assert) {
18+
test('returns 404 for unknown versions', async function (assert) {
1919
this.server.create('crate', { name: 'rand' });
2020

2121
let response = await fetch('/api/v1/crates/rand/1.0.0/downloads');
22-
// we should probably return 404 for this, but the production API
23-
// currently doesn't do this either
24-
assert.strictEqual(response.status, 200);
22+
assert.strictEqual(response.status, 404);
2523
assert.deepEqual(await response.json(), { errors: [{ detail: 'crate `rand` does not have a version `1.0.0`' }] });
2624
});
2725

0 commit comments

Comments
 (0)