Skip to content

Commit 74b6ed1

Browse files
committed
implementation of EtherscanApiStatusError class for better error handling
1 parent 4025a33 commit 74b6ed1

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
'use strict';
2-
const init = require('./lib/init');
1+
"use strict";
2+
const init = require("./lib/init");
3+
const EtherscanApiStatusError = require("./lib/etherscanApiStatusError");
34

45
module.exports = {
5-
init
6+
init,
7+
EtherscanApiStatusError,
68
};

lib/etherscanApiStatusError.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = class EtherscanApiStatusError {
2+
constructor(message) {
3+
this.message = message;
4+
}
5+
};

lib/get-request.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
const axios = require("axios");
2+
const EtherscanApiStatusError = require("./etherscanApiStatusError");
13

2-
const axios = require('axios');
34
/**
45
* @param {string} chain
56
* @returns {string}
@@ -12,55 +13,60 @@ function pickChainUrl(chain) {
1213
return TESTNET_API_URL_MAP[chain];
1314
}
1415

15-
16-
const MAIN_API_URL = 'https://api.etherscan.io';
16+
const MAIN_API_URL = "https://api.etherscan.io";
1717
const TESTNET_API_URL_MAP = {
18-
ropsten: 'https://api-ropsten.etherscan.io',
19-
kovan: 'https://api-kovan.etherscan.io',
20-
rinkeby: 'https://api-rinkeby.etherscan.io',
21-
homestead: 'https://api.etherscan.io'
18+
ropsten: "https://api-ropsten.etherscan.io",
19+
kovan: "https://api-kovan.etherscan.io",
20+
rinkeby: "https://api-rinkeby.etherscan.io",
21+
homestead: "https://api.etherscan.io",
2222
};
2323

24-
module.exports = function(chain, timeout) {
24+
// Axios automatically throws an error if statusCode is 4xx/5xx
25+
module.exports = function (chain, timeout) {
2526
var client = axios.create({
2627
baseURL: pickChainUrl(chain),
27-
timeout: timeout
28+
timeout: timeout,
2829
});
2930

3031
/**
3132
* @param query
3233
* @returns {Promise<any>}
3334
*/
3435
function getRequest(query) {
35-
return new Promise(function(resolve, reject) {
36-
client.get('/api?' + query).then(function(response) {
37-
var data = response.data;
36+
return new Promise(function (resolve, reject) {
37+
client
38+
.get("/api?" + query)
39+
.then(function (response) {
40+
var data = response.data;
41+
if (data.status && data.status != 1) {
42+
let returnMessage = data.message || "NOTOK";
43+
if (
44+
data.hasOwnProperty("result") &&
45+
typeof data.result === "string"
46+
) {
47+
returnMessage = data.result;
48+
} else if (data.message && typeof data.message === "string") {
49+
returnMessage = data.message;
50+
}
3851

39-
if (data.status && data.status != 1) {
40-
let returnMessage = data.message ||'NOTOK';
41-
if (data.result && typeof data.result === 'string') {
42-
returnMessage = data.result;
43-
} else if (data.message && typeof data.message === 'string') {
44-
returnMessage = data.message;
52+
return reject(new EtherscanApiStatusError(returnMessage));
4553
}
4654

47-
return reject(returnMessage);
48-
}
55+
if (data.error) {
56+
var message = data.error;
4957

50-
if (data.error) {
51-
var message = data.error;
58+
if (typeof data.error === "object" && data.error.message) {
59+
message = data.error.message;
60+
}
5261

53-
if(typeof data.error === 'object' && data.error.message){
54-
message = data.error.message;
62+
return reject(new Error(message));
5563
}
5664

57-
return reject(new Error(message));
58-
}
59-
60-
resolve(data);
61-
}).catch(function(error) {
62-
return reject(new Error(error));
63-
});
65+
resolve(data);
66+
})
67+
.catch(function (error) {
68+
return reject(new Error(error));
69+
});
6470
});
6571
}
6672

0 commit comments

Comments
 (0)