flipped-aurora/gin-vue-admin · error

获取角色当前API权限失败: %w

Error message

获取角色当前API权限失败: %w

What it means

Wrap of an upstream error when fetching the role's current Casbin API policies via POST /casbin/getPolicyPathByAuthorityId. The assigner needs the current policy list to compute the diff before updating; failing this read aborts the assignment.

Source

Thrown at server/mcp/role_api_assigner.go:79

	if err != nil {
		return nil, err
	}

	path, ok := args["path"].(string)
	if !ok || strings.TrimSpace(path) == "" {
		return nil, errors.New("path 参数是必需的")
	}
	method := "POST"
	if value, ok := args["method"].(string); ok && strings.TrimSpace(value) != "" {
		method = value
	}

	path, method = normalizePolicy(path, method)
	currentResp, err := postUpstream[map[string][]systemReq.CasbinInfo](ctx, "/casbin/getPolicyPathByAuthorityId", map[string]any{
		"authorityId": authorityID,
	})
	if err != nil {
		return nil, fmt.Errorf("获取角色当前API权限失败: %w", err)
	}

	current := currentResp.Data["paths"]
	updated, added := appendPolicyIfMissing(current, path, method)
	if added {
		if _, err = postUpstream[map[string]any](ctx, "/casbin/updateCasbin", map[string]any{
			"authorityId": authorityID,
			"casbinInfos": updated,
		}); err != nil {
			return nil, fmt.Errorf("分配API权限失败: %w", err)
		}
	}

	msg := "权限已存在,无需重复分配"
	if added {
		msg = fmt.Sprintf("成功为角色 %d 分配权限 %s %s", authorityID, method, path)
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Verify the authorityId corresponds to an existing role (query roles first)
  2. Check the wrapped cause for the upstream status/body
  3. Confirm MCP credentials can access the casbin endpoints
  4. Retry when the backend is confirmed healthy
Defensive patterns

Strategy: validation

Validate before calling

roles, err := client.ListRoles(ctx)
if err != nil { return err }
exists := false
for _, r := range roles {
    if uint(r.ID) == authorityID { exists = true; break }
}
if !exists {
    return fmt.Errorf("authorityId %d does not exist", authorityID)
}

Try / catch

_, err := assigner.Handle(ctx, args)
if err != nil {
    if strings.Contains(err.Error(), "获取角色当前API权限失败") {
        // read-phase failure: check role exists, token perms, backend health
        // retry only on network/5xx causes
    }
    return err
}

Prevention

When it happens

Trigger: Handle calls getPolicyPathByAuthorityId for the given authorityId and postUpstream fails — network error, non-2xx response, unknown/nonexistent authorityId rejected by the backend, or decode failure into map[string][]systemReq.CasbinInfo.

Common situations: Role (authorityId) does not exist or was deleted; MCP token lacks casbin read permission; backend temporarily down; response schema changed so Data["paths"] no longer decodes.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/86b583ab84ced302. Report an issue: GitHub.