Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return a response that docker understands better when it doesn't find a manifest #32

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ describe("v2 manifests", () => {
{
code: "MANIFEST_UNKNOWN",
message: "manifest unknown",
detail: "This error is returned when the manifest, identified by name and tag is unknown to the repository.",
detail: {
Tag: "reference",
},
},
],
});
Expand Down
10 changes: 6 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "itty-router";
import { BlobUnknownError, ManifestUnknownError } from "./v2-errors";
import { InternalError, ServerError } from "./errors";
import { errorString, wrap } from "./utils";
import { errorString, jsonHeaders, wrap } from "./utils";
import { hexToDigest } from "./user";
import { ManifestTagsListTooBigError } from "./v2-responses";
import { Env } from "..";
Expand Down Expand Up @@ -66,7 +66,7 @@ v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => {
// Reference is ALWAYS a sha256
const manifest = await env.REGISTRY.head(`${name}/manifests/${reference}`);
if (manifest === null) {
return new Response(JSON.stringify(ManifestUnknownError), { status: 404 });
return new Response(JSON.stringify(ManifestUnknownError(reference)), { status: 404, headers: jsonHeaders() });
}
const limitInt = parseInt(limit?.toString() ?? "1000", 10);
const tags = await env.REGISTRY.list({
Expand Down Expand Up @@ -162,7 +162,7 @@ v2Router.head("/:name+/manifests/:reference", async (req, env: Env) => {
}

if (checkManifestResponse === null || !checkManifestResponse.exists)
return new Response(JSON.stringify(ManifestUnknownError), { status: 404 });
return new Response(JSON.stringify(ManifestUnknownError(reference)), { status: 404, headers: jsonHeaders() });

return new Response(null, {
headers: {
Expand Down Expand Up @@ -221,7 +221,9 @@ v2Router.get("/:name+/manifests/:reference", async (req, env: Env, context: Exec
break;
}

if (getManifestResponse === null) return new Response(JSON.stringify(ManifestUnknownError), { status: 404 });
if (getManifestResponse === null)
return new Response(JSON.stringify(ManifestUnknownError(reference)), { status: 404, headers: jsonHeaders() });

return new Response(getManifestResponse.stream, {
headers: {
"Content-Length": getManifestResponse.size.toString(),
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ export function errorString(err: unknown): string {
export async function wrap<T, E = unknown>(fn: Promise<T>): Promise<[T, null] | [null, E]> {
return fn.then((data) => [data, null] as [T, null]).catch((err) => [null, err as unknown as E] as [null, E]);
}

export function jsonHeaders(): { "content-type": "application/json" } {
return { "content-type": "application/json" };
}
25 changes: 15 additions & 10 deletions src/v2-errors.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
export const ManifestUnknownError = {
errors: [
{
code: "MANIFEST_UNKNOWN",
message: "manifest unknown",
detail: "This error is returned when the manifest, identified by name and tag is unknown to the repository.",
},
],
};
export const ManifestUnknownError = (tag: string) =>
({
errors: [
{
code: "MANIFEST_UNKNOWN",
message: "manifest unknown",
detail: {
Tag: tag,
},
},
],
} as const);

export const BlobUnknownError = {
errors: [
{
code: "BLOB_UNKNOWN",
message: "blob unknown to registry",
detail: "This error may be returned when a manifest blob is unknown to the registry.",
detail: {
message: "This error may be returned when a layer blob is unknown to the registry.",
},
},
],
};