Skip to content

Commit

Permalink
fix: added common error class (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Dec 11, 2023
1 parent ac83886 commit 6b82482
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
9 changes: 8 additions & 1 deletion src/cognito-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
RevokeTokenException,
SignUpException,
UpdateUserAttributesException,
VerifyUserAttributeException
VerifyUserAttributeException,
COMMON_EXCEPTIONS,
CommonError,
CommonException
} from './error.js';

import {
Expand Down Expand Up @@ -455,6 +458,10 @@ export async function cognitoRequest(body: object, serviceTarget: ServiceTarget,
'Unknown'
);

if (COMMON_EXCEPTIONS.includes(cognitoException as CommonException)) {
throw new CommonError(errorMessage, cognitoException as CommonException);
}

switch (serviceTarget) {
case ServiceTarget.InitiateAuth:
throw new InitAuthError(errorMessage, cognitoException as InitiateAuthException);
Expand Down
67 changes: 55 additions & 12 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
export enum CommonException {
AccessDeniedException = 'AccessDeniedException',
IncompleteSignature = 'IncompleteSignature',
InternalFailure = 'InternalFailure',
InvalidAction = 'InvalidAction',
InvalidClientTokenId = 'InvalidClientTokenId',
NotAuthorized = 'NotAuthorized',
OptInRequired = 'OptInRequired',
RequestExpired = 'RequestExpired',
ServiceUnavailable = 'ServiceUnavailable',
ThrottlingException = 'ThrottlingException',
ValidationError = 'ValidationError'
}

export const COMMON_EXCEPTIONS = [
CommonException.AccessDeniedException,
CommonException.IncompleteSignature,
CommonException.InternalFailure,
CommonException.InvalidAction,
CommonException.InvalidClientTokenId,
CommonException.NotAuthorized,
CommonException.OptInRequired,
CommonException.RequestExpired,
CommonException.ServiceUnavailable,
CommonException.ThrottlingException,
CommonException.ValidationError
];

export enum AssociateSoftwareTokenException {
ConcurrentModificationException = 'ConcurrentModificationException',
ForbiddenException = 'ForbiddenException',
Expand Down Expand Up @@ -373,7 +401,22 @@ export enum RevokeTokenException {
UnsupportedTokenTypeException = 'UnsupportedTokenTypeException'
}

export class InitAuthError extends Error {
export class CognitoError extends Error {
constructor(message: string) {
super(message);
}
}

export class CommonError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: CommonException
) {
super(message);
}
}

export class InitAuthError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: InitiateAuthException
Expand All @@ -382,7 +425,7 @@ export class InitAuthError extends Error {
}
}

export class RespondToAuthChallengeError extends Error {
export class RespondToAuthChallengeError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: RespondToAuthChallengeException
Expand All @@ -391,7 +434,7 @@ export class RespondToAuthChallengeError extends Error {
}
}

export class SignUpError extends Error {
export class SignUpError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: SignUpException
Expand All @@ -400,7 +443,7 @@ export class SignUpError extends Error {
}
}

export class ConfirmSignUpError extends Error {
export class ConfirmSignUpError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: ConfirmSignUpException
Expand All @@ -409,7 +452,7 @@ export class ConfirmSignUpError extends Error {
}
}

export class ChangePasswordError extends Error {
export class ChangePasswordError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: ChangePasswordException
Expand All @@ -418,7 +461,7 @@ export class ChangePasswordError extends Error {
}
}

export class RevokeTokenError extends Error {
export class RevokeTokenError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: RevokeTokenException
Expand All @@ -427,7 +470,7 @@ export class RevokeTokenError extends Error {
}
}

export class ForgotPasswordError extends Error {
export class ForgotPasswordError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: ForgotPasswordException
Expand All @@ -436,7 +479,7 @@ export class ForgotPasswordError extends Error {
}
}

export class ConfirmForgotPasswordError extends Error {
export class ConfirmForgotPasswordError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: ConfirmForgotPasswordException
Expand All @@ -445,7 +488,7 @@ export class ConfirmForgotPasswordError extends Error {
}
}

export class ResendConfirmationCodeError extends Error {
export class ResendConfirmationCodeError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: ResendConfirmationException
Expand All @@ -454,7 +497,7 @@ export class ResendConfirmationCodeError extends Error {
}
}

export class UpdateUserAttributesError extends Error {
export class UpdateUserAttributesError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: UpdateUserAttributesException
Expand All @@ -463,7 +506,7 @@ export class UpdateUserAttributesError extends Error {
}
}

export class VerifyUserAttributeError extends Error {
export class VerifyUserAttributeError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: VerifyUserAttributeException
Expand All @@ -472,7 +515,7 @@ export class VerifyUserAttributeError extends Error {
}
}

export class GlobalSignOutError extends Error {
export class GlobalSignOutError extends CognitoError {
constructor(
message: string,
public readonly cognitoException: GlobalSignOutException
Expand Down
24 changes: 23 additions & 1 deletion src/tests/cognito-client.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import {
RevokeTokenException,
SignUpException,
UpdateUserAttributesException,
VerifyUserAttributeException
VerifyUserAttributeException,
COMMON_EXCEPTIONS,
CommonError
} from '../error.js';

const fetchMocker = createFetchMock(vi);
Expand Down Expand Up @@ -302,5 +304,25 @@ describe('Cognito Client', () => {
expect(cognitoRequest({}, ServiceTarget.GlobalSignOut, 'http://localhost')).rejects.toThrowError(
new GlobalSignOutError('test', 'code' as GlobalSignOutException)
);

COMMON_EXCEPTIONS.forEach(exception => {
fetchMocker.mockResponse(
JSON.stringify({
message: 'test',
code: exception
}),
{
status: 400,
headers: {
'X-Amzn-ErrorMessage': 'test',
'X-Amzn-ErrorType': exception
}
}
);

expect(cognitoRequest({}, ServiceTarget.RespondToAuthChallenge, 'http://localhost')).rejects.toThrowError(
new CommonError('test', exception)
);
});
});
});

0 comments on commit 6b82482

Please sign in to comment.