Skip to content

Commit 16b349a

Browse files
authored
fix: better error for invalid client id or client secret (#84)
* fix: better error for invalid client id or client secret * chore: update src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.spec.ts
1 parent 6d08696 commit 16b349a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ describe('OAuthClientCredentialsClient', () => {
118118
});
119119

120120
it('should call fetch with new init if init is not provided', async () => {
121+
(<any>sut)._fetch.and.returnValue(Promise.resolve(fakeAuthorizeResponseBody));
122+
121123
await sut.fetch(route);
122124

123125
const mostRecentCallArgs = (<any>sut)._fetch.calls.mostRecent().args;
@@ -130,6 +132,17 @@ describe('OAuthClientCredentialsClient', () => {
130132
it('should return result', () => {
131133
expect(result).toEqual(fakeFetchResponseBody);
132134
});
135+
136+
describe('error', () => {
137+
it('should throw error with useful message if fetch returns 401', async () => {
138+
(<any>sut)._fetch.and.resolveTo(createFakeResponseBody(401));
139+
140+
await expectAsync(sut.fetch(route)).toBeRejectedWithError(
141+
Error,
142+
/Could not authenticate/
143+
);
144+
});
145+
});
133146
});
134147
});
135148

src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ export class OAuthClientCredentialsClient implements ApiClient {
7474

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

77-
return this._fetch(url.href, init);
77+
const response = await this._fetch(url.href, init);
78+
79+
if (response.status === 401) {
80+
throw new Error('Could not authenticate, check credentials and try again');
81+
}
82+
83+
return response;
7884
}
7985
}

0 commit comments

Comments
 (0)