Skip to content

Commit 93c6015

Browse files
authored
Merge pull request #712 from semantic-release/feat/improve-error-output
2 parents aa59292 + e69d32e commit 93c6015

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/publish.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,25 @@ export default async (pluginConfig, context) => {
2828
const encodedRepoId = encodeURIComponent(repoId);
2929
const encodedGitTag = encodeURIComponent(gitTag);
3030
const encodedVersion = encodeURIComponent(version);
31-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
31+
const apiOptions = {
32+
headers: {
33+
"PRIVATE-TOKEN": gitlabToken,
34+
},
35+
hooks: {
36+
beforeError: [
37+
(error) => {
38+
const { response } = error;
39+
if (response?.body && response.headers["content-type"] === "application/json") {
40+
const parsedBody = JSON.parse(response.body);
41+
if (parsedBody.message) {
42+
error.message = `Response code ${response.statusCode} (${parsedBody.message})`;
43+
}
44+
}
45+
return error;
46+
},
47+
],
48+
},
49+
};
3250

3351
debug("repoId: %o", repoId);
3452
debug("release name: %o", gitTag);

test/publish.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,27 @@ test.serial("Publish a release with an asset link", async (t) => {
617617
t.deepEqual(t.context.log.args[0], ["Published GitLab release: %s", nextRelease.gitTag]);
618618
t.true(gitlab.isDone());
619619
});
620+
621+
test.serial("Publish a release with error response", async (t) => {
622+
const owner = "test_user";
623+
const repo = "test_repo";
624+
const env = { GITLAB_TOKEN: "gitlab_token" };
625+
const pluginConfig = {};
626+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
627+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
628+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
629+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
630+
const gitlab = authenticate(env)
631+
.post(`/projects/${encodedRepoId}/releases`, {
632+
tag_name: nextRelease.gitTag,
633+
description: nextRelease.notes,
634+
assets: {
635+
links: [],
636+
},
637+
})
638+
.reply(499, { message: "Something went wrong" });
639+
640+
const error = await t.throwsAsync(publish(pluginConfig, { env, options, nextRelease, logger: t.context.logger }));
641+
t.is(error.message, `Response code 499 (Something went wrong)`);
642+
t.true(gitlab.isDone());
643+
});

0 commit comments

Comments
 (0)