Skip to content

Commit 1b6c0ee

Browse files
authored
fix: Unify error objects with GitHub errors. (#75)
1 parent 4dc7c28 commit 1b6c0ee

File tree

3 files changed

+11
-20
lines changed

3 files changed

+11
-20
lines changed

index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ function checkResponseError(response) {
4747
const errorMessage = hoek.reach(response, 'body.error.message', {
4848
default: `SCM service unavailable (${response.statusCode}).`
4949
});
50-
const errorReason = hoek.reach(response, 'body.error.detail.required', {
50+
const errorReason = hoek.reach(response, 'body.error.detail', {
5151
default: JSON.stringify(response.body)
5252
});
5353

5454
const error = new Error(`${errorMessage} Reason "${errorReason}"`);
5555

56-
error.code = response.statusCode;
56+
error.status = response.statusCode;
5757
throw error;
5858
}
5959

@@ -527,10 +527,7 @@ class BitbucketScm extends Scm {
527527

528528
const response = await this.breaker.runCommand(options);
529529

530-
if (response.statusCode !== 200) {
531-
throw new Error(
532-
`STATUS CODE ${response.statusCode}: ${JSON.stringify(response.body)}`);
533-
}
530+
checkResponseError(response);
534531

535532
return response.body.target.hash;
536533
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"mocha": "^8.2.1",
4343
"mocha-multi-reporters": "^1.5.1",
4444
"mocha-sonarqube-reporter": "^1.0.2",
45-
"nyc": "^15.0.0",
4645
"mockery": "^2.0.0",
46+
"nyc": "^15.0.0",
4747
"sinon": "^4.5.0"
4848
},
4949
"dependencies": {

test/index.test.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,9 @@ describe('index', function () {
857857
assert.fail('Should not get here');
858858
}).catch((error) => {
859859
assert.calledWith(requestMock, expectedOptions);
860-
assert.match(error.message, 'STATUS CODE 404');
860+
assert.match(error.message, 'Resource not found Reason ' +
861+
'"There is no API hosted at this URL"');
862+
assert.match(error.status, 404);
861863
});
862864
});
863865

@@ -1744,10 +1746,7 @@ describe('index', function () {
17441746
type: 'error',
17451747
error: {
17461748
message: 'Your credentials lack one or more required privilege scopes.',
1747-
detail: {
1748-
granted: ['repository'],
1749-
required: ['webhook']
1750-
}
1749+
detail: 'webhook'
17511750
}
17521751
};
17531752

@@ -1771,6 +1770,7 @@ describe('index', function () {
17711770
]
17721771
}).then(assert.fail, (err) => {
17731772
assert.strictEqual(err.message, expectedMessage);
1773+
assert.strictEqual(err.status, 403);
17741774
});
17751775
});
17761776

@@ -1806,10 +1806,7 @@ describe('index', function () {
18061806
type: 'error',
18071807
error: {
18081808
message: 'Your credentials lack one or more required privilege scopes.',
1809-
detail: {
1810-
granted: ['repository'],
1811-
required: ['webhook']
1812-
}
1809+
detail: 'webhook'
18131810
}
18141811
};
18151812

@@ -1855,10 +1852,7 @@ describe('index', function () {
18551852
type: 'error',
18561853
error: {
18571854
message: 'Your credentials lack one or more required privilege scopes.',
1858-
detail: {
1859-
granted: ['repository'],
1860-
required: ['webhook']
1861-
}
1855+
detail: 'webhook'
18621856
}
18631857
};
18641858

0 commit comments

Comments
 (0)