Skip to content

Commit

Permalink
Rename to error handler and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Mar 5, 2024
1 parent 22fe22f commit dce8aea
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/bearbyWallet/BearbyAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { IAccount } from '../account/IAccount';
import { AddressInfo, web3 } from '@hicaru/bearby.js';
import { postRequest } from '../massaStation/RequestHandler';
import { IAccountSignOutput } from '../account/AccountSign';
import { operationErrorMapping } from '../errors/utils/errorMapping';
import { operationType } from 'src/utils/constants';
import { errorHandler } from '../errors/utils/errorHandler';
import { operationType } from '../utils/constants';

export class BearbyAccount implements IAccount {
private _providerName: string;
Expand Down Expand Up @@ -91,7 +91,7 @@ export class BearbyAccount implements IAccount {
base58Encoded: signature.signature,
};
} catch (error) {
throw operationErrorMapping(operationType.SIGN, error);
throw errorHandler(operationType.Sign, error);
}
}

Expand All @@ -103,7 +103,7 @@ export class BearbyAccount implements IAccount {
operationId,
};
} catch (error) {
throw operationErrorMapping(operationType.BUY_ROLLS, error);
throw errorHandler(operationType.BuyRolls, error);
}
}

Expand All @@ -115,7 +115,7 @@ export class BearbyAccount implements IAccount {
operationId,
};
} catch (error) {
throw operationErrorMapping('sellRolls', error);
throw errorHandler(operationType.SellRolls, error);
}
}

Expand All @@ -133,7 +133,7 @@ export class BearbyAccount implements IAccount {

return { operationId };
} catch (error) {
throw operationErrorMapping(operationType.SEND_TRANSACTION, error);
throw errorHandler(operationType.SendTransaction, error);
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ export class BearbyAccount implements IAccount {
unsafeParameters,
});
} catch (error) {
throw operationErrorMapping(operationType.CALL_SC, error);
throw errorHandler(operationType.CallSC, error);
}
return { operationId };
}
Expand Down
4 changes: 2 additions & 2 deletions src/errors/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ test('BaseError with cause', () => {
const cause = new Error('The cause of the error');
const baseError = new BaseError('An error occurred.', { cause });
expect(baseError.cause).toBe(cause);
expect(baseError.message).toBe(`An error occurred.`);
expect(baseError.message).toBe('An error occurred.');
if (baseError.cause instanceof Error) {
expect(baseError.cause.message).toBe(`The cause of the error`);
expect(baseError.cause.message).toBe('The cause of the error');
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserRejectionError } from '../userRejection';
import { isUserRejectionError, operationErrorMapping } from './errorMapping';
import { isUserRejectionError, errorHandler } from './errorHandler';

const massaUserRejectionErrorMessage = 'Action canceled by user';
const bearbyUserRejectionErrorMessage = 'User rejected';
Expand All @@ -22,19 +22,19 @@ describe('isUserRejectionError', () => {
});
});

describe('operationErrorMapping', () => {
describe('errorHandler', () => {
it('should return a UserRejectionError if the error is a user rejection error', () => {
const error = new Error('User rejected');
const mappedError = operationErrorMapping('someOperation', error);
expect(mappedError).toBeInstanceOf(UserRejectionError);
expect(mappedError.message).toBe(
const processedError = errorHandler('someOperation', error);
expect(processedError).toBeInstanceOf(UserRejectionError);
expect(processedError.message).toBe(
'The operation someOperation was rejected by the user.',
);
});

it('should return the original error if the error is not a user rejection error', () => {
const error = new Error(someOtherErrorMessage);
const mappedError = operationErrorMapping('someOperation', error);
expect(mappedError).toBe(error);
const processedError = errorHandler('someOperation', error);
expect(processedError).toBe(error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export function isUserRejectionError(error: Error): boolean {
);
}

export function operationErrorMapping(
operationName: string,
error: Error,
): Error {
export function errorHandler(operationName: string, error: Error): Error {
if (isUserRejectionError(error)) {
return new UserRejectionError({
operationName: operationName,
Expand Down
13 changes: 7 additions & 6 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { argsToBase64, uint8ArrayToBase64 } from '../utils/argsToBase64';
import { IAccountSignOutput, ISignMessage } from '../account/AccountSign';
import { encode as base58Encode } from 'bs58check';
import { ExecuteFunctionBody } from './types';
import { operationErrorMapping } from '../errors/utils/errorMapping';
import { errorHandler } from '../errors/utils/errorHandler';
import { operationType } from '../utils/constants';

/**
* This interface represents the the individual wallet's final and pending balances returned by MassaStation
Expand Down Expand Up @@ -145,7 +146,7 @@ export class MassaStationAccount implements IAccount {
);

if (signOpResponse.isError || signOpResponse.error) {
throw operationErrorMapping('sign', signOpResponse.error);
throw errorHandler(operationType.Sign, signOpResponse.error);
}

const signature = base58Encode(
Expand Down Expand Up @@ -180,7 +181,7 @@ export class MassaStationAccount implements IAccount {
buyRollsOpResponse = await postRequest<ITransactionDetails>(url, body);

if (buyRollsOpResponse.isError || buyRollsOpResponse.error) {
operationErrorMapping('buyRolls', buyRollsOpResponse.error);
errorHandler(operationType.BuyRolls, buyRollsOpResponse.error);
}
return buyRollsOpResponse.result;
}
Expand All @@ -207,7 +208,7 @@ export class MassaStationAccount implements IAccount {
sellRollsOpResponse = await postRequest<ITransactionDetails>(url, body);

if (sellRollsOpResponse.isError || sellRollsOpResponse.error) {
operationErrorMapping('sellRolls', sellRollsOpResponse.error);
errorHandler(operationType.SellRolls, sellRollsOpResponse.error);
}
return sellRollsOpResponse.result;
}
Expand Down Expand Up @@ -235,7 +236,7 @@ export class MassaStationAccount implements IAccount {
sendTxOpResponse = await postRequest<ITransactionDetails>(url, body);

if (sendTxOpResponse.isError || sendTxOpResponse.error) {
throw operationErrorMapping('sendTransaction', sendTxOpResponse.error);
throw errorHandler(operationType.SendTransaction, sendTxOpResponse.error);
}

return sendTxOpResponse.result;
Expand Down Expand Up @@ -302,7 +303,7 @@ export class MassaStationAccount implements IAccount {
CallSCOpResponse = await postRequest<ITransactionDetails>(url, body);

if (CallSCOpResponse.isError || CallSCOpResponse.error) {
throw operationErrorMapping('callSmartContract', CallSCOpResponse.error);
throw errorHandler('callSmartContract', CallSCOpResponse.error);
}
return CallSCOpResponse.result;
}
Expand Down
10 changes: 5 additions & 5 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export enum operationType {
CALL_SC = 'callSC',
SEND_TRANSACTION = 'sendTransaction',
SELL_ROLLS = 'sellRolls',
BUY_ROLLS = 'buyRolls',
SIGN = 'sign',
CallSC = 'callSC',
SendTransaction = 'sendTransaction',
SellRolls = 'sellRolls',
BuyRolls = 'buyRolls',
Sign = 'sign',
}

0 comments on commit dce8aea

Please sign in to comment.