parse GHCR error format for errors

This commit is contained in:
Conor Sloan
2024-08-08 14:07:54 +01:00
parent 2bbf08d922
commit bebbbc6eee
4 changed files with 137 additions and 43 deletions
Generated Vendored
+38 -9
View File
@@ -104749,8 +104749,7 @@ async function uploadLayer(layer, file, registryURL, checkBlobEndpoint, uploadBl
return;
}
if (checkExistsResponse.status !== 404) {
const responseBody = await checkExistsResponse.text();
throw new Error(`Unexpected response from blob check for layer ${layer.digest}: ${checkExistsResponse.status}. Response Body: ${responseBody}.`);
throw new Error(await errorMessageForFailedRequest(`check blob (${layer.digest}) exists`, checkExistsResponse));
}
core.info(`Uploading layer ${layer.digest}.`);
const initiateUploadResponse = await fetchWithDebug(uploadBlobEndpoint, {
@@ -104761,9 +104760,7 @@ async function uploadLayer(layer, file, registryURL, checkBlobEndpoint, uploadBl
body: JSON.stringify(layer)
});
if (initiateUploadResponse.status !== 202) {
const responseBody = await initiateUploadResponse.text();
core.error(`Unexpected response from upload post ${uploadBlobEndpoint}: ${initiateUploadResponse.status}. Response Body: ${responseBody}.`);
throw new Error(`Unexpected response from POST upload ${initiateUploadResponse.status}. Response Body: ${responseBody}.`);
throw new Error(await errorMessageForFailedRequest(`initiate layer upload`, initiateUploadResponse));
}
const locationResponseHeader = initiateUploadResponse.headers.get('location');
if (locationResponseHeader === undefined) {
@@ -104790,8 +104787,7 @@ async function uploadLayer(layer, file, registryURL, checkBlobEndpoint, uploadBl
body: data
});
if (putResponse.status !== 201) {
const responseBody = await putResponse.text();
throw new Error(`Unexpected response from PUT upload ${putResponse.status} for layer ${layer.digest}. Response Body: ${responseBody}.`);
throw new Error(await errorMessageForFailedRequest(`layer (${layer.digest}) upload`, putResponse));
}
}
// Uploads the manifest and returns the digest returned by GHCR
@@ -104806,8 +104802,7 @@ async function uploadManifest(manifestJSON, manifestEndpoint, b64Token) {
body: manifestJSON
});
if (putResponse.status !== 201) {
const responseBody = await putResponse.text();
throw new Error(`Unexpected response from PUT manifest ${putResponse.status}. Response Body: ${responseBody}.`);
throw new Error(await errorMessageForFailedRequest(`manifest upload`, putResponse));
}
const digestResponseHeader = putResponse.headers.get('docker-content-digest');
if (digestResponseHeader === undefined || digestResponseHeader === null) {
@@ -104815,6 +104810,40 @@ async function uploadManifest(manifestJSON, manifestEndpoint, b64Token) {
}
return digestResponseHeader;
}
// Generate an error message for a failed HTTP request
async function errorMessageForFailedRequest(requestDescription, response) {
const bodyText = await response.text();
// Try to parse the body as JSON and extract the expected fields returned from GHCR
// Expected format: { "errors": [{"code": "BAD_REQUEST", "message": "Something went wrong."}] }
// If the body does not match the expected format, just return the whole response body
let errorString = `Response Body: ${bodyText}.`;
try {
const body = JSON.parse(bodyText);
const errors = body.errors;
if (Array.isArray(errors) &&
errors.length > 0 &&
errors.every(isGHCRError)) {
const errorMessages = errors.map((error) => {
return `${error.code} - ${error.message}`;
});
errorString = `Errors: ${errorMessages.join(', ')}`;
}
}
catch (error) {
// Ignore error
}
return `Unexpected ${response.status} ${response.statusText} response from ${requestDescription}. ${errorString}`;
}
// Runtime checks that parsed JSON object is in the expected format
// {"code": "BAD_REQUEST", "message": "Something went wrong."}
function isGHCRError(obj) {
return (typeof obj === 'object' &&
obj !== null &&
'code' in obj &&
typeof obj.code === 'string' &&
'message' in obj &&
typeof obj.message === 'string');
}
const fetchWithDebug = async (url, config = {}) => {
core.debug(`Request from ${url} with config: ${JSON.stringify(config)}`);
try {