flipped-aurora/gin-vue-admin · error

设置用户部门失败: %w

Error message

设置用户部门失败: %w

What it means

Wrap of an upstream HTTP error from POST /user/setUserDepartments when updating a member's departments. The assigner's own logic succeeded; the actual write against the admin backend API failed and the underlying error is chained via %w.

Source

Thrown at server/mcp/org_member_assigner.go:164

		primary := user.DeptId
		if value, ok := args["primaryDeptId"]; ok && value != nil {
			explicit, err := parseUintParam(value, "primaryDeptId")
			if err != nil {
				return nil, err
			}
			primary = explicit
		}
		if primary != 0 && !containsUint(merged, primary) {
			return nil, fmt.Errorf("primaryDeptId %d 必须属于合并后的部门集合 %v", primary, merged)
		}

		if len(added) > 0 || (primary != user.DeptId && primary != 0) {
			if _, err := postUpstream[map[string]any](ctx, "/user/setUserDepartments", map[string]any{
				"ID":            user.ID,
				"deptIds":       merged,
				"primaryDeptId": primary,
			}); err != nil {
				return nil, fmt.Errorf("设置用户部门失败: %w", err)
			}
			messages = append(messages, fmt.Sprintf("部门新增 %v", added))
		} else {
			messages = append(messages, "部门均已存在,未写入")
		}
		result.Departments = &orgAssignChange{Before: currentDeptIDs, After: merged, Added: added}
		// 上游对 primaryDeptId=0 取部门集合首个;如实回报生效主部门,不因 0 值被 omitempty 隐去而误导
		result.PrimaryDeptID = primary
		if primary == 0 && len(merged) > 0 {
			result.PrimaryDeptID = merged[0]
		}
	}

	if len(positionIDs) > 0 {
		merged, added := mergeUintSets(currentPositionIDs, positionIDs)
		if len(added) > 0 {
			if _, err := postUpstream[map[string]any](ctx, "/user/setUserPositions", map[string]any{
				"ID":          user.ID,

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Read the wrapped cause (%w) to see the upstream status/message and fix accordingly
  2. Confirm the admin backend API is reachable and healthy from the MCP server
  3. Check the MCP credentials have rights to call /user/setUserDepartments for this tenant
  4. Retry after confirming the target user and dept IDs still exist
Defensive patterns

Strategy: try-catch

Validate before calling

if user == nil || len(deptIDs) == 0 && primaryDeptId == 0 {
    return errors.New("nothing to assign: provide deptIds or primaryDeptId")
}

Try / catch

res, err := assigner.Handle(ctx, args)
if err != nil {
    var upstreamErr *url.Error
    if errors.As(err, &upstreamErr) {
        // network-level failure: check backend health, retry with backoff
    } else {
        // upstream returned an error status: inspect message for status/body
    }
    return fmt.Errorf("set departments failed: %w", err)
}

Prevention

When it happens

Trigger: Handle calls postUpstream to /user/setUserDepartments and the upstream returns a non-2xx status, auth failure, validation rejection (e.g. invalid deptIds/primaryDeptId combination), or the server is unreachable.

Common situations: Backend service down or restarted; MCP token lacks casbin permission for the user API; concurrent modification where the user was deleted mid-flow; department IDs referencing soft-deleted depts.

Related errors


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