Skip to content

Commit

Permalink
fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
you21979 committed Nov 25, 2016
1 parent 1daff48 commit 478d0c1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,51 @@ Please note that there is a default limit of 6 calls per second. If you require
* O public api
* O trade api
* O push api


## Error Handling

* simple error control

```
api.balances().catch(function(e){
console.log(e.message)
})
```

* technical error control

```
var errors = require('@you21979/poloniex.com/errors')
api.balances()
.catch(errors.HttpApiError, function (reason) {
// API ERROR
console.log(reason.message, "API", reason.error_code)
})
.catch(errors.StatusCodeError, function (reason) {
// HTTP STATUS ERROR(404 or 500, 502, etc...)
console.log("HTTP StatusCodeError " + reason.statusCode, "HTTP", reason.statusCode)
})
.catch(errors.RequestError, function (reason) {
// REQUEST ERROR(SYSTEMCALL, TIMEOUT)
console.log(reason.message, "SYSCALL", reason.error.code)
})
.catch(function(e){
// OTHER ERROR
console.log(e.message)
})
```

License
-------

MIT License


Donate
------

```
bitcoin:1DWLJFxmPQVSYER6pjwdaVHfJ98nM76LiN
monacoin:MCEp2NWSFc352uaDc6nQYv45qUChnKRsKK
```
6 changes: 6 additions & 0 deletions errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
var objectutil = require('@you21979/object-util');

module.exports = objectutil.keyMerge({
HttpApiError : require('@you21979/http-api-error'),
}, require('limit-request-promise/errors'));
17 changes: 12 additions & 5 deletions lib/private_api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
var verify = require('@you21979/simple-verify')
var objectutil = require('@you21979/object-util');
var HttpApiError = require('@you21979/http-api-error');
var qstring = require('querystring');
var constant = require('./constant');
var lp = require('./system').lp;
Expand All @@ -18,14 +19,15 @@ var createParameter = function(method, nonce, params){
return qstring.stringify(objectutil.keyMerge({ command: method, nonce: nonce }, params));
}

var createPostOption = function(url, nonce, api_key, secret_key, user_agent, method, params){
var createPostOption = function(url, nonce, api_key, secret_key, user_agent, timeout, method, params){
var qs = createParameter(method, nonce, params);
return {
url: url,
method: 'POST',
form: qs,
headers: createHeader(api_key, secret_key, user_agent, qs),
timeout : Math.floor(constant.OPT_TIMEOUT_SEC * 1000),
timeout : timeout,
transform2xxOnly: true,
transform : function(body){
return JSON.parse(body)
}
Expand All @@ -36,17 +38,22 @@ var TradeApi = function(api_key, secret_key, user_agent){
this.name = 'POLONIEX';
this.url = constant.OPT_TRADEAPI_URL;
this.nonce = new Date() * 1;
this.timeout = Math.floor(constant.OPT_TIMEOUT_SEC * 1000);
this.api_key = api_key;
this.secret_key = secret_key;
this.user_agent = user_agent;
}

TradeApi.prototype.query = function(method, mustparams, options){
var params = objectutil.keyMerge(mustparams, options);
return lp.req(createPostOption(this.url, this.incrementNonce(), this.api_key, this.secret_key, this.user_agent, method, params)).
return lp.req(createPostOption(this.url, this.incrementNonce(), this.api_key, this.secret_key, this.user_agent, this.timeout, method, params)).
then(function(v){
if(v.error) throw(new Error(v.error));
else return v
if(v.error){
var error_code = v.error.replace(/ /g, '_').replace('.', '').toUpperCase();
throw new HttpApiError(v.message, "API", error_code, v);
}else{
return v
}
});

}
Expand Down
12 changes: 11 additions & 1 deletion lib/public_api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
var qstring = require('querystring');
var objectutil = require('@you21979/object-util');
var HttpApiError = require('@you21979/http-api-error');
var constant = require('./constant');
var lp = require('./system').lp;

Expand All @@ -20,6 +21,7 @@ var createGetOption = function(url, user_agent, qs){
method: 'GET',
headers: createHeader(user_agent),
timeout : Math.floor(constant.OPT_TIMEOUT_SEC * 1000),
transform2xxOnly: true,
transform : function(body){
return JSON.parse(body)
}
Expand All @@ -29,7 +31,15 @@ var createGetOption = function(url, user_agent, qs){
var query = exports.query = function(method, params){
var user_agent = '';
var url = constant.OPT_RESTAPI_URL;
return lp.req(createGetOption(url, user_agent, createParameter(method, params || {})))
return lp.req(createGetOption(url, user_agent, createParameter(method, params || {}))).
then(function(v){
if(v.error){
var error_code = v.error.replace(/ /g, '_').replace('.', '').toUpperCase();
throw new HttpApiError(v.error, "API", error_code, v);
}else{
return v
}
})
}

var ticker = exports.ticker = function(){
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"author": "Yuki Akiyama <you2197901 [at] gmail.com>",
"name": "@you21979/poloniex.com",
"version": "0.0.6",
"version": "0.0.7",
"private": false,
"dependencies": {
"autobahn" : "0.10.x",
"@you21979/simple-verify" : "0.0.x",
"@you21979/object-util" : "0.0.x",
"@you21979/http-api-error" : "0.0.x",
"limit-request-promise" : "0.1.x",
"request": "^2.34"
},
Expand Down

0 comments on commit 478d0c1

Please sign in to comment.