OSDN Git Service

fix request url args
authorxuhongxin <xuhongxin@luojilab.com>
Sun, 28 Oct 2018 06:24:04 +0000 (14:24 +0800)
committerxuhongxin <xuhongxin@luojilab.com>
Sun, 28 Oct 2018 06:24:04 +0000 (14:24 +0800)
package.json
src/sdk/accounts.js
src/sdk/keys.js
src/sdk/transaction.js
src/utils/http.js

index 1f6139d..b2ef3ba 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "bytom-js-sdk",
-  "version": "1.3.5",
+  "version": "1.3.6",
   "description": "Bytom JS SDK",
   "main": "src/index.js",
   "keywords": [
index 4b6758d..9c82dbe 100644 (file)
@@ -1,7 +1,6 @@
 import {getDB} from '../db/db';
 import {createAccount, createAccountReceiver} from '../wasm/func';
-import {handleAxiosError} from '../utils/http';
-
+import {handleApiError, handleAxiosError} from '../utils/http';
 function accountsSDK(bytom){
     this.http = bytom.serverHttp;
     this.bytom = bytom;
@@ -75,6 +74,10 @@ accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) {
             pm.label = label;
         }
         this.http.request('account/new-address', pm, net).then(resp => {
+            if (resp.status !== 200) {
+                reject(handleApiError(resp));
+                return;
+            }
             let dbData = resp.data.data;
             dbData.guid = guid;
             dbData.net = net;
@@ -125,6 +128,10 @@ accountsSDK.prototype.createAccountUseServer = function(rootXPub, alias, label)
                     pm.label = label;
                 }
                 that.http.request('account/create', pm, net).then(resp => {
+                    if (resp.status !== 200) {
+                        reject(handleApiError(resp));
+                        return;
+                    }
                     let dbData = resp.data.data;
                     dbData.rootXPub = rootXPub;
                     dbData.alias = alias;
index 74614b7..33e3752 100644 (file)
@@ -125,7 +125,7 @@ keysSDK.prototype.create = function(alias, password) {
                 .get(normalizedAlias);
             getRequest.onsuccess = function (e) {
                 if (e.target.result) {
-                    reject(new Error('alias already exists'));
+                    reject(new Error('key alias already exists'));
                     return;
                 }
                 let data = {};
index e9224be..84d8a62 100644 (file)
@@ -1,5 +1,5 @@
 import { signTransaction1 } from '../wasm/func';
-import { handleAxiosError } from '../utils/http';
+import { handleApiError, handleAxiosError } from '../utils/http';
 import { getDB } from '../db/db';
 
 function transactionSDK(bytom) {
@@ -25,13 +25,16 @@ transactionSDK.prototype.list = function(guid, address, start, limit) {
         if (address) {
             pm.address = address;
         }
+        let url = 'merchant/list-transactions';
+        let args = new URLSearchParams();
         if (typeof start !== 'undefined') {
-            pm.start = start;
+            args.append('start', start);
         }
         if (limit) {
-            pm.limit = limit;
+            args.append('limit', limit);
         }
-        this.http.request('merchant/list-transactions', pm, net).then(resp => {
+        url = url + '?' + args.toString();
+        this.http.request(url, pm, net).then(resp => {
             resolve(resp.data);
         }).catch(err => {
             reject(handleAxiosError(err));
@@ -53,6 +56,10 @@ transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatu
     let retPromise = new Promise((resolve, reject) => {
         let pm = {guid: guid, raw_transaction: raw_transaction, signatures: signatures};
         this.http.request('merchant/submit-payment', pm, net).then(resp => {
+            if (resp.status !== 200) {
+                reject(handleApiError(resp));
+                return;
+            }
             resolve(resp.data);
         }).catch(err => {
             reject(handleAxiosError(err));
@@ -85,6 +92,10 @@ transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, from,
             pm.fee = fee;
         }
         this.http.request('merchant/build-payment', pm, net).then(resp => {
+            if (resp.status !== 200) {
+                reject(handleApiError(resp));
+                return;
+            }
             resolve(resp.data);
         }).catch(err => {
             reject(handleAxiosError(err));
index bfe392d..d0df633 100644 (file)
@@ -1,7 +1,15 @@
 export function handleAxiosError(error) {
-    if (error.response) {
+    if (error.response && error.response.data.hasOwnProperty('error')) {
         return new Error(error.response.data.error);
     } else {
-        return error;  
+        return error;
+    }
+}
+
+export function handleApiError(response) {
+    if (response.data.hasOwnProperty('error')) {
+        return new Error(response.data.error);
+    } else {
+        return new Error('Unknow error');
     }
 }
\ No newline at end of file