From 81f02b853d204ae3c04b3b6b23f75411c934124e Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 23 May 2019 18:03:20 +0800 Subject: [PATCH 1/2] refactor(neuron-wallet): refactor helpers controller and networks controller with CatchControllerErr handle controller error in CatchControllerError --- .../neuron-wallet/src/controllers/helpers.ts | 22 +- .../neuron-wallet/src/controllers/index.ts | 1 + .../neuron-wallet/src/controllers/networks.ts | 199 ++++++++++-------- packages/neuron-wallet/src/locales/en.ts | 8 + packages/neuron-wallet/src/locales/zh.ts | 7 + .../neuron-wallet/src/utils/decorators.ts | 22 ++ 6 files changed, 163 insertions(+), 96 deletions(-) create mode 100644 packages/neuron-wallet/src/utils/decorators.ts diff --git a/packages/neuron-wallet/src/controllers/helpers.ts b/packages/neuron-wallet/src/controllers/helpers.ts index 0fecdbff21..78109194ae 100644 --- a/packages/neuron-wallet/src/controllers/helpers.ts +++ b/packages/neuron-wallet/src/controllers/helpers.ts @@ -1,11 +1,26 @@ import Key from '../keys/key' import { ResponseCode } from '.' +import { CatchControllerError } from '../utils/decorators' +import i18n from '../utils/i18n' export enum HelpersMethod { GenerateMnemonic = 'generateMnemonic', } + +/** + * @class HelpersController + * @description handle messages from helpers channel + */ class HelpersController { - public static generateMnemonic = (): Controller.Response => { + /** + * @method generateMnemonic + * @static + * @memberof HelpersController + * @description generate mnemonic + */ + + @CatchControllerError + public static async generateMnemonic() { const mnemonic = Key.generateMnemonic() if (mnemonic) { return { @@ -13,10 +28,7 @@ class HelpersController { result: mnemonic, } } - return { - status: ResponseCode.Fail, - result: 'Failed to generate mnemonic', - } + throw new Error(i18n.t('messages.failed-to-create-mnemonic')) } } diff --git a/packages/neuron-wallet/src/controllers/index.ts b/packages/neuron-wallet/src/controllers/index.ts index ff95231bfe..a7cabf8050 100644 --- a/packages/neuron-wallet/src/controllers/index.ts +++ b/packages/neuron-wallet/src/controllers/index.ts @@ -7,6 +7,7 @@ export enum ResponseCode { Fail, Success, } + export const methods = { NetworksMethod, WalletsMethod, diff --git a/packages/neuron-wallet/src/controllers/networks.ts b/packages/neuron-wallet/src/controllers/networks.ts index 3f851b5c88..5886f05895 100644 --- a/packages/neuron-wallet/src/controllers/networks.ts +++ b/packages/neuron-wallet/src/controllers/networks.ts @@ -1,5 +1,7 @@ import { ResponseCode } from '.' import NetworksService, { NetworkType, NetworkID, Network } from '../services/networks' +import { CatchControllerError } from '../utils/decorators' +import i18n from '../utils/i18n' export enum NetworksMethod { GetAll = 'getAll', @@ -13,107 +15,118 @@ export enum NetworksMethod { Status = 'status', } +/** + * @class NetworksController + * @description handle messages from networks channel + */ class NetworksController { static service = new NetworksService() - public static getAll = async () => { + /** + * @method getAll + * @static + * @memberof NetworksController + * @description return all networks if possible + */ + + @CatchControllerError + public static async getAll() { + const networks = await NetworksController.service.getAll() return { status: ResponseCode.Success, - result: await NetworksController.service.getAll(), + result: networks, } } - public static get = async (id: NetworkID) => { + /** + * @method get + * @static + * @memberof NetworksController + * @description get netowrk by id + */ + @CatchControllerError + public static async get(id: NetworkID) { + if (typeof id === 'undefined') throw new Error(i18n.t('messages.id-is-required')) + const network = await NetworksController.service.get(id) - if (network) { - return { - status: ResponseCode.Success, - result: network, - } - } + if (!network) throw new Error(i18n.t('messages.network-of-id-is-not-found', { id })) + return { - status: ResponseCode.Fail, - msg: `Network of id ${id} is not found`, + status: ResponseCode.Success, + result: network, } } - public static create = async ({ name, remote, type = NetworkType.Normal }: Network) => { - if (!name || !remote) { - return { - status: ResponseCode.Fail, - msg: 'Name and remote are required', - } - } - // example for return error - if (name === 'error') { - return { - status: ResponseCode.Fail, - msg: `Name cannot be "error"`, - } - } - try { - const created = await NetworksController.service.create(name, remote, type) - return { - status: ResponseCode.Success, - result: created, - } - } catch (err) { - return { - status: ResponseCode.Fail, - msg: err.message, - } + /** + * + * @method create + * @static + * @memberof NetworksController + * @description create network with name, remote address, netowrk type + */ + @CatchControllerError + public static async create({ name, remote, type = NetworkType.Normal }: Network) { + if (!name || !remote) throw new Error(i18n.t('messages.name-and-remote-address-are-required')) + if (name === 'error') throw new Error(i18n.t('messages.invalid-name')) + + const created = await NetworksController.service.create(name, remote, type) + return { + status: ResponseCode.Success, + result: created, } } - public static update = async (id: NetworkID, options: Partial) => { - try { - await NetworksController.service.update(id, options) - return { - status: ResponseCode.Success, - result: true, - } - } catch (err) { - return { - status: ResponseCode.Fail, - msg: err.message, - } + /** + * @method update + * @static + * @memberof NetworksController + * @description update network by id + */ + @CatchControllerError + public static async update(id: NetworkID, options: Partial) { + if (options.name && options.name === 'error') throw new Error(i18n.t('messages.invalid-name')) + + await NetworksController.service.update(id, options) + return { + status: ResponseCode.Success, + result: true, } } - public static delete = async (id: NetworkID) => { + /** + * @method delete + * @static + * @memberof NetworksController + * @description delete network by id + */ + @CatchControllerError + public static async delete(id: NetworkID) { const defaultNetwork = await NetworksController.service.defaultOne() - if (defaultNetwork && defaultNetwork.id === id) { - return { - status: ResponseCode.Fail, - msg: 'Default network is unremovable', - } - } - try { - const activeId = await NetworksController.service.activeId() - if (activeId === id) { - if (!defaultNetwork) { - return { - status: ResponseCode.Fail, - msg: 'Default network is not set, cannot delete active network', - } - } - await NetworksController.service.delete(id) - await NetworksController.activate(defaultNetwork.id) - } + + if (defaultNetwork && defaultNetwork.id === id) throw new Error(i18n.t('messages.default-network-is-unremovable')) + + const activeId = await NetworksController.service.activeId() + if (activeId === id) { + if (!defaultNetwork) throw new Error('messages.cannot-delete-active-network-due-to-lack-of-default-one') await NetworksController.service.delete(id) - return { - status: ResponseCode.Success, - result: true, - } - } catch (err) { - return { - status: ResponseCode.Fail, - msg: err.message, - } + await NetworksController.activate(defaultNetwork.id) + } + await NetworksController.service.delete(id) + + return { + status: ResponseCode.Success, + result: true, } } - public static activeOne = async () => { + /** + * @method activeOne + * @static + * @memberof NetworksController + * @description get the currecnt/active network id + */ + @CatchControllerError + public static async activeOne() { const activeId = await NetworksController.service.activeId() if (activeId) { return { @@ -121,28 +134,32 @@ class NetworksController { result: activeId, } } - return { - status: ResponseCode.Fail, - msg: 'Active network is not set', - } + throw new Error(i18n.t('messages.active-network-is-not-set')) } - public static activate = async (id: NetworkID) => { - try { - await NetworksController.service.activate(id) - } catch (err) { - return { - status: ResponseCode.Fail, - msg: err.message, - } - } + /** + * @method activate + * @static + * @memberof NetworksController + * @description set the current/active network by id + */ + @CatchControllerError + public static async activate(id: NetworkID) { + await NetworksController.service.activate(id) return { status: ResponseCode.Success, result: true, } } - public static clear = async () => { + /** + * @mehtod clear + * @static + * @memberof NetworksController + * @description clear the networks + */ + @CatchControllerError + public static async clear() { await NetworksController.service.clear() return { status: ResponseCode.Success, diff --git a/packages/neuron-wallet/src/locales/en.ts b/packages/neuron-wallet/src/locales/en.ts index bbbe49f7cf..b6b6f91f37 100644 --- a/packages/neuron-wallet/src/locales/en.ts +++ b/packages/neuron-wallet/src/locales/en.ts @@ -53,6 +53,14 @@ export default { 'failed-to-activate-wallet': 'Failed to activate wallet', 'failed-to-delete-wallet': 'Failed to delete wallet', 'wallet-name-existed': 'Wallet name existed', + 'network-of-id-is-not-found': 'Network of id {{id}} is not found', + 'id-is-required': 'ID is required', + 'name-and-remote-address-are-required': 'Name and remote address are required', + 'invalid-name': 'Name is invalid', + 'default-network-is-unremovable': 'Default network is unremovable', + 'cannot-delete-active-network-due-to-lack-of-default-one': + 'Cannot delete active network due to lack of default one', + 'active-network-is-not-set': 'Active network is not set', }, }, } diff --git a/packages/neuron-wallet/src/locales/zh.ts b/packages/neuron-wallet/src/locales/zh.ts index dab0deb5c9..880e91c3dc 100644 --- a/packages/neuron-wallet/src/locales/zh.ts +++ b/packages/neuron-wallet/src/locales/zh.ts @@ -52,6 +52,13 @@ export default { 'failed-to-activate-wallet': '设置默认钱包失败', 'failed-to-delete-wallet': '删除钱包失败', 'wallet-name-existed': '钱包名称重复', + 'network-of-id-is-not-found': '未找到 Id 为 {{id}} 的网络设置', + 'id-is-required': '缺少参数 ID', + 'name-and-remote-address-are-required': '缺少名称或地址', + 'invalid-name': '非法名称', + 'default-network-is-unremovable': '默认网络不可删除', + 'cannot-delete-active-network-due-to-lack-of-default-one': '未设置默认网络, 因此无法删除当前网络', + 'active-network-is-not-set': '未设置当前网络', }, }, } diff --git a/packages/neuron-wallet/src/utils/decorators.ts b/packages/neuron-wallet/src/utils/decorators.ts new file mode 100644 index 0000000000..7cd11c1ae8 --- /dev/null +++ b/packages/neuron-wallet/src/utils/decorators.ts @@ -0,0 +1,22 @@ +import { ResponseCode } from '../controllers' + +export const CatchControllerError = (_target: any, _name: string, descripor: PropertyDescriptor) => { + const originalMethod = descripor.value + return { + ...descripor, + async value(...args: any[]) { + try { + return await originalMethod.apply(this, args) + } catch (err) { + return { + status: ResponseCode.Fail, + msg: err.message, + } + } + }, + } +} + +export default { + CatchControllerError, +} From 2d99096a4af44ef6b972125cbc47f29c9e8d0da9 Mon Sep 17 00:00:00 2001 From: Keith Date: Thu, 23 May 2019 18:34:21 +0800 Subject: [PATCH 2/2] refactor(neuron-wallet): refactor transactions controller with CatchControllerError --- .../src/controllers/transactions.ts | 61 +++++++++++-------- packages/neuron-wallet/src/locales/en.ts | 2 + packages/neuron-wallet/src/locales/zh.ts | 2 + 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/neuron-wallet/src/controllers/transactions.ts b/packages/neuron-wallet/src/controllers/transactions.ts index c50a2e2887..f4fac2d9ba 100644 --- a/packages/neuron-wallet/src/controllers/transactions.ts +++ b/packages/neuron-wallet/src/controllers/transactions.ts @@ -5,21 +5,25 @@ import TransactionsService, { PaginationResult, TransactionsByLockHashesParam, } from '../services/transactions' +import { CatchControllerError } from '../utils/decorators' +import i18n from '../utils/i18n' export default class TransactionsController { static service = new TransactionsService() - public static getAll = async ( + /** + * @method getAll + * @static + * @memberof TransactionsController + * @description get all transactions + */ + @CatchControllerError + public static async getAll( params: TransactionsByLockHashesParam, - ): Promise>> => { + ): Promise>> { const transactions = await TransactionsService.getAll(params) - if (!transactions) { - return { - status: ResponseCode.Fail, - msg: 'Transactions not found', - } - } + if (!transactions) throw new Error(i18n.t('messages.no-response-from-transaction-service')) return { status: ResponseCode.Success, @@ -27,31 +31,38 @@ export default class TransactionsController { } } - public static getAllByAddresses = async ( + /** + * @method getAllByAddresses + * @static + * @memberof TransactionsController + * @description get all transactions by page number, page size, address list + */ + @CatchControllerError + public static async getAllByAddresses( params: TransactionsByAddressesParam, - ): Promise>> => { + ): Promise>> { const transactions = await TransactionsService.getAllByAddresses(params) - if (transactions) { - return { - status: ResponseCode.Success, - result: { ...params, ...transactions }, - } - } + if (!transactions) throw new Error(i18n.t('messages.no-response-from-transaction-service')) + return { - status: ResponseCode.Fail, - msg: 'Transactions not found', + status: ResponseCode.Success, + result: { ...params, ...transactions }, } } - public static get = async (hash: string): Promise> => { + /** + * @method get + * @static + * @memberof TransactionsController + * @description get transaction by hash + */ + @CatchControllerError + public static async get(hash: string): Promise> { const transaction = await TransactionsService.get(hash) - if (!transaction) { - return { - status: ResponseCode.Fail, - msg: 'Transaction not found', - } - } + + if (!transaction) throw new Error(i18n.t('messages.transaction-is-not-found', { hash })) + return { status: ResponseCode.Success, result: transaction, diff --git a/packages/neuron-wallet/src/locales/en.ts b/packages/neuron-wallet/src/locales/en.ts index b6b6f91f37..a887f43460 100644 --- a/packages/neuron-wallet/src/locales/en.ts +++ b/packages/neuron-wallet/src/locales/en.ts @@ -61,6 +61,8 @@ export default { 'cannot-delete-active-network-due-to-lack-of-default-one': 'Cannot delete active network due to lack of default one', 'active-network-is-not-set': 'Active network is not set', + 'no-response-from-transaction-service': 'No response from transaction service', + 'transaction-is-not-found': 'Transaction {{hash}} is not found', }, }, } diff --git a/packages/neuron-wallet/src/locales/zh.ts b/packages/neuron-wallet/src/locales/zh.ts index 880e91c3dc..9c8379cc18 100644 --- a/packages/neuron-wallet/src/locales/zh.ts +++ b/packages/neuron-wallet/src/locales/zh.ts @@ -59,6 +59,8 @@ export default { 'default-network-is-unremovable': '默认网络不可删除', 'cannot-delete-active-network-due-to-lack-of-default-one': '未设置默认网络, 因此无法删除当前网络', 'active-network-is-not-set': '未设置当前网络', + 'no-response-from-transaction-service': '交易服务未响应', + 'transaction-is-not-found': '未找到交易 {{hash}}', }, }, }