Skip to content

Commit 5e77864

Browse files
Kinplemelonysfscream
authored andcommitted
refactor(plugin): optimize error msg if there are not authorized
1 parent 27c10cf commit 5e77864

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

src/api/plugins.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const queryPluginDetail = (nameWithVersion: string): Promise<PluginDetail
1313
export const installPlugin = (file: File): Promise<void> => {
1414
const form = new FormData()
1515
form.append('plugin', file)
16-
return http.post(`/plugins/install`, form)
16+
return http.post(`/plugins/install`, form, { errorsHandleCustom: [403] })
1717
}
1818

1919
export const uninstallPlugin = (nameWithVersion: string): Promise<void> => {

src/common/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const readBlobResponse = async (data) => {
6868
}
6969
}
7070

71-
const getErrorMessage = (data, status) => {
71+
export const getErrorMessage = (data, status) => {
7272
if (!data) {
7373
return `${status} Network error`
7474
}

src/i18n/Plugins.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export default {
124124
en: 'This plugin does not support configuration via the Dashboard.',
125125
},
126126
pluginInstallGuidance: {
127-
zh: `为确保系统安全,EMQX 不允许直接通过 Dashboard 上传并安装插件。点击“安装”按钮前,需先使用命令 \`emqx ctl plugins allow {'{'}NAME{'}'}-{'{'}VSN{'}'}\` 授权插件安装,命令行执行后对集群内所有节点生效。安装完毕后当次授权失效,如需再次安装,需重新授权。<br /><br />或用其他方法将插件包上传到 EMQX 所在服务器的 plugins/ 目录并解压安装包进行安装。`,
128-
en: `To ensure system security, EMQX does not allow direct upload and installation of plugins through the Dashboard. Before clicking the "Install" button, you need to use the command \`emqx ctl plugins allow {'{'}NAME{'}'}-{'{'}VSN{'}'}\` to allow plugin installation. After the command is executed, it takes effect on all nodes in the cluster. After the installation is complete, the current authorization expires. If you need to install it again, you need to authorize again. <br /><br />Or use other methods to upload the plugin package to the plugins/ directory on the EMQX server and extract the package for installation.`,
127+
zh: `为确保系统安全,EMQX 不允许直接通过 Dashboard 安装插件。点击“安装”按钮前,需先使用命令 \`emqx ctl plugins allow {'{'}NAME{'}'}-{'{'}VSN{'}'}\` 授权插件安装,命令行执行后对集群内所有节点生效。安装完成后,授权将失效,如需再次安装插件,请重新授权。<br /><br />您也可以通过其他方法将插件包上传到 EMQX 服务器的 plugins/ 目录,并解压安装包进行安装。`,
128+
en: `To ensure system security, EMQX does not allow direct installation of plugins through the Dashboard. Before clicking the "Install" button, you need to use the command \`emqx ctl plugins allow {'{'}NAME{'}'}-{'{'}VSN{'}'}\` to allow plugin installation. After the installation is complete, the current authorization expires. If you need to install it again, you need to authorize again. <br /><br />You can also upload the plugin package to the plugins/ directory on the EMQX server and extract the package for installation.`,
129129
},
130130
pluginInstallCommand: {
131131
zh: '授权插件安装命令:',
@@ -135,4 +135,8 @@ export default {
135135
zh: '请先上传插件包',
136136
en: 'Please upload the plugin package first',
137137
},
138+
pluginInstallForbidden: {
139+
zh: '插件包不允许安装; 请先运行命令: <code>{code}</code>',
140+
en: 'Package is not allowed installation; first allow it to be installed by running: <code>{code}</code>',
141+
},
138142
}

src/views/Plugins/PluginInstall.vue

+33-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
22
<div class="plugin-install app-wrapper">
33
<detail-header :item="{ name: t('components.plugin-install'), path: '/plugins' }" />
4+
<TipContainer>
5+
<MarkdownContent :content="tl('pluginInstallGuidance')" />
6+
</TipContainer>
47
<el-card class="app-card plugin-install-card">
5-
<TipContainer>
6-
<MarkdownContent :content="tl('pluginInstallGuidance')" />
7-
</TipContainer>
88
<el-upload
99
class="plugin-uploader"
1010
drag
@@ -54,6 +54,9 @@ import { ElMessage } from 'element-plus'
5454
import { Plus } from '@element-plus/icons-vue'
5555
import { installPlugin } from '@/api/plugins'
5656
import DetailHeader from '@/components/DetailHeader.vue'
57+
import CustomMessage from '@/common/CustomMessage'
58+
import { getErrorMessage } from '@/common/http'
59+
import xss from 'xss'
5760
5861
const router = useRouter()
5962
const { t } = useI18n()
@@ -82,8 +85,21 @@ const submit = async () => {
8285
await installPlugin(file.value as File)
8386
ElMessage.success(tl('successfulInstallation'))
8487
router.push({ name: 'plugins' })
85-
} catch (error) {
86-
console.error(error)
88+
} catch (error: any) {
89+
const { data, status } = error?.response ?? {}
90+
if (status === 403) {
91+
if (data?.code === 'FORBIDDEN') {
92+
const cmd = data.message.match(/`(.*?)`/)?.[1]
93+
ElMessage({
94+
dangerouslyUseHTMLString: true,
95+
message: xss(t('Plugins.pluginInstallForbidden', { code: cmd })),
96+
customClass: 'markdown-body',
97+
type: 'error',
98+
})
99+
} else {
100+
CustomMessage.error(getErrorMessage(data, status))
101+
}
102+
}
87103
} finally {
88104
isUploading.value = false
89105
}
@@ -100,14 +116,17 @@ const submit = async () => {
100116
display: flex;
101117
flex-direction: column;
102118
align-items: center;
103-
padding-top: 32px;
119+
padding-top: 48px;
104120
padding-bottom: 64px;
105121
}
106122
.tip-container {
107-
width: 970px;
108123
margin-bottom: 24px;
124+
:deep(.result-tip) {
125+
align-items: flex-start;
126+
}
109127
:deep(.icon-tip) {
110128
margin-right: 8px;
129+
margin-top: 3px;
111130
}
112131
:deep(.markdown-body) {
113132
font-size: 14px;
@@ -128,7 +147,7 @@ const submit = async () => {
128147
opacity: 0.78;
129148
}
130149
.plugin-uploader {
131-
width: 400px;
150+
width: 440px;
132151
margin-left: auto;
133152
margin-right: auto;
134153
.icon-plus {
@@ -166,3 +185,9 @@ const submit = async () => {
166185
}
167186
}
168187
</style>
188+
189+
<style lang="scss">
190+
.el-message.markdown-body {
191+
background: #ffe9e7;
192+
}
193+
</style>

0 commit comments

Comments
 (0)