Skip to content

Commit 2a5bbcf

Browse files
pvdlggr2m
authored andcommitted
fix: remove HttpError sub-classes
1 parent ad6b97b commit 2a5bbcf

File tree

5 files changed

+18
-28
lines changed

5 files changed

+18
-28
lines changed

lib/plugins/endpoint-methods/validate.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module.exports = validate
44

55
const set = require('lodash/set')
6-
const errors = require('../../request/errors')
6+
const HttpError = require('../../request/http-error')
77

88
function validate (endpointParams, options) {
99
Object.keys(endpointParams).forEach(parameterName => {
@@ -24,34 +24,34 @@ function validate (endpointParams, options) {
2424

2525
if ((parameter.required && !paramIsPresent) ||
2626
(parameter['allow-null'] === false && paramIsNull)) {
27-
throw new errors.BadRequest(`Empty value for parameter '${parameterName}': ${value}`)
27+
throw new HttpError(`Empty value for parameter '${parameterName}': ${value}`, 400)
2828
}
2929

3030
if (parameter.enum) {
3131
if (parameter.enum.indexOf(value) === -1) {
32-
throw new errors.BadRequest(`Invalid value for parameter '${parameterName}': ${value}`)
32+
throw new HttpError(`Invalid value for parameter '${parameterName}': ${value}`, 400)
3333
}
3434
}
3535

3636
if (parameter.validation) {
3737
const regex = new RegExp(parameter.validation)
3838
if (!regex.test(value)) {
39-
throw new errors.BadRequest(`Invalid value for parameter '${parameterName}': ${value}`)
39+
throw new HttpError(`Invalid value for parameter '${parameterName}': ${value}`, 400)
4040
}
4141
}
4242

4343
if (expectedType === 'number') {
4444
value = parseInt(value, 10)
4545
if (isNaN(value)) {
46-
throw new errors.BadRequest(`Invalid value for parameter '${parameterName}': ${options[parameterName]} is NaN`)
46+
throw new HttpError(`Invalid value for parameter '${parameterName}': ${options[parameterName]} is NaN`, 400)
4747
}
4848
}
4949

5050
if (expectedType === 'json' && typeof value === 'string') {
5151
try {
5252
value = JSON.parse(value)
5353
} catch (exception) {
54-
throw new errors.BadRequest(`JSON parse error of value for parameter '${parameterName}': ${value}`)
54+
throw new HttpError(`JSON parse error of value for parameter '${parameterName}': ${value}`, 400)
5555
}
5656
}
5757

lib/plugins/pagination/get-page.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = getPage
22

3-
const errors = require('../../request/errors')
3+
const HttpError = require('../../request/http-error')
44
const getPageLinks = require('./get-page-links')
55

66
function getPage (octokit, link, which, headers, callback) {
@@ -12,7 +12,7 @@ function getPage (octokit, link, which, headers, callback) {
1212
const url = getPageLinks(link)[which]
1313

1414
if (!url) {
15-
const urlError = new errors.NotFound(`No ${which} page found`)
15+
const urlError = new HttpError(`No ${which} page found`, 404)
1616
if (callback) {
1717
return callback(urlError)
1818
}

lib/request/errors.js lib/request/http-error.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const STATUS_CODES = {
88
504: 'Gateway Timeout'
99
}
1010

11-
module.exports.HttpError = class extends Error {
11+
module.exports = class HttpError extends Error {
1212
constructor (message, code, headers) {
1313
super(message)
1414
Error.captureStackTrace(this, this.constructor)
15-
this.name = STATUS_CODES[code] ? STATUS_CODES[code].replace(/\s/g, '') : 'HttpError'
15+
this.name = 'HttpError'
1616
this.code = code
1717
this.status = STATUS_CODES[code]
1818
this.headers = headers
@@ -30,13 +30,3 @@ module.exports.HttpError = class extends Error {
3030
}
3131
}
3232
}
33-
34-
Object.keys(STATUS_CODES).forEach(status => {
35-
const className = STATUS_CODES[status].replace(/\s/g, '')
36-
module.exports[className] = class extends module.exports.HttpError {
37-
constructor (message) {
38-
super(message, parseInt(status, 10))
39-
this.name = className
40-
}
41-
}
42-
})

lib/request/request.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const debug = require('debug')('octokit:rest')
1111
const isArrayBuffer = require('is-array-buffer')
1212
const isStream = require('is-stream')
1313

14-
const errors = require('./errors')
14+
const HttpError = require('./http-error')
1515

1616
function httpRequest (requestOptions) {
1717
requestOptions = Object.assign(
@@ -60,7 +60,7 @@ function httpRequest (requestOptions) {
6060

6161
/* istanbul ignore next */
6262
response.on('error', (error) => {
63-
reject(new errors.InternalServerError(error.message))
63+
reject(new HttpError(error.message, 500))
6464
})
6565
response.on('end', () => {
6666
if (response.statusCode !== 304 && response.statusCode >= 301 && response.statusCode <= 307) {
@@ -70,7 +70,7 @@ function httpRequest (requestOptions) {
7070
}
7171

7272
if (response.statusCode === 304 || response.statusCode >= 400 || response.statusCode < 10) {
73-
reject(new errors.HttpError(data, response.statusCode, response.headers))
73+
reject(new HttpError(data, response.statusCode, response.headers))
7474
return
7575
}
7676

@@ -90,7 +90,7 @@ function httpRequest (requestOptions) {
9090
request.on('error', (error) => {
9191
if (aborted) return
9292
debug('REQUEST ERROR: ' + error.message)
93-
reject(new errors.InternalServerError(error.message))
93+
reject(new HttpError(error.message, 500))
9494
})
9595

9696
if (requestOptions.timeout) {
@@ -100,7 +100,7 @@ function httpRequest (requestOptions) {
100100
debug('REQUEST ERROR: timed out')
101101
request.abort()
102102
aborted = true
103-
reject(new errors.GatewayTimeout('Request timeout'))
103+
reject(new HttpError('Request timeout', 504))
104104
})
105105

106106
if (requestOptions.body) {

test/integration/request-errors-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('request errors', () => {
2323
return github.orgs.get({org: 'myorg'})
2424

2525
.catch(error => {
26-
error.name.should.equal('GatewayTimeout')
26+
error.name.should.equal('HttpError')
2727
error.code.should.equal(504)
2828
error.should.have.property('stack')
2929
})
@@ -42,7 +42,7 @@ describe('request errors', () => {
4242
return github.orgs.get({org: 'myorg'})
4343

4444
.catch(error => {
45-
error.name.should.equal('InternalServerError')
45+
error.name.should.equal('HttpError')
4646
error.code.should.equal(500)
4747
error.should.have.property('stack')
4848
})
@@ -61,7 +61,7 @@ describe('request errors', () => {
6161
return github.orgs.get({org: 'myorg'})
6262

6363
.catch(error => {
64-
error.name.should.equal('NotFound')
64+
error.name.should.equal('HttpError')
6565
error.code.should.equal(404)
6666
error.should.have.property('stack')
6767
})

0 commit comments

Comments
 (0)