Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve error logging #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Signer = require('./db/Signer');
const { Op } = require('sequelize');
const Tx = require('./db/Tx');
const { isAddressValid } = require('@aeternity/aepp-sdk');
const { TxUnpackFailedError, TxHashNotMatchingError, HashAlreadyExistentError } = require('./util');
const { TxUnpackFailedError, TxHashNotMatchingError, HashAlreadyExistentError, logError } = require('./util');

let running = true;
let status = 'started';
Expand All @@ -26,7 +26,7 @@ const sync = (height) => {
return indexSigners(height)
.then(() => (running = false))
.catch((e) => {
console.error(e);
logError(e);
running = false;
});
};
Expand Down Expand Up @@ -78,7 +78,7 @@ const start = async () => {
res.status(400);
return res.json({ error: e.message });
} else {
console.error(e);
logError(e);
return res.sendStatus(500);
}
});
Expand Down
31 changes: 21 additions & 10 deletions src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Tx = require('./db/Tx');

const CONTRACT_ACI = require('./contractAci.json');
const { Buffer } = require('buffer');
const { TxUnpackFailedError, TxHashNotMatchingError, HashAlreadyExistentError } = require('./util');
const { TxUnpackFailedError, TxHashNotMatchingError, HashAlreadyExistentError, logError } = require('./util');
const { migrate } = require('./db/migration');

// TODO: don't validate hash or accept fee, gasPrice provided by wallet
Expand All @@ -25,7 +25,7 @@ const initWebsocket = () => {
ws.send('{"op":"Subscribe", "payload": "Transactions"}');
});

ws.on('error', console.error);
ws.on('error', logError);

ws.on('message', async (data) => {
const json = JSON.parse(data);
Expand Down Expand Up @@ -83,7 +83,14 @@ async function indexContract(ownerId, height) {
}

const indexSigners = async (height = 0, url = `/v2/txs?scope=gen:${height}-${Number.MAX_SAFE_INTEGER}&direction=forward&type=paying_for&limit=10`) => {
const { data, next } = await fetch(`${process.env.MIDDLEWARE_URL}${url}`).then((res) => res.json());
let data;
let next;
try {
({ data, next } = await (await fetch(`${process.env.MIDDLEWARE_URL}${url}`)).json());
} catch (e) {
logError('indexSigners[fetchFromMiddleware]', url, e);
throw e;
}

const checkCursor = height + ';' + data.length + ';' + next;
if (latestCheckedCursor === checkCursor) {
Expand All @@ -108,7 +115,7 @@ const indexSigners = async (height = 0, url = `/v2/txs?scope=gen:${height}-${Num
});
} catch (e) {
// there will be cases that we check, but not of our contract, that then throw, ignore them
console.error(ownerId, e.message);
logError('indexSigners[indexContract]', ownerId, e.message);
process.stdout.write('-');
}

Expand Down Expand Up @@ -136,14 +143,18 @@ const createTransaction = async (hash, tx) => {
try {
unpackTx(tx);
} catch (e) {
console.error(e);
logError('createTransaction[unpackTx]', tx, hash, e);
throw new TxUnpackFailedError();
}

const computedHash = (
await buildAuthTxHash(tx, { onNode: node, ...GA_META_PARAMS })
).toString('hex');
if (computedHash !== hash) throw new TxHashNotMatchingError();
try {
const computedHash = (
await buildAuthTxHash(tx, { onNode: node, ...GA_META_PARAMS })
).toString('hex');
if (computedHash !== hash) throw new TxHashNotMatchingError();
} catch (e) {
logError('createTransaction[buildAuthTxHash]', e);
throw e;
}
return Tx.create({ hash, tx }).catch((e) => {
if (e.errors?.some((e) => e.validatorKey === 'not_unique')) throw new HashAlreadyExistentError();
else throw e;
Expand Down
6 changes: 6 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ class TxHashNotMatchingError extends Error {
}
}

function logError(...error) {
console.log('Error:\n');
console.error(...error);
}

module.exports = {
HashAlreadyExistentError,
TxUnpackFailedError,
TxHashNotMatchingError,
logError,
};
Loading