Skip to content

Commit

Permalink
Add 3rd url argument to didEncounterErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Aug 21, 2023
1 parent d2f0f5d commit 109ca0e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
11 changes: 11 additions & 0 deletions .changeset/gorgeous-timers-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@apollo/datasource-rest': minor
---

Add `url` parameter to `didEncounterErrors` hook

In previous versions of `RESTDataSource`, the URL
of the request was available on the `Request` object
passed in to the hook. The `Request` object is no
longer passed as an argument, so this restores
the availability of the `url` to the hook.
8 changes: 6 additions & 2 deletions src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ export abstract class RESTDataSource {
request: FetcherRequestInit,
): ValueOrPromise<CacheOptions | undefined>;

protected didEncounterError(_error: Error, _request: RequestOptions) {
protected didEncounterError(
_error: Error,
_request: RequestOptions,
_url: URL,
) {
// left as a no-op instead of an unimplemented optional method to avoid
// breaking an existing use case where one calls
// `super.didEncounterErrors(...)` This could be unimplemented / undefined
Expand Down Expand Up @@ -544,7 +548,7 @@ export abstract class RESTDataSource {
},
};
} catch (error) {
this.didEncounterError(error as Error, outgoingRequest);
this.didEncounterError(error as Error, outgoingRequest, url);
throw error;
}
});
Expand Down
43 changes: 35 additions & 8 deletions src/__tests__/RESTDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ describe('RESTDataSource', () => {
class AuthedDataSource extends RESTDataSource {
override baseURL = 'https://api.example.com';

constructor(
private token: string,
config?: DataSourceConfig,
) {
constructor(private token: string, config?: DataSourceConfig) {
super(config);
}

Expand Down Expand Up @@ -261,10 +258,7 @@ describe('RESTDataSource', () => {
class AuthedDataSource extends RESTDataSource {
override baseURL = 'https://api.example.com';

constructor(
private token: string,
config?: DataSourceConfig,
) {
constructor(private token: string, config?: DataSourceConfig) {
super(config);
}

Expand Down Expand Up @@ -2193,6 +2187,39 @@ describe('RESTDataSource', () => {
message: 'I replaced the error entirely',
});
});

it('is called with the url', async () => {
let urlFromDidEncounterError: URL | null = null;
const dataSource = new (class extends RESTDataSource {
override baseURL = 'https://api.example.com';

getFoo() {
return this.get('foo');
}

override didEncounterError(_: Error, __: RequestOptions, url: URL) {
urlFromDidEncounterError = url;
}
})();

nock(apiUrl)
.get('/foo')
.reply(
500,
{
errors: [{ message: 'Houston, we have a problem.' }],
},
{ 'content-type': 'application/json' },
);

try {
await dataSource.getFoo();
} catch {}

expect((urlFromDidEncounterError as any).toString()).toMatch(
'https://api.example.com/foo',
);
});
});
});
});
Expand Down

0 comments on commit 109ca0e

Please sign in to comment.