Skip to content

Commit

Permalink
Merge pull request #1351 from pseudoramble/bug/invoke-afterware-on-ht…
Browse files Browse the repository at this point in the history
…tp-errors

Invoke afterware after any kind of response.
  • Loading branch information
helfer authored Mar 5, 2017
2 parents e91f3cf + fa49ee1 commit 9e8662c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API.

### vNEXT
- Fix: Invoke afterware even on requests that error [PR #1351](https://github.com/apollographql/apollo-client/pull/1351)


### 0.10.1
- Address deprecation warnings coming from `graphql-tag` [graphql-tag#54](https://github.com/apollographql/graphql-tag/issues/54)
Expand Down
9 changes: 6 additions & 3 deletions src/transport/batchedNetworkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ export class HTTPBatchedNetworkInterface extends BaseNetworkInterface {
const httpResponse = result as Response;

if (!httpResponse.ok) {
const httpError = new Error(`Network request failed with status ${httpResponse.status} - "${httpResponse.statusText}"`);
(httpError as any).response = httpResponse;
return this.applyBatchAfterwares({ responses: [httpResponse], options: batchRequestAndOptions })
.then(() => {
const httpError = new Error(`Network request failed with status ${httpResponse.status} - "${httpResponse.statusText}"`);
(httpError as any).response = httpResponse;

throw httpError;
throw httpError;
});
}

// XXX can we be stricter with the type here?
Expand Down
54 changes: 54 additions & 0 deletions test/batchedNetworkInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,58 @@ describe('HTTPBatchedNetworkInterface', () => {
opts: options,
});
});

describe('afterware execution', () => {
const afterwareStub = sinon.stub();
const testAfterwares: BatchAfterwareInterface[] = [
{
applyBatchAfterware(response, next) {
afterwareStub();
next();
},
},
{
applyBatchAfterware(response, next) {
afterwareStub();
next();
},
},
];

afterEach(() => afterwareStub.reset());

it('executes afterware when valid responses given back', done => {
assertRoundtrip({
requestResultPairs: [{
request: { query: authorQuery },
result: authorResult,
}],
afterwares: testAfterwares,
}).then(() => {
assert.equal(afterwareStub.callCount, testAfterwares.length, 'Afterwares provided were not invoked');
done();
}).catch(err => {
done(err);
});
});

it('executes afterware when an invalid response is given back', done => {
const fakeForbiddenResponse = createMockedIResponse([], { status: 401, statusText: 'Unauthorized'});
const fetchFunc = () => Promise.resolve(fakeForbiddenResponse);

assertRoundtrip({
requestResultPairs: [{
request: { query: authorQuery },
result: authorResult,
}],
fetchFunc,
afterwares: testAfterwares,
}).then(() => {
done(new Error('The networkInterface did not reject as expected'));
}).catch(err => {
assert.equal(afterwareStub.callCount, testAfterwares.length, 'Afterwares provided were not invoked');
done();
});
});
});
});

0 comments on commit 9e8662c

Please sign in to comment.