Skip to content

Commit

Permalink
refactor: improve InvalidBlockHashError message format to be concise
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed May 10, 2023
1 parent 3224083 commit 38ec5e2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/taquito-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ export class InvalidAddressError extends ParameterValidationError {
* @description Error indicates an invalid block hash being passed or used
*/
export class InvalidBlockHashError extends ParameterValidationError {
constructor(public blockHash: string) {
constructor(public blockHash: string, errorDetail?: string) {
super();
this.name = 'InvalidBlockHashError';
this.message = `Block hash "${blockHash}" is invalid.`;
this.message = `Invalid block hash "${blockHash}"`;
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 @@ -103,7 +103,7 @@ describe('common error classes', () => {
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidBlockHashError);
expect(error.message).toEqual(`Block hash "foo" is invalid.`);
expect(error.message).toEqual(`Invalid block hash "foo"`);
}
});

Expand Down
7 changes: 4 additions & 3 deletions packages/taquito-local-forging/src/taquito-local-forging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CODEC } from './constants';
import { decoders } from './decoder';
import { encoders } from './encoder';
import { Uint8ArrayConsumer } from './uint8array-consumer';
import { validateBlock } from '@taquito/utils';
import { invalidErrorDetail, validateBlock } from '@taquito/utils';
import { InvalidOperationSchemaError } from './error';
import { validateMissingProperty, validateOperationKind } from './validator';
import { ProtocolsHash } from './protocols';
Expand Down Expand Up @@ -40,8 +40,9 @@ export class LocalForger implements Forger {
private codec = getCodec(CODEC.MANAGER, this.protocolHash);

forge(params: ForgeParams): Promise<string> {
if (validateBlock(params.branch) !== ValidationResult.VALID) {
throw new InvalidBlockHashError(params.branch);
const branchValidation = validateBlock(params.branch);
if (branchValidation !== ValidationResult.VALID) {
throw new InvalidBlockHashError(params.branch, invalidErrorDetail(branchValidation));
}

for (const content of params.contents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Forge and parse operations default protocol', () => {
localForger.forge(operation);
}).toThrow(
expect.objectContaining({
message: expect.stringContaining(`Block hash "Invalid_Block_Hash" is invalid`),
message: expect.stringContaining(`Invalid block hash "Invalid_Block_Hash"`),
})
);
expect(() => {
Expand Down

0 comments on commit 38ec5e2

Please sign in to comment.