fix(queries): MySQL 实例误调 Redis 接口 500 + 点表生成 SELECT + 光标多行闪烁
=== 一、/queries 打开 MySQL 实例报 500 ===
现象:GET /api/v1/redis/5/dbs → 500
后端日志 redis: invalid reply: "J\x00\x00\x00\n"
("J\x00\x00\x00" 是 MySQL 握手包,说明用 Redis 协议连了 3306)
根因:SchemaSidebar 是 mysql/redis 共用的组件,其中
const redisDbs = useRedisDbs(Number(instanceId));
是无条件调用(React hooks 不能条件调用),而 useRedisDbs 内部
只看 instanceId 就请求 /redis/{id}/dbs →
MySQL 实例也发 Redis 请求 → 后端用 Redis 协议连 MySQL → 500。
修复(前后端双保险):
1. useRedisDbs 增加 enabled 参数(默认 true),false 时不发请求并复位 [0]/false
2. SchemaSidebar 传 isRedis:useRedisDbs(Number(instanceId), isRedis)
3. redis.go getRedisClient 前置校验 DBType != "redis" 直接返回
4. resp 包新增 UserFacingError / UserFacing(),InternalErr 识别后返回
400 + 可读原因(不再把所有错误笼统包装成 500 "服务器内部错误")
验证:
GET /redis/5/dbs(MySQL)→ 400
{"code":"bad_request","detail":"实例「pre-mysql-只读」的类型是 mysql,
不是 redis。Redis 接口只适用于 redis 实例;MySQL/PostgreSQL 请使用
/api/v1/instances/5/databases 等对应接口"}
GET /instances/5/databases → 200
GET /redis/4/dbs(Redis 集群)→ 200 {"cluster":true,"dbs":[0]}
=== 二、左侧栏点表不生成 SELECT ===
原交互:单击=展开/折叠,双击=执行 SELECT,Alt+点击=插入表名。
需求:单击表 → 在右侧编辑器生成 SELECT * FROM <db>.<table>
修复:
1. SchemaSidebar 表节点:箭头图标包 data-chevron 单独触发展开/折叠;
点表名区域 → onInsert(previewSelectSql(...) + ';\n')
Alt+点击仍为只插表名;双击仍为直接执行
2. QueriesPage onInsert:生成的文本以 SELECT 开头时「替换」编辑器内容
(原来一律追加,会和上一条语句拼成非法 SQL);表名/字段名仍追加
=== 三、SQL 编辑器光标同时在多行闪烁 ===
根因:.cm-cursor 强制了 height:1.25em !important + marginTop:0.15em,
而 .cm-line 行高是 1.55em,光标高度溢出到相邻行,视觉上像两行同时闪。
修复:光标样式只保留颜色与 1.5px 宽度,height/marginTop 交回 CodeMirror 自行计算。
镜像:lghost/opsxsql:latest + v0.1
This commit is contained in:
1 parent
0427b87412
commit
36724bba94
6 files changed
+76
-16
No files matched your search
@@ -351,6 +351,18 @@ func writeRedisKeyValue(ctx context.Context, client *redisClientWrapper, key str
|
||||
}
|
||||
|
||||
func getRedisClient(instance *model.Instance, db int) (*redisClientWrapper, error) {
|
||||
// 防御:非 Redis 实例不能走 Redis 协议。
|
||||
// 若前端误调 /api/v1/redis/:id/*(如共用组件的 MySQL 实例),
|
||||
// 用 Redis 协议连 3306 会收到 MySQL 握手包,报
|
||||
// `redis: invalid reply: "J\x00\x00\x00\n"` 并抛 500。
|
||||
// 这里提前拦截,给出可读错误。
|
||||
if instance.DBType != "redis" {
|
||||
return nil, resp.UserFacing(fmt.Sprintf(
|
||||
"实例「%s」的类型是 %s,不是 redis。Redis 接口只适用于 redis 实例;"+
|
||||
"MySQL/PostgreSQL 请使用 /api/v1/instances/%d/databases 等对应接口",
|
||||
instance.Name, instance.DBType, instance.ID))
|
||||
}
|
||||
|
||||
password := instance.Password
|
||||
// Decrypt if encrypted
|
||||
if strings.HasPrefix(password, "enc:") {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
package resp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@@ -54,7 +55,25 @@ func Forbidden(c *gin.Context, detail string) { Fail(c, http.StatusForbidden,
|
||||
func NotFound(c *gin.Context, detail string) { Fail(c, http.StatusNotFound, detail) }
|
||||
|
||||
// InternalErr 500:服务端 log 完整错误,客户端只收通用文案(防 DB/内部错误原文泄漏)
|
||||
// UserFacingError 标记「可以展示给用户」的错误。
|
||||
//
|
||||
// 典型场景:调用方用错了接口/参数不满足前置条件(例如拿 MySQL 实例去调
|
||||
// /api/v1/redis/:id/*),这既不是服务端故障也不该泄漏内部细节,
|
||||
// 用 400 + 明确原因比 500 + "服务器内部错误" 更有助于定位。
|
||||
type UserFacingError struct{ Msg string }
|
||||
|
||||
func (e *UserFacingError) Error() string { return e.Msg }
|
||||
|
||||
// UserFacing 构造一个可展示给用户的错误
|
||||
func UserFacing(msg string) error { return &UserFacingError{Msg: msg} }
|
||||
|
||||
func InternalErr(c *gin.Context, err error) {
|
||||
var uf *UserFacingError
|
||||
if errors.As(err, &uf) {
|
||||
log.Printf("[resp] bad request on %s %s: %v", c.Request.Method, c.Request.URL.Path, err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"detail": uf.Msg, "code": "bad_request"})
|
||||
return
|
||||
}
|
||||
log.Printf("[resp] internal error on %s %s: %v", c.Request.Method, c.Request.URL.Path, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"detail": "服务器内部错误,请稍后重试", "code": "internal"})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user