Skip to content

Commit

Permalink
1993 consolidating duplicate errors (#2463)
Browse files Browse the repository at this point in the history
* refactor: consolidated InvalidAddressError into @taquito/core

re #1993

* refactor: consolidated InvalidBlockHashError into @taquito/core

re #1993

* refactor: consolidated InvalidDerivationPathError into @taquito/core

re #1993

* refactor: added InvalidDerivationPathError unit test in @taquito/core

re #1993

* refactor: consolidated InvalidHexStringError in to @taquito/core and some style patches

re #1993

* refactor: consolidated InvalidMessageError into @taquito/core

re #1993

* refactor: consolidated InvalidViewParameterError into @taquito/core

re #1993

* refactor: improve InvalidViewParameterError in @taquito/core

re #1993

* refactor: removed tailling params destination/source in InvalidAddressError
  • Loading branch information
hui-an-yang authored May 5, 2023
1 parent f447d24 commit 282ec7d
Show file tree
Hide file tree
Showing 44 changed files with 289 additions and 245 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"js"
],
"moduleNameMapper": {
"^@taquito/core$": "<rootDir>/packages/taquito-core/src/taquito-core.ts",
"^@taquito/rpc$": "<rootDir>/packages/taquito-rpc/src/taquito-rpc.ts",
"^@taquito/taquito$": "<rootDir>/packages/taquito/src/taquito.ts",
"^@taquito/michelson-encoder$": "<rootDir>/packages/taquito-michelson-encoder/src/taquito-michelson-encoder.ts",
Expand All @@ -48,7 +49,6 @@
"^@taquito/local-forging$": "<rootDir>/packages/taquito-local-forging/src/taquito-local-forging.ts",
"^@taquito/tzip16$": "<rootDir>/packages/taquito-tzip16/src/taquito-tzip16.ts",
"^@taquito/tzip12$": "<rootDir>/packages/taquito-tzip12/src/taquito-tzip12.ts",
"^@taquito/core$": "<rootDir>/packages/taquito-core/src/taquito-core.ts",
"^@taquito/contracts-library$": "<rootDir>/packages/taquito-contracts-library/src/contracts-library.ts"
},
"coveragePathIgnorePatterns": [
Expand Down
1 change: 1 addition & 0 deletions packages/taquito-contracts-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2",
"@taquito/rpc": "^16.1.2",
"@taquito/taquito": "^16.1.2",
"@taquito/utils": "^16.1.2",
Expand Down
10 changes: 0 additions & 10 deletions packages/taquito-contracts-library/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**
* @category Error
* @description Error that indicates an invalid address being used or passed
*/
export class InvalidAddressError extends Error {
constructor(message: string) {
super(message);
}
}

/**
* @category Error
* @description Error that indicates invalid script format being useed or passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import { EntrypointsResponse, ScriptedContracts } from '@taquito/rpc';
import { Extension, Context } from '@taquito/taquito';
import { validateAddress, ValidationResult } from '@taquito/utils';
import { InvalidAddressError, InvalidScriptFormatError } from './errors';
import { InvalidScriptFormatError } from './errors';
import { ReadWrapperContractsLibrary } from './read-provider-wrapper';
import { InvalidAddressError } from '@taquito/core';

interface ContractsData {
[contractAddress: string]: { script: ScriptedContracts; entrypoints: EntrypointsResponse };
Expand Down Expand Up @@ -70,7 +71,7 @@ export class ContractsLibrary implements Extension {

private validateContractAddress(address: string) {
if (validateAddress(address) !== ValidationResult.VALID) {
throw new InvalidAddressError(`Address is invalid: ${address}`);
throw new InvalidAddressError(address);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InvalidAddressError, InvalidScriptFormatError } from '../src/errors';
import { InvalidScriptFormatError } from '../src/errors';
import { ContractsLibrary } from '../src/taquito-contracts-library';
import { entrypoints, entrypoints2 } from './data/contract-entrypoints';
import { script, script2 } from './data/contract-script';
import { VERSION } from '../src/version';
import { InvalidAddressError } from '@taquito/core';

describe('ContractsLibrary tests', () => {
it('ContractsLibrary is instantiable', () => {
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('ContractsLibrary tests', () => {
})
).toThrow(
expect.objectContaining({
message: expect.stringContaining('Address is invalid: KTinvalid'),
message: expect.stringContaining(`Address "KTinvalid" is invalid.`),
})
);
});
Expand Down
84 changes: 84 additions & 0 deletions packages/taquito-core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// ==========================================================================================
// parent error classes for Taquito
// ==========================================================================================
/**
* @category Error
* @description Parent error class all taquito errors to extend from
Expand Down Expand Up @@ -39,3 +42,84 @@ export class NetworkError extends TaquitoError {}
* @description Error indicates user attempts an action without necessary permissions
*/
export class PermissionDeniedError extends TaquitoError {}

// ==========================================================================================
// common error classes for Taquito
// ==========================================================================================
/**
* @category Error
* @description Error indicates an invalid originated or implicit address being passed or used
*/
export class InvalidAddressError extends ParameterValidationError {
constructor(public address: string, errorDetail?: string) {
super();
this.name = 'InvalidAddressError';
this.message = `Address "${address}" is invalid.`;
errorDetail ? (this.message += ` ${errorDetail}`) : null;
}
}

/**
* @category Error
* @description Error indicates an invalid block hash being passed or used
*/ export class InvalidBlockHashError extends ParameterValidationError {
constructor(public blockHash: string) {
super();
this.name = 'InvalidBlockHashError';
this.message = `Block hash "${blockHash}" is invalid.`;
}
}

/**
* @category Error
* @description Error indicates an invalid derivation path being passed or used
*/
export class InvalidDerivationPathError extends ParameterValidationError {
constructor(public derivationPath: string, errorDetail?: string) {
super();
this.name = 'InvalidDerivationPathError';
this.message = `Derivation path "${derivationPath}" is invalid.`;
errorDetail ? (this.message += ` ${errorDetail}`) : null;
}
}

/**
* @category Error
* @description Error indicates an invalid hex string have been passed or used
*/
export class InvalidHexStringError extends ParameterValidationError {
constructor(public hexString: string, errorDetail?: string) {
super();
this.name = 'InvalidHexStringError';
this.message = `Hex string "${hexString}" is invalid.`;
errorDetail ? (this.message += ` ${errorDetail}`) : null;
}
}

/**
* @category Error
* @description Error that indicates an invalid message being passed or used
*/
export class InvalidMessageError extends ParameterValidationError {
constructor(public msg: string, errorDetail?: string) {
super();
this.name = 'InvalidMessageError';
this.message = `Message "${msg}" is invalid.`;
errorDetail ? (this.message += ` ${errorDetail}`) : null;
}
}

/**
* @category Error
* @description Error indicates invalid view parameter of a smart contract
*/
export class InvalidViewParameterError extends ParameterValidationError {
name = 'Invalid view parameters error';
constructor(public viewName: string, public sigs: any, public args: any, public cause?: any) {
super();
this.name = 'InvalidViewParameterError';
this.message = `view name: ${viewName} received arguments: ${JSON.stringify(
args
)} while expecting one of the following signatures: (${JSON.stringify(sigs)})`;
}
}
106 changes: 105 additions & 1 deletion packages/taquito-core/test/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {
UnsupportedAction,
NetworkError,
PermissionDeniedError,
InvalidAddressError,
InvalidBlockHashError,
InvalidDerivationPathError,
InvalidHexStringError,
InvalidMessageError,
InvalidViewParameterError,
} from '../src/taquito-core';

describe('parent-errors', () => {
describe('parent errors classes', () => {
it('should throw an ParameterValidationError', () => {
try {
throw new ParameterValidationError('tez');
Expand All @@ -30,6 +36,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an TezosToolkitConfigError', () => {
try {
throw new TezosToolkitConfigError('tez');
Expand All @@ -40,6 +47,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an UnsupportedAction', () => {
try {
throw new UnsupportedAction('tez');
Expand All @@ -50,6 +58,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an NetworkError', () => {
try {
throw new NetworkError('tez');
Expand All @@ -60,6 +69,7 @@ describe('parent-errors', () => {
expect(error.message).toEqual('tez');
}
});

it('should throw an PermissionDeniedError', () => {
try {
throw new PermissionDeniedError('tez');
Expand All @@ -71,3 +81,97 @@ describe('parent-errors', () => {
}
});
});

describe('common error classes', () => {
it('should throw an InvalidAddressError', () => {
try {
throw new InvalidAddressError('tz1');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidAddressError);
expect(error.message).toEqual(`Address "tz1" is invalid.`);
}
try {
throw new InvalidAddressError('tz1', 'params source');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidAddressError);
expect(error.message).toEqual(`Address "tz1" is invalid. params source`);
}
});

it('should throw an InvalidBlockHashError', () => {
try {
throw new InvalidBlockHashError('Bl');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidBlockHashError);
expect(error.message).toEqual(`Block hash "Bl" is invalid.`);
}
});

it('should throw an InvalidDerivationPathError', () => {
try {
throw new InvalidDerivationPathError('1729');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidDerivationPathError);
expect(error.message).toEqual(`Derivation path "1729" is invalid.`);
}
try {
throw new InvalidDerivationPathError('1729', 'invalid BIP32 path');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidDerivationPathError);
expect(error.message).toEqual(`Derivation path "1729" is invalid. invalid BIP32 path`);
}
});

it('should throw an InvalidHexStringError', () => {
try {
throw new InvalidHexStringError('0x12');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidHexStringError);
expect(error.message).toEqual(`Hex string "0x12" is invalid.`);
}
try {
throw new InvalidHexStringError('0x12', 'invalid hexadecimal number');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidHexStringError);
expect(error.message).toEqual(`Hex string "0x12" is invalid. invalid hexadecimal number`);
}
});

it('should throw an InvalidMessageError', () => {
try {
throw new InvalidMessageError('hi');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidMessageError);
expect(error.message).toEqual(`Message "hi" is invalid.`);
}
try {
throw new InvalidMessageError('', 'cannot be empty');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidMessageError);
expect(error.message).toEqual(`Message "" is invalid. cannot be empty`);
}
});

it('should throw an InvalidViewParameterError', () => {
try {
throw new InvalidViewParameterError('add', { parameter: 'nat', result: 'nat' }, 'test');
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidViewParameterError);
expect(error.message).toEqual(
`view name: add received arguments: "test" while expecting one of the following signatures: (${JSON.stringify(
{ parameter: 'nat', result: 'nat' }
)})`
);
}
});
});
20 changes: 5 additions & 15 deletions packages/taquito-ledger-signer/src/taquito-ledger-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {
PublicKeyRetrievalError,
InvalidLedgerResponseError,
} from './error';
import { InvalidDerivationPathError } from '@taquito/core';

export { InvalidDerivationPathError } from '@taquito/core';

export type LedgerTransport = Pick<Transport, 'send' | 'decorateAppAPIMethods' | 'setScrambleKey'>;

Expand All @@ -43,19 +46,6 @@ export class InvalidDerivationTypeError extends Error {
}
}

/**
* @category Error
* @description Error that indicates an invalid derivation path being passed or used
*/
export class InvalidDerivationPathError extends Error {
public name = 'InvalidDerivationPathError';
constructor(public derivationPath: string) {
super(
`The derivation path ${derivationPath} is invalid. The derivation path must start with 44'/1729`
);
}
}

export const HDPathTemplate = (account: number) => {
return `44'/1729'/${account}'/0'`;
};
Expand Down Expand Up @@ -111,8 +101,8 @@ export class LedgerSigner implements Signer {
private derivationType: DerivationType = DerivationType.ED25519
) {
this.transport.setScrambleKey('XTZ');
if (!path.startsWith("44'/1729'")) {
throw new InvalidDerivationPathError(path);
if (!path.startsWith(`44'/1729'`)) {
throw new InvalidDerivationPathError(path, `The derivation path must start with 44'/1729'`);
}
if (!Object.values(DerivationType).includes(derivationType)) {
throw new InvalidDerivationTypeError(derivationType.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe('LedgerSigner test', () => {
it('Should throw exception if path is incorrect', () => {
expect(() => {
new LedgerSigner(mockTransport, "4'/1729'/0'/0'", true, DerivationType.SECP256K1);
}).toThrow("The derivation path 4'/1729'/0'/0' is invalid");
}).toThrow(
`Derivation path "4'/1729'/0'/0'" is invalid. The derivation path must start with 44'/1729'`
);
});

it('Should get public key and public key hash for default path and tz1 curve', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/taquito-local-forging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
]
},
"dependencies": {
"@taquito/core": "^16.1.2",
"@taquito/utils": "^16.1.2",
"bignumber.js": "^9.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/taquito-local-forging/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
prefixLength,
InvalidKeyHashError,
InvalidPublicKeyError,
InvalidAddressError,
InvalidContractAddressError,
} from '@taquito/utils';
import {
Expand All @@ -30,6 +29,7 @@ import {
} from './michelson/codec';
import { Uint8ArrayConsumer } from './uint8array-consumer';
import { pad } from './utils';
import { InvalidAddressError } from '@taquito/core';

// https://tezos.gitlab.io/shell/p2p_api.html specifies data types and structure for forging

Expand Down
Loading

0 comments on commit 282ec7d

Please sign in to comment.