Repository: busyorg/busy (js)

Issue:
When trying to vote with 1%, system shows unknown error has occured
<!--
Thanks for opening an issue! To help the team to understand your needs, please complete the below template to ensure we have the necessary details to assist you. 
-->

#### Expected behavior

Site should show proper error message when there is an error. This will allow user to correct the problem.

#### Actual behavior

If the user has small amount of SP (less than 100), trying to vote with small vote percent throws an Unknown error has occured. Console displayed the error as 
`Unknown error info->abs_rshares > STEEM_VOTE_DUST_THRESHOLD || vote_weight == 0: Voting weight is too small, please accumulate more voting power or steem power.`

#### How to reproduce

- Login with an account with low SP. Mine has 73.
- Go to settings page and enable Voting Power slider.
- Click on the up-vote button on any post. This is show the voting slider.
- Drag the voting slider to 1% and click confirm.
- An `Unknown Error has occured` message is displayed on the screen.
- Open dev console to see actual error.

![image](https://user-images.githubusercontent.com/13856451/41518018-6ffcd164-7274-11e8-84d1-2ba667446818.png)

* Browser: Google Chrome ( 66.0.3359.181 Version)
* Operating system: OSX 10.11.6


Pull request:
test: add errorMiddleware tests
Fixes #1977

### Changes

* Add `parseBlockChainError` tests

Patch:
diff --git a/src/client/errorMiddleware.js b/src/client/helpers/errorMiddleware.js
similarity index 81%
rename from src/client/errorMiddleware.js
rename to src/client/helpers/errorMiddleware.js
index 908371cc..aa0af2cf 100644
--- a/src/client/errorMiddleware.js
+++ b/src/client/helpers/errorMiddleware.js
@@ -1,8 +1,8 @@
 import _ from 'lodash';
-import errors from '../common/constants/errors';
-import { notify } from './app/Notification/notificationActions';
+import errors from '../../common/constants/errors';
+import { notify } from '../app/Notification/notificationActions';
 
-function parseBlockChainError(error) {
+export function parseBlockChainError(error) {
   const errorType = _.find(errors, e => error.includes(e.fingerprint));
 
   if (_.has(errorType, 'message')) {
@@ -13,7 +13,7 @@ function parseBlockChainError(error) {
     return error.slice(idx + 1).trim();
   }
   console.log(error);
-  return 'Unknown error';
+  return 'Unknown error.';
 }
 
 export default function errorMiddleware({ dispatch }) {
diff --git a/src/client/store.js b/src/client/store.js
index d5b1cd8c..260e9466 100644
--- a/src/client/store.js
+++ b/src/client/store.js
@@ -5,7 +5,7 @@ import { routerMiddleware } from 'react-router-redux';
 import steemAPI from './steemAPI';
 import createBusyAPI from '../common/services/createBusyAPI';
 import history from './history';
-import errorMiddleware from './errorMiddleware';
+import errorMiddleware from './helpers/errorMiddleware';
 import createReducer from './reducers';
 
 export default steemConnectAPI => {
diff --git a/src/common/constants/errors.js b/src/common/constants/errors.js
index 176a3a15..989e02cb 100644
--- a/src/common/constants/errors.js
+++ b/src/common/constants/errors.js
@@ -13,7 +13,7 @@ export default {
   },
   DUPLICATE_REBLOG: {
     fingerprint: 'Account has already reblogged this post',
-    message: 'You have already reblogged this post',
+    message: 'You have already reblogged this post.',
   },
   POST_TOO_BIG: {
     fingerprint: '<= (get_dynamic_global_properties().maximum_block_size - 256)',


Test patch:
diff --git a/src/client/helpers/__tests__/errorMiddleware.js b/src/client/helpers/__tests__/errorMiddleware.js
new file mode 100644
index 00000000..5f43e975
--- /dev/null
+++ b/src/client/helpers/__tests__/errorMiddleware.js
@@ -0,0 +1,58 @@
+import { parseBlockChainError } from '../errorMiddleware';
+
+describe('parseBlockChainError', () => {
+  it('should fingerprint errors', () => {
+    let actual = parseBlockChainError(
+      '( now - auth.last_root_post ) > STEEM_MIN_ROOT_COMMENT_INTERVAL: You may only post once every 5 minutes.',
+    );
+    let expected = 'You may only post once every 5 minutes.';
+    expect(actual).toEqual(expected);
+
+    actual = parseBlockChainError(
+      '(now - auth.last_post) > STEEM_MIN_REPLY_INTERVAL: You may only comment once every 20 seconds.',
+    );
+    expected = 'You may only comment once every 20 seconds.';
+    expect(actual).toEqual(expected);
+
+    actual = parseBlockChainError(
+      'vote.vote_percent != o.weight: You have already voted in a similar way.',
+    );
+    expected = 'You have already voted in a similar way.';
+    expect(actual).toEqual(expected);
+
+    actual = parseBlockChainError(
+      'blog_itr == blog_comment_idx.end(): Account has already reblogged this post',
+    );
+    expected = 'You have already reblogged this post.';
+    expect(actual).toEqual(expected);
+
+    actual = parseBlockChainError(
+      '10 assert_exception: Assert Exception\nfc::raw::pack_size(trx) <= (get_dynamic_global_properties().maximum_block_size - 256): \n {}\n th_a database.cpp:642 push_transaction',
+    );
+    expected = 'Your post is too big.';
+    expect(actual).toEqual(expected);
+
+    actual = parseBlockChainError(
+      'Account: account bandwidth limit exceeded. Please wait to transact or power up STEEM.',
+    );
+    expected = 'Your bandwith has been exceeded. Please wait to transact or power up STEEM.';
+    expect(actual).toEqual(expected);
+
+    actual = parseBlockChainError(
+      'info->abs_rshares > STEEM_VOTE_DUST_THRESHOLD || vote_weight == 0: Voting weight is too small, please accumulate more voting power or steem power.',
+    );
+    expected =
+      'Your voting power is too small, please accumulate more voting power or steem power.';
+    expect(actual).toEqual(expected);
+  });
+
+  it('should extract message from unknown error', () => {
+    expect(parseBlockChainError('assert 122 < 2: This is some error.')).toEqual(
+      'This is some error.',
+    );
+  });
+
+  it('should return unknown error when error is in wrong format', () => {
+    expect(parseBlockChainError('this is not good')).toEqual('Unknown error.');
+  });
+});