Skip to content

Commit 0199a01

Browse files
authored
fix: throws statusCode on parseUrl (#82)
1 parent 34bf2c9 commit 0199a01

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ const STATE_MAP = {
2828
};
2929
const WEBHOOK_PAGE_SIZE = 30;
3030

31+
/**
32+
* Throw error with error code
33+
* @param {String} errorReason Error message
34+
* @param {Number} errorCode Error code
35+
* @throws {Error} Throws error
36+
*/
37+
function throwError(errorReason, errorCode = 500) {
38+
const err = new Error(errorReason);
39+
40+
err.statusCode = errorCode;
41+
throw err;
42+
}
43+
3144
/**
3245
* Get repo information
3346
* @method getRepoInfo
@@ -328,16 +341,13 @@ class BitbucketScm extends Scm {
328341
};
329342

330343
if (hostname !== this.hostname) {
331-
throw new Error('This checkoutUrl is not supported for your current login host.');
344+
throwError('This checkoutUrl is not supported for your current login host.', 400);
332345
}
333346

334347
const response = await this.breaker.runCommand(options);
335348

336-
if (response.statusCode === 404) {
337-
throw new Error(`Cannot find repository ${checkoutUrl}`);
338-
}
339349
if (response.statusCode !== 200) {
340-
throw new Error(`STATUS CODE ${response.statusCode}: ${JSON.stringify(response.body)}`);
350+
throwError(`STATUS CODE ${response.statusCode}: ${JSON.stringify(response.body)}`, response.statusCode);
341351
}
342352

343353
const scmUri = `${hostname}:${username}/${response.body.target.repository.uuid}:${branch}`;

test/index.test.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,10 @@ describe('index', function() {
267267
});
268268

269269
it('rejects if status code is 404', () => {
270-
fakeResponse = {
271-
statusCode: 404,
272-
body: {
273-
error: {
274-
message: 'Resource not found',
275-
detail: 'There is no API hosted at this URL'
276-
}
277-
}
278-
};
270+
const err = new Error('Cannot find repository');
279271

280-
requestMock.resolves(fakeResponse);
272+
err.statusCode = 404;
273+
requestMock.rejects(err);
281274

282275
return scm
283276
.parseUrl({

0 commit comments

Comments
 (0)