Skip to content

Commit

Permalink
fix: Default error handling on api helpers (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuliomir authored Sep 2, 2024
1 parent 1d04dbb commit 4437de3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 82 deletions.
8 changes: 4 additions & 4 deletions src/api/addressApiLegacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const addressApi = {
}
return undefined;
})
.catch(_error => {
// something wrong with request
.catch(error => {
console.error(`Failure obtaining address ${address} balance`, error);
});
},

Expand Down Expand Up @@ -49,8 +49,8 @@ const addressApi = {
}
return undefined;
})
.catch(_error => {
// something wrong with request
.catch(error => {
console.error('Failure searching for addresses', error);
});
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/api/graphvizApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const graphvizApi = {
return res.data;
},
res => {
throw new Error(res.data.message);
throw new Error(
res?.data?.message || res?.message || 'Unknown error at get node neighbors'
);
}
);
},
Expand Down
4 changes: 2 additions & 2 deletions src/api/metadataApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const metadataApi = {
}
return undefined;
})
.catch(_error => {
// something wrong with request
.catch(error => {
console.error(`Error fetching dag metadata for ${id}`, error);
});
},
};
Expand Down
66 changes: 34 additions & 32 deletions src/api/nanoApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const nanoApi = {
*/
getState(id, fields, balances, calls) {
const data = { id, fields, balances, calls };
return requestExplorerServiceV1.get(`node_api/nc_state`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/nc_state`, { params: data })
.then(res => res.data)
.catch(err => {
throw new Error(
err?.data?.message || err?.message || `Unknown error on get nc state for ${id}`
);
});
},

/**
Expand All @@ -51,14 +51,12 @@ const nanoApi = {
if (before) {
data.before = before;
}
return requestExplorerServiceV1.get(`node_api/nc_history`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/nc_history`, { params: data })
.then(res => res.data)
.catch(err => {
throw new Error(err?.data?.message || err?.message || `Unknown error on get nc history`);
});
},

/**
Expand All @@ -70,14 +68,16 @@ const nanoApi = {
*/
getBlueprintInformation(blueprintId) {
const data = { blueprint_id: blueprintId };
return requestExplorerServiceV1.get(`node_api/nc_blueprint_information`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/nc_blueprint_information`, { params: data })
.then(res => res.data)
.catch(err => {
throw new Error(
err?.data?.message ||
err?.message ||
`Unknown error on get blueprint data for ${blueprintId}`
);
});
},

/**
Expand All @@ -89,14 +89,16 @@ const nanoApi = {
*/
getBlueprintSourceCode(blueprintId) {
const data = { blueprint_id: blueprintId };
return requestExplorerServiceV1.get(`node_api/nc_blueprint_source_code`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/nc_blueprint_source_code`, { params: data })
.then(res => res.data)
.catch(err => {
throw new Error(
err?.data?.message ||
err?.message ||
`Unknown error on get blueprint source code for ${blueprintId}`
);
});
},
};

Expand Down
6 changes: 3 additions & 3 deletions src/api/networkApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const networkApi = {
return requestExplorerServiceV1
.get(`node`)
.then(res => {
if (!res.data) {
if (!res?.data) {
throw new Error('unknown_error');
}
return res.data;
})
.catch(res => {
throw new Error(res.data.message);
throw new Error(res?.data?.message || res?.message || 'Unknown error on get all nodes');
});
},
getPeer(hash) {
Expand All @@ -31,7 +31,7 @@ const networkApi = {
return res.data;
})
.catch(res => {
throw new Error(res.data.message);
throw new Error(res?.data?.message || res?.message || `Unknown error on get node ${hash}`);
});
},
};
Expand Down
46 changes: 22 additions & 24 deletions src/api/tokenApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,36 @@ import { TX_COUNT } from '../constants';

const tokenApi = {
getList() {
return requestExplorerServiceV1.get(`node_api/tokens`).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/tokens`)
.then(res => res.data)
.catch(err => {
throw new Error(err?.data?.message || err?.message || `Unknown error on get tokens list`);
});
},

get(id) {
const data = { id };
return requestExplorerServiceV1.get(`node_api/token`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/token`, { params: data })
.then(res => res.data)
.catch(err => {
throw new Error(
err?.data?.message || err?.message || `Unknown error on get token data for ${id}`
);
});
},

getHistory(id, timestamp, hash, page) {
const data = { id, timestamp, hash, page, count: TX_COUNT };
return requestExplorerServiceV1.get(`node_api/token_history`, { params: data }).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/token_history`, { params: data })
.then(res => res.data)
.catch(err => {
throw new Error(
err?.data?.message || err?.message || `Unknown error on get history for ${id}`
);
});
},
};

Expand Down
11 changes: 3 additions & 8 deletions src/api/txApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,9 @@ const txApi = {
*/
getConfirmationData(id) {
const data = { id };
return requestExplorerServiceV1.get(`node_api/transaction_acc_weight`, { params: data }).then(
res => {
return res.data;
},
res => {
return Promise.reject(res);
}
);
return requestExplorerServiceV1
.get(`node_api/transaction_acc_weight`, { params: data })
.then(res => res.data);
},
};

Expand Down
14 changes: 6 additions & 8 deletions src/api/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import requestExplorerServiceV1 from './axiosInstance';

const versionApi = {
getVersion() {
return requestExplorerServiceV1.get(`node_api/version`).then(
res => {
return res.data;
},
res => {
throw new Error(res.data.message);
}
);
return requestExplorerServiceV1
.get(`node_api/version`)
.then(res => res.data)
.catch(err => {
throw new Error(err?.data?.message || err?.message || `Unknown error on get node version`);
});
},
};

Expand Down

0 comments on commit 4437de3

Please sign in to comment.