Skip to content

Commit

Permalink
Match 404 object errors to API for non-GET endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rhodgkins committed Aug 9, 2021
1 parent e821aa8 commit 3bc428a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
27 changes: 24 additions & 3 deletions scripts/storage-emulator-integration/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ describe("Storage emulator", () => {
const [existsAfter] = await toDeleteFile.exists();
expect(existsAfter).to.equal(false);
});

it("should throw 404 object error for file not found", async () => {
await expect(testBucket.file("blah").delete())
.to.be.eventually.rejectedWith(`No such object: ${storageBucket}/blah`)
.and.nested.include({
code: 404,
"errors[0].reason": "notFound",
});
});
});

describe("#download()", () => {
Expand All @@ -334,9 +343,12 @@ describe("Storage emulator", () => {
});

it("should throw 404 error for file not found", async () => {
await expect(testBucket.file("blah").download())
.to.be.eventually.rejectedWith(`No such object: ${storageBucket}/blah`)
.and.have.property("code", 404);
const err = (await expect(
testBucket.file("blah").download()
).to.be.eventually.rejectedWith(`No such object: ${storageBucket}/blah`)) as Error;

expect(err).to.have.property("code", 404);
expect(err).not.have.nested.property("errors[0]");
});
});

Expand Down Expand Up @@ -629,6 +641,15 @@ describe("Storage emulator", () => {
md.metadata.firebaseStorageDownloadTokens
);
});

it("should throw 404 object error for file not found", async () => {
await expect(testBucket.file("blah").getMetadata())
.to.be.eventually.rejectedWith(`No such object: ${storageBucket}/blah`)
.and.nested.include({
code: 404,
"errors[0].reason": "notFound",
});
});
});

describe("#setMetadata()", () => {
Expand Down
20 changes: 19 additions & 1 deletion src/emulator/storage/apis/gcloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,23 @@ function sendFileBytes(

/** Sends 404 matching API */
function sendObjectNotFound(req: Request, res: Response): void {
res.status(404).send(`No such object: ${req.params.bucketId}/${req.params.objectId}`);
res.status(404);
const message = `No such object: ${req.params.bucketId}/${req.params.objectId}`;
if (req.method === "GET" && req.query.alt == "media") {
res.send(message);
} else {
res.json({
error: {
code: 404,
message,
errors: [
{
message,
domain: "global",
reason: "notFound",
},
],
},
});
}
}

0 comments on commit 3bc428a

Please sign in to comment.