Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better error for invalid client id or client secret #84

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ describe('OAuthClientCredentialsClient', () => {
});

it('should call fetch with new init if init is not provided', async () => {
(<any>sut)._fetch.and.returnValue(Promise.resolve(fakeAuthorizeResponseBody));

await sut.fetch(route);

const mostRecentCallArgs = (<any>sut)._fetch.calls.mostRecent().args;
Expand All @@ -130,6 +132,17 @@ describe('OAuthClientCredentialsClient', () => {
it('should return result', () => {
expect(result).toEqual(fakeFetchResponseBody);
});

describe('error', () => {
it('should throw error with useful message if fetch returns 401', async () => {
(<any>sut)._fetch.and.resolveTo(createFakeResponseBody(401));

await expectAsync(sut.fetch(route)).toBeRejectedWithError(
Error,
/Could not authenticate/
);
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export class OAuthClientCredentialsClient implements ApiClient {

init.headers['Authorization'] = `${this._tokenType} ${this._accessToken}`;

return this._fetch(url.href, init);
const response = await this._fetch(url.href, init);

if (response.status === 401) {
throw new Error('Could not authenticate, check credentials and try again');
}

return response;
}
}