Skip to content

Commit

Permalink
refactor(crypto): make error more verbose (#2938)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed Sep 19, 2019
1 parent 2981176 commit 2334cd2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions __tests__/unit/crypto/transactions/deserializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ByteBuffer from "bytebuffer";
import { Enums, Errors, Utils } from "../../../../packages/crypto/src";
import { Hash } from "../../../../packages/crypto/src/crypto";
import {
MalformedTransactionBytesError,
InvalidTransactionBytesError,
TransactionSchemaError,
TransactionVersionError,
UnkownTransactionError,
Expand Down Expand Up @@ -219,7 +219,7 @@ describe("Transaction serializer / deserializer", () => {
transaction.serialized = transaction.serialized.slice(0, transaction.serialized.length - 2);

expect(() => TransactionFactory.fromBytes(transaction.serialized)).toThrowError(
MalformedTransactionBytesError,
InvalidTransactionBytesError,
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions __tests__/unit/crypto/transactions/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "jest-extended";

import { Interfaces, Utils } from "@arkecosystem/crypto";
import {
MalformedTransactionBytesError,
InvalidTransactionBytesError,
TransactionSchemaError,
UnkownTransactionError,
} from "../../../../packages/crypto/src/errors";
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("TransactionFactory", () => {
});

it("should fail to create a transaction from hex that contains malformed bytes", () => {
expect(() => TransactionFactory.fromHex("deadbeef")).toThrowError(MalformedTransactionBytesError);
expect(() => TransactionFactory.fromHex("deadbeef")).toThrowError(InvalidTransactionBytesError);
});
});

Expand All @@ -57,7 +57,7 @@ describe("TransactionFactory", () => {

it("should fail to create a transaction from a buffer that contains malformed bytes", () => {
expect(() => TransactionFactory.fromBytes(Buffer.from("deadbeef"))).toThrowError(
MalformedTransactionBytesError,
InvalidTransactionBytesError,
);
});
});
Expand All @@ -69,7 +69,7 @@ describe("TransactionFactory", () => {

it("should fail to create a transaction from a buffer that contains malformed bytes", () => {
expect(() => TransactionFactory.fromBytesUnsafe(Buffer.from("deadbeef"))).toThrowError(
MalformedTransactionBytesError,
InvalidTransactionBytesError,
);
});

Expand Down
10 changes: 5 additions & 5 deletions __tests__/unit/crypto/transactions/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "jest-extended";

import { Utils } from "@arkecosystem/crypto";
import {
MalformedTransactionBytesError,
InvalidTransactionBytesError,
TransactionSchemaError,
TransactionTypeError,
TransactionVersionError,
Expand Down Expand Up @@ -154,10 +154,10 @@ describe("Transaction", () => {
});

it("should throw when getting garbage", () => {
expect(() => TransactionFactory.fromBytes(undefined)).toThrow(MalformedTransactionBytesError);
expect(() => TransactionFactory.fromBytes(Buffer.from("garbage"))).toThrow(MalformedTransactionBytesError);
expect(() => TransactionFactory.fromHex(undefined)).toThrow(MalformedTransactionBytesError);
expect(() => TransactionFactory.fromHex("affe")).toThrow(MalformedTransactionBytesError);
expect(() => TransactionFactory.fromBytes(undefined)).toThrow(InvalidTransactionBytesError);
expect(() => TransactionFactory.fromBytes(Buffer.from("garbage"))).toThrow(InvalidTransactionBytesError);
expect(() => TransactionFactory.fromHex(undefined)).toThrow(InvalidTransactionBytesError);
expect(() => TransactionFactory.fromHex("affe")).toThrow(InvalidTransactionBytesError);
});

it("should throw when getting an unsupported version", () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export class TransactionTypeError extends CryptoError {
}
}

export class MalformedTransactionBytesError extends CryptoError {
constructor() {
super(`Failed to deserialize transaction, because the bytes are malformed.`);
export class InvalidTransactionBytesError extends CryptoError {
constructor(message: string) {
super(`Failed to deserialize transaction, encountered invalid bytes: ${message}`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/crypto/src/transactions/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ByteBuffer from "bytebuffer";
import { TransactionType, TransactionTypeGroup } from "../enums";
import {
DuplicateParticipantInMultiSignatureError,
MalformedTransactionBytesError,
InvalidTransactionBytesError,
TransactionVersionError,
} from "../errors";
import { Address } from "../identities";
Expand Down Expand Up @@ -130,7 +130,7 @@ class Deserializer {
}

if (buf.remaining()) {
throw new MalformedTransactionBytesError();
throw new InvalidTransactionBytesError("signature buffer not exhausted");
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ class Deserializer {
transaction.signatures.push(multiSignaturePart);
}
} else {
throw new MalformedTransactionBytesError();
throw new InvalidTransactionBytesError("signature buffer not exhausted");
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto/src/transactions/factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:member-ordering
import {
DuplicateParticipantInMultiSignatureError,
MalformedTransactionBytesError,
InvalidTransactionBytesError,
TransactionSchemaError,
TransactionVersionError,
} from "../errors";
Expand Down Expand Up @@ -44,7 +44,7 @@ export class TransactionFactory {

return transaction;
} catch (error) {
throw new MalformedTransactionBytesError();
throw new InvalidTransactionBytesError(error.message);
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ export class TransactionFactory {
throw error;
}

throw new MalformedTransactionBytesError();
throw new InvalidTransactionBytesError(error.message);
}
}
}

0 comments on commit 2334cd2

Please sign in to comment.