Skip to content

Commit

Permalink
refactor: improve InvalidMessageError message format
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed May 10, 2023
1 parent 7f42f9e commit c1987f3
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/taquito-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ 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;
this.message = `Invalid message "${msg}"`;
errorDetail ? (this.message += `${errorDetail}`) : null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/taquito-core/test/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('common error classes', () => {
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidMessageError);
expect(error.message).toEqual(`Message "foo" is invalid.`);
expect(error.message).toEqual(`Invalid message "foo"`);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/taquito-michel-codec/src/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function sha256(msg: number[] | Uint8Array): number[] {
const pad = r === 0 ? 0 : 64 - r;

if (msg.length > 268435455) {
throw new InvalidMessageError('', `SHA-256 -- message length is too big: ${msg.length}`);
throw new InvalidMessageError('', `: Invalid length ${msg.length} is too big -- SHA-256.`);
}

const l = msg.length << 3;
Expand Down
5 changes: 1 addition & 4 deletions packages/taquito-utils/src/verify-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ export function verifySignature(

function validateMessageNotEmpty(message: string) {
if (message === '') {
throw new InvalidMessageError(
message,
'The message provided for verifying signature cannot be empty.'
);
throw new InvalidMessageError(message, ': Cannot be empty.');
}
return message;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/taquito-utils/test/verify-signature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ describe('utils signature validation tests', () => {
'spsig1cdLkp1RLgUHAp13aRFkZ6MQDPp7xCnjAExGL3MBSdMDmT6JgQSX8cufyDgJRM3sinFtiCzLbsyP6d365EHoNevxhT47nx';

expect(() => verifySignature(message, pk, sig)).toThrowError(InvalidMessageError);
expect(() => verifySignature(message, pk, sig)).toThrowError(
/message provided for verifying signature cannot be empty/
);
expect(() => verifySignature(message, pk, sig)).toThrowError(/: Cannot be empty./);
});

it('Should throw InvalidPublicKeyError if the public key is an empty string', () => {
Expand Down

0 comments on commit c1987f3

Please sign in to comment.