500 Internal server with npm package on getNFTMetadata, bypassing try/catch statement outside of the npm package
Closed this issue · 4 comments
Details:
Error thrown on line 58 of sendRestPayloaddist/cjs/web3-adapter/sendRestPayload.js seems to bypass outside try catch statements of outside program calling web3.alchemy.getNftMetadata.
Whenever this error occurs and a 500 internal server error comes back from a call, the outside program exists immediately without entering any outside try/catch statements, even if the whole program is wrapped in try catch. Also no luck checking for promise rejections. Could this be because this error is coming from another thread or something?
Full error msg is as follows :
throw new Error(response.status + ":" + response.statusText);
^
Error: 500:Error getting NFT Metadata
at Object.<anonymous> (/home/dak/Desktop/wise/contract_repos/merkle-tree/liquid-pool-merkle-price-tree/generate/node_modules/@alch/alchemy-web3/dist/cjs/web3-adapter/sendRestPayload.js:58:35)
at step (/home/dak/Desktop/wise/contract_repos/merkle-tree/liquid-pool-merkle-price-tree/generate/node_modules/tslib/tslib.js:143:27)
at Object.next (/home/dak/Desktop/wise/contract_repos/merkle-tree/liquid-pool-merkle-price-tree/generate/node_modules/tslib/tslib.js:124:57)
at fulfilled (/home/dak/Desktop/wise/contract_repos/merkle-tree/liquid-pool-merkle-price-tree/generate/node_modules/tslib/tslib.js:114:62)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)```
@brucdarc Can you provide a repro or a code sample? AFAIK, the library should catch and automatically retry errors.
Sure thing, here is the snippet I have been using. Keep in mind though this has changed a few times as I have tried different error handling strategies such as wrapping the entire program in try catches, and looking at promise rejections, but here it is.
const transformMetadata = (result) => {
let transformed = {
"Identifier" : result.id.tokenId,
"Traits" : {}
}
result.metadata.attributes.forEach( (trait) => {
transformed.Traits[trait.trait_type] = trait.value;
})
return transformed;
}
const queryNftTraits = async (tokenId, contractAddress) => {
const result = await web3.alchemy.getNftMetadata({
contractAddress: contractAddress,
tokenId: tokenId
});
return result;
}
const queryCollection = async (collectionName, collectionContract, startIdentifier, endIdentifier) => {
let datastructure = {
"CollectionName" : collectionName,
"CollectionContract" : collectionContract,
"tokens" : []
}
let promises = [];
let failedIndexes = [];
for (let currentId = startIdentifier; currentId <= endIdentifier; currentId++) {
try {
const prom = queryNftTraits(currentId, collectionContract);
promises.push(prom);
}
catch (error) {
failedIndexes.push(currentId);
console.error("Error querying token " + currentId);
}
}
for (let i = 0; i < promises.length; i++){
try {
const prom = promises[i];
const currentNft = await Promise.resolve(prom);
datastructure.tokens.push(transformMetadata(currentNft));
}
catch (error) {
failedIndexes.push(parseInt(i) + parseInt(startIdentifier));
console.error("ERROR ON TOKEN INDEX " + i);
}
};
for (let i = 0; i < failedIndexes.length; i++){
let index = failedIndexes[i];
try {
console.log("Querying Failed ID " + index);
const currentNft = await queryNftTraits(index, collectionContract);
datastructure.tokens.push(transformMetadata(currentNft));
console.log("Successfully requeried");
}
catch (error) {
console.error("Error re-querying token " + index);
console.error(error);
}
}
return datastructure;
}
console.log("fetching metadata for specified collection");
const args = process.argv.slice(2);
queryCollection(
args[0],
args[1],
args[2],
args[3],
).then( (result) => {
console.log("SAVING TOKENS")
var fs = require('fs');
fs.writeFile(args[0] + ".json", JSON.stringify(result, null, 4), function (err) {
if (err) {
console.log(err);
}
});
})```
@brucdarc I'm having trouble reproducing this locally, even with your code snippet. If it's not too much work, can you create a repro in a package that I could download and run?
closing due to inactivity. Feel free to reply, and I'll open this issue again.