Skip to content

Commit

Permalink
refactor: imporve InvalidProtocolHashError and ValueConversionError i…
Browse files Browse the repository at this point in the history
…n @taquito/utils

re #1994
  • Loading branch information
hui-an-yang committed May 9, 2023
1 parent ef00092 commit 1dee9a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/taquito-utils/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ParameterValidationError, UnsupportedAction } from '@taquito/core';

export {
InvalidAddressError,
InvalidBlockHashError,
Expand All @@ -17,22 +19,26 @@ export {

/**
* @category Error
* @description Error that indicates invalid protocol hash being passed or used
* @description Error indicates invalid protocol hash being passed or used
*/
export class InvalidProtocolHashError extends Error {
export class InvalidProtocolHashError extends ParameterValidationError {
public name = 'InvalidProtocolHashError';
constructor(public protocolHash: string) {
super(`The protocol hash '${protocolHash}' is invalid`);
constructor(public protocolHash: string, errorDetails?: string) {
super();
this.name = 'InvalidProtocolHashError';
this.message = `The protocol hash '${protocolHash}' is invalid`;
errorDetails ? (this.message += `: ${errorDetails}`) : null;
}
}

/**
* @category Error
* @description General error that indicates a failure when trying to convert data from one type to another
* @description Error indicates unable to convert data type from one to another
*/
export class ValueConversionError extends Error {
public name = 'ValueConversionError';
export class ValueConversionError extends UnsupportedAction {
constructor(public value: string, public desiredType: string) {
super(`Unable to convert ${value} to a ${desiredType}`);
super();
this.name = 'ValueConversionError';
this.message = `Unable to convert ${value} to a ${desiredType}`;
}
}
1 change: 1 addition & 0 deletions packages/taquito-utils/src/taquito-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export function encodeKeyHash(value: string) {
* @description Convert an hex string to a Uint8Array
*
* @param hex Hex string to convert
* @throws {@link ValueConversionError}
*/
export const hex2buf = (hex: string): Uint8Array => {
const match = hex.match(/[\da-f]{2}/gi);
Expand Down

0 comments on commit 1dee9a1

Please sign in to comment.