Skip to content

Commit 116e878

Browse files
committed
pass originalError, add test
1 parent 816a7f1 commit 116e878

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/http-data-source.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,17 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
213213

214214
if (error instanceof HTTPError) {
215215
if (error.response.statusCode === 401) {
216-
error_ = new AuthenticationError(error.message)
216+
const err = new AuthenticationError(error.message)
217+
err.originalError = error
218+
error_ = err
217219
} else if (error.response.statusCode === 403) {
218-
error_ = new ForbiddenError(error.message)
220+
const err = new ForbiddenError(error.message)
221+
err.originalError = error
222+
error_ = err
219223
} else {
220-
error_ = new ApolloError(error.message)
224+
const err = new ApolloError(error.message)
225+
err.originalError = error
226+
error_ = err
221227
}
222228
}
223229

test/rest-data-source.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ApolloError, AuthenticationError, ForbiddenError } from 'apollo-server-
22
import anyTest, { TestInterface } from 'ava'
33
import { uid } from 'uid'
44
import nock from 'nock'
5-
import { CancelError, HTTPDataSource, TimeoutError, RequestOptions } from '../src'
5+
import { CancelError, HTTPDataSource, TimeoutError, RequestOptions, RequestError, Request } from '../src'
66
import { DataSourceConfig } from 'apollo-datasource'
77

88
const test = anyTest as TestInterface<{ path: string }>
@@ -126,6 +126,8 @@ test('Should cache subsequent GET calls to the same endpoint', async (t) => {
126126
})
127127

128128
test('Should be able to define a custom cache key for request memoization', async (t) => {
129+
t.plan(5)
130+
129131
const baseURL = 'https://api.example.com'
130132
const { path } = t.context
131133
const scope = nock(baseURL).get(path).times(1).reply(200, { name: 'foo' })
@@ -134,6 +136,7 @@ test('Should be able to define a custom cache key for request memoization', asyn
134136
baseURL = baseURL
135137

136138
onCacheKeyCalculation(_requestOptions: RequestOptions) {
139+
t.pass('onCacheKeyCalculation');
137140
return 'foo'
138141
}
139142

@@ -188,6 +191,39 @@ test('Should timeout', async (t) => {
188191
t.is(scope.isDone(), true)
189192
})
190193

194+
test('Should call onRequestError on request error', async (t) => {
195+
t.plan(5)
196+
197+
const baseURL = 'https://api.example.com'
198+
const { path } = t.context
199+
const scope = nock(baseURL).get(path).reply(500)
200+
201+
const dataSource = new (class extends HTTPDataSource {
202+
baseURL = baseURL
203+
204+
async onRequestError(error: Error, request?: Request) {
205+
t.true(error instanceof RequestError);
206+
t.truthy(request);
207+
t.pass('onRequestError');
208+
}
209+
210+
async getFoo() {
211+
return await this.get(path)
212+
}
213+
})()
214+
215+
await t.throwsAsync(
216+
dataSource.getFoo(),
217+
{
218+
instanceOf: ApolloError,
219+
message: "Response code 500 (Internal Server Error)",
220+
},
221+
'Server error',
222+
)
223+
224+
t.is(scope.isDone(), true)
225+
})
226+
191227
test.cb('Should abort request', (t) => {
192228
t.plan(2)
193229

0 commit comments

Comments
 (0)