Skip to content

Commit b2cf073

Browse files
authored
fix: http error handling regression (#1285)
* tests: add failing test case provided in ticket * fix: throw original fetch error if we don't have any response data
1 parent a5bf44d commit b2cf073

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/http.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export default async function http(url, request = {}) {
4646
}
4747
}
4848
catch (resError) {
49+
if (!res) {
50+
// res is completely absent, so we can't construct our own error
51+
// so we'll just throw the error we got
52+
throw resError
53+
}
4954
const error = new Error(res.statusText)
5055
error.statusCode = error.status = res.status
5156
error.responseError = resError

test/client.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ describe('http', () => {
171171
})
172172
})
173173

174+
/**
175+
* See https://github.com/swagger-api/swagger-js/issues/1277
176+
*/
177+
it('should return a helpful error when the connection is refused', () => {
178+
return Swagger('http://localhost:1/untouchable.yaml')
179+
.then((client) => {
180+
throw new Error('Expected an error.')
181+
})
182+
.catch((error) => {
183+
expect(error.message).toEqual('request to http://localhost:1/untouchable.yaml failed, reason: connect ECONNREFUSED 127.0.0.1:1')
184+
expect(error.name).toEqual('FetchError')
185+
})
186+
})
187+
174188
/**
175189
* See https://github.com/swagger-api/swagger-js/issues/1002
176190
*/

0 commit comments

Comments
 (0)