Skip to content

Commit

Permalink
refactor: improve InvalidAddressError 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 2b08fc9 commit 3224083
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('ContractsLibrary tests', () => {
})
).toThrow(
expect.objectContaining({
message: expect.stringContaining(`Address "KTinvalid" is invalid`),
message: expect.stringContaining(`Invalid address "KTinvalid": Invalid prefix`),
})
);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/taquito-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ 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;
this.message = `Invalid address "${address}"`;
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 @@ -93,7 +93,7 @@ describe('common error classes', () => {
} catch (error) {
expect(error).toBeInstanceOf(ParameterValidationError);
expect(error).toBeInstanceOf(InvalidAddressError);
expect(error.message).toEqual(`Address "foo" is invalid`);
expect(error.message).toEqual(`Invalid address "foo"`);
}
});

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 @@ -293,7 +293,7 @@ export const addressDecoder = (val: Uint8ArrayConsumer) => {
return address;
}
default:
throw new InvalidAddressError(val.toString(), 'unable to decode');
throw new InvalidAddressError(val.toString(), ': Unable to decode.');
}
};

Expand Down
7 changes: 2 additions & 5 deletions packages/taquito-sapling/src/taquito-sapling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,8 @@ export class SaplingToolkit {
default: {
throw new InvalidAddressError(
destination,
`${invalidErrorDetail(
ValidationResult.NO_PREFIX_MATCHED
)}, expecting one of the following prefix '${Prefix.TZ1}', '${Prefix.TZ2}' or '${
Prefix.TZ3
}'.`
invalidErrorDetail(ValidationResult.NO_PREFIX_MATCHED) +
` expecting one of the following prefix '${Prefix.TZ1}', '${Prefix.TZ2}' or '${Prefix.TZ3}'.`
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/taquito-sapling/test/taquito-sapling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('SaplingToolkit', () => {
},
])
).rejects.toThrowError(
`Address "tz1awHvfqEVsmNpXtLsLoHcjLk9HaXkzHf7Z" is invalid wtih invalid prefix expecting prefix zet1.`
`Invalid address "tz1awHvfqEVsmNpXtLsLoHcjLk9HaXkzHf7Z": Invalid prefix expecting prefix zet1.`
);

done();
Expand Down Expand Up @@ -654,7 +654,7 @@ describe('SaplingToolkit', () => {
},
])
).rejects.toThrowError(
`Address "tz2V17qQHTuQ3GJLu5bmPQDJTLDVwiWCrYFh" is invalid wtih invalid prefix expecting prefix zet1`
`Invalid address "tz2V17qQHTuQ3GJLu5bmPQDJTLDVwiWCrYFh": Invalid prefix expecting prefix zet1`
);

done();
Expand Down
10 changes: 3 additions & 7 deletions packages/taquito-utils/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,12 @@ export function validateSpendingKey(value: any): ValidationResult {
export function invalidErrorDetail(validation: ValidationResult): string {
switch (validation) {
case ValidationResult.NO_PREFIX_MATCHED:
return 'wtih invalid prefix';
break;
return ': Invalid prefix';
case ValidationResult.INVALID_CHECKSUM:
return 'wtih invalid checksum';
break;
return ': Checksum failed';
case ValidationResult.INVALID_LENGTH:
return 'with invalid length';
break;
return ': Invalid length';
default:
return '';
break;
}
}

0 comments on commit 3224083

Please sign in to comment.