Skip to content

Commit

Permalink
make error handling reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
flanagankp committed Nov 22, 2024
1 parent 88a7e58 commit 99cc7c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
21 changes: 4 additions & 17 deletions src/classes/Auth/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AuthenticateApi, ResponseError, TransactionsApi } from '../../generated';
import { AuthenticateApi, TransactionsApi } from '../../generated';
import { PassageBase, PassageInstanceConfig } from '../PassageBase';
import { PassageError } from '../PassageError';
import { RegisterTransactionArgs } from './types';

/**
Expand Down Expand Up @@ -35,11 +34,7 @@ export class Auth extends PassageBase {

return response.transactionId;
} catch (err) {
if (err instanceof ResponseError) {
throw await PassageError.fromResponseError(err);
}

throw err;
throw await this.parseError(err);
}
}

Expand All @@ -58,11 +53,7 @@ export class Auth extends PassageBase {

return response.transactionId;
} catch (err) {
if (err instanceof ResponseError) {
throw await PassageError.fromResponseError(err);
}

throw err;
throw await this.parseError(err);
}
}

Expand All @@ -83,11 +74,7 @@ export class Auth extends PassageBase {

return response.externalId;
} catch (err) {
if (err instanceof ResponseError) {
throw await PassageError.fromResponseError(err);
}

throw err;
throw await this.parseError(err);
}
}
}
14 changes: 14 additions & 0 deletions src/classes/PassageBase/PassageBase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ResponseError } from '../../generated';
import { PassageError } from '../PassageError';
import { PassageInstanceConfig } from './types';

/**
Expand All @@ -9,4 +11,16 @@ export class PassageBase {
* @param {PassageInstanceConfig} config config properties for Passage instance
*/
public constructor(protected config: PassageInstanceConfig) {}

/**
* Handle errors from PassageFlex API
* @param {unknown} err error from node-fetch request
* @return {Promise<void>}
*/
protected async parseError(err: unknown): Promise<Error> {
if (err instanceof ResponseError) {
return await PassageError.fromResponseError(err);
}
return err as Error;
}
}
22 changes: 4 additions & 18 deletions src/classes/User/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ResponseError, UserDevicesApi, UserInfo, UsersApi, WebAuthnDevices } from '../../generated';
import { UserDevicesApi, UserInfo, UsersApi, WebAuthnDevices } from '../../generated';
import { PassageBase, PassageInstanceConfig } from '../PassageBase';
import { PassageError } from '../PassageError';
import { PassageUser, RevokeDeviceArgs } from './types';

/**
Expand Down Expand Up @@ -38,14 +37,9 @@ export class User extends PassageBase {
if (!users.length) {
throw Error('Could not find user with that external ID');
}

return await this.getUserById(users[0].id);
} catch (err) {
if (err instanceof ResponseError) {
throw await PassageError.fromResponseError(err);
}

throw err;
throw await this.parseError(err);
}
}

Expand All @@ -65,11 +59,7 @@ export class User extends PassageBase {

return response.devices;
} catch (err) {
if (err instanceof ResponseError) {
throw await PassageError.fromResponseError(err);
}

throw err;
throw await this.parseError(err);
}
}

Expand All @@ -88,11 +78,7 @@ export class User extends PassageBase {
userId: user.id,
});
} catch (err) {
if (err instanceof ResponseError) {
throw await PassageError.fromResponseError(err);
}

throw err;
throw await this.parseError(err);
}
}

Expand Down

0 comments on commit 99cc7c4

Please sign in to comment.