From 1bf4a2da34e37f6df193ec80571fb3a36abaabf7 Mon Sep 17 00:00:00 2001 From: Bobby Galli Date: Wed, 15 May 2024 09:41:50 -0400 Subject: [PATCH] feat: better hook for handling auth errors (#123) --- src/common/client/api-client.ts | 4 ++++ src/common/client/bugsplat-api-client/bugsplat-api-client.ts | 5 +++-- src/common/client/index.ts | 2 +- .../oauth-client-credentials-api-client.ts | 5 +++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/common/client/api-client.ts b/src/common/client/api-client.ts index 3102731..3e8899d 100644 --- a/src/common/client/api-client.ts +++ b/src/common/client/api-client.ts @@ -9,3 +9,7 @@ export interface BugSplatResponse { json: () => Promise; text: () => Promise; } + +export class BugSplatAuthenticationError extends Error { + readonly isAuthenticationError = true; +} diff --git a/src/common/client/bugsplat-api-client/bugsplat-api-client.ts b/src/common/client/bugsplat-api-client/bugsplat-api-client.ts index 14c4c82..72e3cdc 100644 --- a/src/common/client/bugsplat-api-client/bugsplat-api-client.ts +++ b/src/common/client/bugsplat-api-client/bugsplat-api-client.ts @@ -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(); @@ -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) { @@ -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]; diff --git a/src/common/client/index.ts b/src/common/client/index.ts index a3c7b3a..641751f 100644 --- a/src/common/client/index.ts +++ b/src/common/client/index.ts @@ -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'; \ No newline at end of file diff --git a/src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.ts b/src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.ts index 9b1b07e..243b2a6 100644 --- a/src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.ts +++ b/src/common/client/oauth-client-credentials-api-client/oauth-client-credentials-api-client.ts @@ -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 { @@ -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; @@ -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 {