Skip to content

Commit

Permalink
fix: better error logging for network errors (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Friend authored Jun 22, 2021
1 parent e8b15e2 commit 25a9011
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ module.exports = async (pluginConfig, context) => {
// Uploaded assets to the project
const form = new FormData();
form.append('file', createReadStream(file));
const {url, alt} = await got
.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/uploads`), {...apiOptions, body: form})
.json();

const uploadEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/uploads`);

debug('POST-ing the file %s to %s', file, uploadEndpoint);

let response;
try {
response = await got.post(uploadEndpoint, {...apiOptions, body: form}).json();
} catch (error) {
logger.error('An error occurred while uploading %s to the GitLab project uploads API:\n%O', file, error);
throw error;
}

const {url, alt} = response;

assetsList.push({label, alt, url, type, filepath});

Expand All @@ -71,26 +82,38 @@ module.exports = async (pluginConfig, context) => {
}

debug('Create a release for git tag %o with commit %o', gitTag, gitHead);
await got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/releases`), {
...apiOptions,
json: {
/* eslint-disable camelcase */
tag_name: gitTag,
description: notes && notes.trim() ? notes : gitTag,
milestones,
assets: {
links: assetsList.map(({label, alt, url, type, filepath}) => {
return {
name: label || alt,
url: urlJoin(gitlabUrl, repoId, url),
link_type: type,
filepath,
};
}),
},
/* eslint-enable camelcase */

const createReleaseEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/releases`);

const json = {
/* eslint-disable camelcase */
tag_name: gitTag,
description: notes && notes.trim() ? notes : gitTag,
milestones,
assets: {
links: assetsList.map(({label, alt, url, type, filepath}) => {
return {
name: label || alt,
url: urlJoin(gitlabUrl, repoId, url),
link_type: type,
filepath,
};
}),
},
});
/* eslint-enable camelcase */
};

debug('POST-ing the following JSON to %s:\n%s', createReleaseEndpoint, JSON.stringify(json, null, 2));

try {
await got.post(createReleaseEndpoint, {
...apiOptions,
json,
});
} catch (error) {
logger.error('An error occurred while making a request to the GitLab release API:\n%O', error);
throw error;
}

logger.log('Published GitLab release: %s', gitTag);

Expand Down

0 comments on commit 25a9011

Please sign in to comment.