All files / src errors.ts

92.86% Statements 26/28
77.78% Branches 14/18
100% Functions 8/8
92.86% Lines 26/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                              18x                         127x 127x 127x 127x 127x 127x 127x       3x 3x     3x       3x       18x               11x 11x 11x 11x       1x                 18x         17x       17x   8x               18x         35x       35x   28x              
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
// tslint:disable max-classes-per-file
export class TransactionError extends Error {
	public message: string;
	public id: string;
	public dataPath: string;
	public actual?: string | number;
	public expected?: string | number;
	public constructor(
		message: string = '',
		id: string = '',
		dataPath: string = '',
		actual?: string | number,
		expected?: string | number,
	) {
		super();
		this.message = message;
		this.name = 'TransactionError';
		this.id = id;
		this.dataPath = dataPath;
		this.actual = actual;
		this.expected = expected;
	}
 
	public toString(): string {
		const defaultMessage = `Transaction: ${this.id} failed at ${this.dataPath}: ${this.message}`;
		const withActual = this.actual
			? `${defaultMessage}, actual: ${this.actual}`
			: defaultMessage;
		const withExpected = this.expected
			? `${withActual}, expected: ${this.expected}`
			: withActual;
 
		return withExpected;
	}
}
 
export class TransactionPendingError extends TransactionError {
	public id: string;
	public dataPath: string;
	public constructor(
		message: string = '',
		id: string = '',
		dataPath: string = '',
	) {
		super(message);
		this.name = 'TransactionPendingError';
		this.id = id;
		this.dataPath = dataPath;
	}
 
	public toString(): string {
		return `Transaction: ${this.id} failed at ${this.dataPath}: ${this.message} `;
	}
}
 
interface ErrorObject {
	readonly dataPath: string;
	readonly message?: string;
}
 
export const convertToTransactionError = (
	id: string,
	// tslint:disable-next-line no-null-undefined-union
	errors: ReadonlyArray<ErrorObject> | null | undefined,
): ReadonlyArray<TransactionError> => {
	Iif (!errors) {
		return [];
	}
 
	return errors.map(
		error =>
			new TransactionError(
				`'${error.dataPath}' ${error.message}`,
				id,
				error.dataPath,
			),
	);
};
 
export const convertToAssetError = (
	id: string,
	// tslint:disable-next-line no-null-undefined-union
	errors: ReadonlyArray<ErrorObject> | null | undefined,
): ReadonlyArray<TransactionError> => {
	Iif (!errors) {
		return [];
	}
 
	return errors.map(
		error =>
			new TransactionError(
				`'${error.dataPath || '.asset'}' ${error.message}`,
				id,
				error.dataPath || '.asset',
			),
	);
};