Skip to content

Commit

Permalink
fix: error handling for mnemonic.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfopietro97 committed Oct 27, 2023
1 parent 35dc57d commit 3ba8fe1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/mnemonic/mnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
type WordListRandomGeneratorSizeInBytes,
type WordlistSizeType
} from './types';
import { ERRORS, MNEMONIC_WORDLIST_ALLOWED_SIZES } from '../utils';
import { MNEMONIC_WORDLIST_ALLOWED_SIZES } from '../utils';
import { buildError, HDNODE } from '@vechain-sdk/errors';

/* --- Overloaded functions start --- */

Expand Down Expand Up @@ -54,7 +55,10 @@ function generate(
wordlistSize !== undefined &&
!MNEMONIC_WORDLIST_ALLOWED_SIZES.includes(wordlistSize)
) {
throw new Error(ERRORS.MNEMONIC.INVALID_MNEMONIC_SIZE);
throw buildError(
HDNODE.INVALID_HDNODE_MNEMONICS,
'Invalid wordlist size given as input. Allowed sizes are 12, 15, 18, 21, 24'
);
}

// Use randomBytes as default random generator if not provided
Expand Down
8 changes: 0 additions & 8 deletions packages/core/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
* @constant
*/
const ERRORS = {
/**
* Error messages related to mnemonic operations.
*/
MNEMONIC: {
INVALID_MNEMONIC_SIZE:
'Invalid mnemonic size. It must be 12, 15, 18, 21, or 24.'
},

/**
* Error messages related to data validations.
*/
Expand Down
9 changes: 6 additions & 3 deletions packages/core/tests/mnemonic/mnemonic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type WordlistSizeType
} from '../../src';
import { randomBytes } from 'crypto';
import { InvalidHDNodeMnemonicsError } from '@vechain-sdk/errors';

/**
* Mnemonic tests
Expand All @@ -28,7 +29,9 @@ describe('Mnemonic', () => {

// Wrong length
// @ts-expect-error - Wrong length error for testing purposes
expect(() => mnemonic.generate(13)).toThrow();
expect(() => mnemonic.generate(13)).toThrowError(
InvalidHDNodeMnemonicsError
);
});

/**
Expand Down Expand Up @@ -125,7 +128,7 @@ describe('Mnemonic', () => {
test('Try to derive private key with a wrong deep derivation path', () => {
expect(() =>
mnemonic.derivePrivateKey(words, wrongDerivationPath)
).toThrow();
).toThrowError();
});

/**
Expand All @@ -134,7 +137,7 @@ describe('Mnemonic', () => {
test('try to derive address with a wrong deep derivation path', () => {
expect(() =>
mnemonic.deriveAddress(words, wrongDerivationPath)
).toThrow();
).toThrowError();
});
});
});

0 comments on commit 3ba8fe1

Please sign in to comment.