Skip to content

Commit

Permalink
feat: better hook for handling auth errors (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyg603 authored May 15, 2024
1 parent e5c178b commit 1bf4a2d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/common/client/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export interface BugSplatResponse<T = unknown> {
json: () => Promise<T>;
text: () => Promise<string>;
}

export class BugSplatAuthenticationError extends Error {
readonly isAuthenticationError = true;
}
5 changes: 3 additions & 2 deletions src/common/client/bugsplat-api-client/bugsplat-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiClient, bugsplatAppHostUrl, BugSplatResponse, Environment } from '@common';
import { BugSplatLoginResponse } from './bugsplat-login-response';
import { BugSplatAuthenticationError } from '../api-client';

export class BugSplatApiClient implements ApiClient {
private _createFormData = () => new FormData();
Expand Down Expand Up @@ -78,7 +79,7 @@ export class BugSplatApiClient implements ApiClient {
const response = await this._fetch(url.href, request);

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

if (this._environment === Environment.Node) {
Expand All @@ -99,7 +100,7 @@ export class BugSplatApiClient implements ApiClient {
const regex = new RegExp(/xsrf-token=[^;]*/g);
const matches = cookie.match(regex);
if (!matches) {
throw new Error('Could not parse XSRF token');
throw new BugSplatAuthenticationError('Could not parse XSRF token');
}

const xsrfCookie = matches[0];
Expand Down
2 changes: 1 addition & 1 deletion src/common/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ApiClient, BugSplatResponse } from './api-client';
export { ApiClient, BugSplatResponse, BugSplatAuthenticationError } from './api-client';
export { BugSplatApiClient } from './bugsplat-api-client/bugsplat-api-client';
export { OAuthClientCredentialsClient } from './oauth-client-credentials-api-client/oauth-client-credentials-api-client';
export { S3ApiClient } from './s3-api-client/s3-api-client';
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiClient, bugsplatAppHostUrl, BugSplatResponse } from '@common';
import { OAuthLoginResponse } from './oauth-login-response';
import { BugSplatAuthenticationError } from '../api-client';

export class OAuthClientCredentialsClient implements ApiClient {

Expand Down Expand Up @@ -46,7 +47,7 @@ export class OAuthClientCredentialsClient implements ApiClient {
const responseJson = await response.json();

if ((responseJson as ErrorResponse).error === 'invalid_client') {
throw new Error('Could not authenticate, check credentials and try again');
throw new BugSplatAuthenticationError('Could not authenticate, check credentials and try again');
}

const loginResponse = responseJson as OAuthLoginResponse;
Expand Down Expand Up @@ -75,7 +76,7 @@ export class OAuthClientCredentialsClient implements ApiClient {
const body = response.body;

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

return {
Expand Down

0 comments on commit 1bf4a2d

Please sign in to comment.