Skip to content

Commit fa40887

Browse files
committed
fixup! refactor(rabbitmq): now the package respects all the repository standards
1 parent d31d060 commit fa40887

File tree

3 files changed

+32
-83
lines changed

3 files changed

+32
-83
lines changed

packages/rabbitmq/src/TxSubmitWorker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Channel, Connection, Message, connect } from 'amqplib';
33
import { Logger, dummyLogger } from 'ts-log';
44
import { ProviderError, ProviderFailure, TxSubmitProvider } from '@cardano-sdk/core';
5-
import { SerializedError, TX_SUBMISSION_QUEUE, serializeError, txBodyToId, waitForPending } from './utils';
5+
import { TX_SUBMISSION_QUEUE, serializeError, txBodyToId, waitForPending } from './utils';
66

77
const moduleName = 'TxSubmitWorker';
88

@@ -262,7 +262,7 @@ export class TxSubmitWorker {
262262
private async submitTx(message: Message) {
263263
const counter = ++this.#counter;
264264
let isRetriable = false;
265-
let serializedError: SerializedError | null = null;
265+
let serializableError: unknown;
266266
let txId = '';
267267

268268
try {
@@ -279,14 +279,14 @@ export class TxSubmitWorker {
279279
this.#dependencies.logger!.debug(`${moduleName}: ACKing RabbitMQ message #${counter}`);
280280
this.#channel?.ack(message);
281281
} catch (error) {
282-
({ isRetriable, serializedError } = await this.submitTxErrorHandler(error, counter, message));
282+
({ isRetriable, serializableError } = await this.submitTxErrorHandler(error, counter, message));
283283
} finally {
284284
// If there is no error or the error can't be retried
285-
if (!serializedError || !isRetriable) {
285+
if (!serializableError || !isRetriable) {
286286
// Send the response to the original submitter
287287
try {
288288
// An empty response message means succesful submission
289-
const message = serializedError || {};
289+
const message = serializableError || {};
290290
await this.#channel!.assertQueue(txId);
291291
this.logError(`${moduleName}: sending response for message #${counter}`);
292292
this.#channel!.sendToQueue(txId, Buffer.from(JSON.stringify(message)));
@@ -302,7 +302,7 @@ export class TxSubmitWorker {
302302
* The error handler of submitTx method
303303
*/
304304
private async submitTxErrorHandler(err: unknown, counter: number, message: Message) {
305-
const { isRetriable, serializedError } = serializeError(err);
305+
const { isRetriable, serializableError } = serializeError(err);
306306

307307
if (isRetriable) this.#dependencies.logger!.warn(`${moduleName}: submitting tx #${counter}`);
308308
else this.#dependencies.logger!.error(`${moduleName}: submitting tx #${counter}`);
@@ -321,6 +321,6 @@ export class TxSubmitWorker {
321321
this.logError(error);
322322
}
323323

324-
return { isRetriable, serializedError };
324+
return { isRetriable, serializableError };
325325
}
326326
}

packages/rabbitmq/src/rabbitmqTxSubmitProvider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Buffer } from 'buffer';
22
import { Cardano, HealthCheckResponse, ProviderError, ProviderFailure, TxSubmitProvider } from '@cardano-sdk/core';
33
import { Channel, Connection, connect } from 'amqplib';
44
import { Logger, dummyLogger } from 'ts-log';
5-
import { SerializedError, TX_SUBMISSION_QUEUE, deserializeError, txBodyToId, waitForPending } from './utils';
5+
import { TX_SUBMISSION_QUEUE, getErrorPrototype, txBodyToId, waitForPending } from './utils';
6+
import { fromSerializableObject } from '@cardano-sdk/util';
67

78
const moduleName = 'RabbitMqTxSubmitProvider';
89

@@ -199,12 +200,12 @@ export class RabbitMqTxSubmitProvider implements TxSubmitProvider {
199200
await this.#channel!.deleteQueue(txId);
200201
this.#dependencies.logger!.debug(`${moduleName}: deleted queue: ${txId}`);
201202

202-
const result = JSON.parse(message.content.toString()) as SerializedError;
203+
const result = JSON.parse(message.content.toString());
203204

204205
// An empty result message means submission ok
205206
if (Object.keys(result).length === 0) return done();
206207

207-
done(deserializeError(result));
208+
done(fromSerializableObject(result, { getErrorPrototype }));
208209
} catch (error) {
209210
this.#dependencies.logger!.error(`${moduleName}: while handling response message: ${txId}`);
210211
this.#dependencies.logger!.error(error);

packages/rabbitmq/src/utils.ts

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,29 @@
11
import { CSL, Cardano, cslToCore } from '@cardano-sdk/core';
22
import { OutsideOfValidityInterval } from '@cardano-ogmios/schema';
3-
import { fromSerializableObject, toSerializableObject } from '@cardano-sdk/util';
4-
5-
export interface DeserializedError {
6-
innerError?: {
7-
name: string;
8-
message: string;
9-
};
10-
name: string;
11-
message: string;
12-
}
13-
14-
export interface SerializedError {
15-
value: {
16-
innerError?: {
17-
name: string;
18-
message: string;
19-
stack?: string;
20-
};
21-
name: string;
22-
message: string;
23-
stack?: string;
24-
};
25-
}
3+
import { toSerializableObject } from '@cardano-sdk/util';
264

275
export const TX_SUBMISSION_QUEUE = 'cardano-tx-submit';
286

297
/**
30-
* Enriches an error with the right prototype
8+
* Analyzes a serializable error to get the right prototype object
319
*
32-
* @param err the error to enrich
10+
* @param error the error to analyze
11+
* @returns the right prototype for the error
3312
*/
34-
const addPrototypeToError = (err: DeserializedError) => {
35-
const { name, innerError } = err;
36-
let prototype: object | null = Error.prototype;
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
export const getErrorPrototype = (error: unknown) => {
15+
if (typeof error === 'object') {
16+
const rawError = error as Cardano.TxSubmissionError;
3717

38-
if (name in Cardano.TxSubmissionErrors)
39-
prototype = Cardano.TxSubmissionErrors[name as keyof typeof Cardano.TxSubmissionErrors].prototype;
40-
41-
Object.setPrototypeOf(err, prototype);
42-
43-
if (innerError) addPrototypeToError(innerError);
44-
};
18+
if (typeof rawError.name === 'string' && typeof rawError.message === 'string') {
19+
const txSubmissionErrorName = rawError.name as keyof typeof Cardano.TxSubmissionErrors;
20+
const ErrorClass = Cardano.TxSubmissionErrors[txSubmissionErrorName];
4521

46-
/**
47-
* Deserializes a serialized error
48-
*
49-
* @param serializedError the error to deserialize
50-
*/
51-
export const deserializeError = (serializedError: SerializedError): unknown => {
52-
const ret = fromSerializableObject(serializedError) as DeserializedError;
53-
54-
addPrototypeToError(ret);
22+
if (ErrorClass) return ErrorClass.prototype;
23+
}
24+
}
5525

56-
return ret;
26+
return Error.prototype;
5727
};
5828

5929
/**
@@ -63,38 +33,16 @@ export const deserializeError = (serializedError: SerializedError): unknown => {
6333
*/
6434
export const serializeError = (err: unknown) => {
6535
let isRetriable = false;
66-
let serializedError: SerializedError | null = null;
67-
68-
const lastChance = () => {
69-
try {
70-
serializedError = { value: { message: JSON.stringify(err), name: 'Error' } };
71-
} catch {
72-
serializedError = { value: { message: 'Unknown', name: 'Error' } };
73-
}
74-
};
75-
76-
try {
77-
serializedError = toSerializableObject(err) as SerializedError;
78-
} catch {
79-
lastChance();
80-
}
8136

82-
if (!serializedError!.value) lastChance();
37+
const serializableError = toSerializableObject(err);
8338

84-
delete serializedError!.value.stack;
85-
if (serializedError!.value.innerError) delete serializedError!.value.innerError.stack;
39+
if (err instanceof Cardano.TxSubmissionErrors.OutsideOfValidityIntervalError) {
40+
const details = JSON.parse(err.message) as OutsideOfValidityInterval['outsideOfValidityInterval'];
8641

87-
if (serializedError!.value.name === 'OutsideOfValidityIntervalError')
88-
try {
89-
const details = JSON.parse(
90-
serializedError!.value.message
91-
) as OutsideOfValidityInterval['outsideOfValidityInterval'];
92-
93-
if (details.interval.invalidBefore && details.currentSlot <= details.interval.invalidBefore) isRetriable = true;
94-
// eslint-disable-next-line no-empty
95-
} catch {}
42+
if (details.interval.invalidBefore && details.currentSlot <= details.interval.invalidBefore) isRetriable = true;
43+
}
9644

97-
return { isRetriable, serializedError };
45+
return { isRetriable, serializableError };
9846
};
9947

10048
export const txBodyToId = (txBody: Buffer | Uint8Array, dummy?: boolean) => {

0 commit comments

Comments
 (0)