Skip to content

Commit

Permalink
refactor(subscriptions): move 'SubscribeErrorReason' to type file, re…
Browse files Browse the repository at this point in the history
…format enum type and message function
  • Loading branch information
oceanlvr authored and ysfscream committed Dec 17, 2020
1 parent 6e2b1d5 commit 799c291
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
39 changes: 16 additions & 23 deletions src/components/SubscriptionsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ import { updateConnection } from '@/api/connection'
import { defineColors, getRandomColor } from '@/utils/colors'
import LeftPanel from '@/components/LeftPanel.vue'
import MyDialog from '@/components/MyDialog.vue'
import { ConnectionModel } from '../views/connections/types'
import { ConnectionModel, SubscribeErrorReason } from '../views/connections/types'
import VueI18n from 'vue-i18n'
enum subscribeErrorReason {
normal = 0,
qosSubFailed = 1 << 0, // qos is abnormal
qosSubSysFailed = 1 << 1, // qos is abnormal becauseof $SYS subscribe
emptySubFailed = 1 << 2, // subscription returns empty array
}
@Component({
components: {
LeftPanel,
Expand Down Expand Up @@ -208,24 +203,24 @@ export default class SubscriptionsList extends Vue {
/**
* Get the error reason message corresponding to the enumeration.
* Check that errorReason not equal `subscribeErrorReason.normal` before using.
* Check that errorReason not equal `SubscribeErrorReason.normal` before using.
* @return Return the message of failure subscribe
* @param errorReason - Type:enum, The reason cause the failed subscription
*/
private getErrorReasonMsg(errorReason: subscribeErrorReason): VueI18n.TranslateResult {
if (errorReason === subscribeErrorReason.normal) return ''
private getErrorReasonMsg(errorReason: SubscribeErrorReason): VueI18n.TranslateResult {
if (errorReason === SubscribeErrorReason.normal) return ''
switch (errorReason) {
case errorReason & subscribeErrorReason.qosSubFailed: {
case errorReason & SubscribeErrorReason.qosSubFailed: {
return this.$t('connections.qosSubFailed')
}
case errorReason & subscribeErrorReason.qosSubSysFailed: {
case errorReason & SubscribeErrorReason.qosSubSysFailed: {
return this.$t('connections.qosSubSysFailed')
}
case errorReason & subscribeErrorReason.emptySubFailed: {
case errorReason & SubscribeErrorReason.emptySubFailed: {
return this.$t('connections.emptySubFailed')
}
}
return 'Unknow Failed'
return this.$t('connections.unknowSubFailed')
}
public subscribe({ topic, qos }: SubscriptionModel, isAuto?: boolean) {
Expand All @@ -240,18 +235,16 @@ export default class SubscriptionsList extends Vue {
return false
}
let errorReason: subscribeErrorReason = subscribeErrorReason.normal
let errorReason: SubscribeErrorReason = SubscribeErrorReason.normal
if (res.length < 1) {
errorReason += subscribeErrorReason.emptySubFailed
}
if (![0, 1, 2].includes(res[0].qos) && topic.match('/^($SYS)//i')?.length) {
errorReason += subscribeErrorReason.qosSubSysFailed
}
if (![0, 1, 2].includes(res[0].qos)) {
errorReason += subscribeErrorReason.qosSubFailed
errorReason = SubscribeErrorReason.emptySubFailed
} else if (![0, 1, 2].includes(res[0].qos) && !topic.match('/^($SYS)//i')) {
errorReason = SubscribeErrorReason.qosSubSysFailed
} else if (![0, 1, 2].includes(res[0].qos)) {
errorReason = SubscribeErrorReason.qosSubFailed
}
if (errorReason !== subscribeErrorReason.normal) {
if (errorReason !== SubscribeErrorReason.normal) {
const errorReasonMsg: VueI18n.TranslateResult = this.getErrorReasonMsg(errorReason)
const errorMsg: string = `${topic} ${this.$t('connections.subFailed')} ${errorReasonMsg}`
this.$message.error(errorMsg)
Expand Down
18 changes: 12 additions & 6 deletions src/lang/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,26 @@ export default {
ja: 'サブスクリプションが失敗しました',
},
qosSubSysFailed: {
zh: '错误的 QOS,MQTT Broker 拒绝了订阅。请检查 ACL 配置',
en: 'Unexpected QOS, MQTT Broker declined the subscription. Please check ACL configuration',
ja: '予期しないQOS、MQTT Brokerはサブスクリプションを拒否しました。ACL構成を確認してください',
zh: '拒绝了$SYS主题,错误的 QoS,MQTT Broker 拒绝了订阅。请检查 ACL 配置',
en: 'Rejected the $SYS topic,Unexpected QoS, MQTT Broker declined the subscription. Please check ACL configuration',
ja:
'$SYSトピックを拒否しま。した予期しないQoS、MQTT Brokerはサブスクリプションを拒否しました。ACL構成を確認してください',
},
qosSubFailed: {
zh: '错误的 QOS',
en: 'Unexpected QOS',
ja: '予期しないQOS',
zh: '错误的 QoS',
en: 'Unexpected QoS',
ja: '予期しないQoS',
},
emptySubFailed: {
zh: '订阅为空',
en: 'Subscription is empty',
ja: 'サブスクリプションは空です',
},
unknowSubFailed: {
zh: '未知的订阅错误',
en: 'Unknown subscription error',
ja: '不明なサブスクリプションエラー',
},
connected: {
zh: '已连接',
en: 'Connected',
Expand Down
7 changes: 7 additions & 0 deletions src/views/connections/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ export interface ChartDataModel {
recevied: number
sent: number
}

export enum SubscribeErrorReason {
normal,
qosSubFailed, // qos is abnormal
qosSubSysFailed, // qos is abnormal becauseof $SYS subscribe
emptySubFailed, // subscription returns empty array
}

0 comments on commit 799c291

Please sign in to comment.