flipped-aurora/gin-vue-admin · error

设置数据权限失败: %w

Error message

设置数据权限失败: %w

What it means

This wraps any failure of the final POST to `/authority/setDataScope`, which persists the role's data-scope tier and custom department set. All validations and pre-reads have already succeeded at this point, so the failure is in the write step. The wrapped error carries the upstream cause.

Source

Thrown at server/mcp/role_data_scope_setter.go:135

	query := url.Values{}
	query.Set("authorityId", strconv.FormatUint(uint64(authorityID), 10))
	beforeDeptsResp, err := getUpstream[[]uint](ctx, "/authority/getDataScopeDepts", query)
	if err != nil {
		return nil, fmt.Errorf("读取角色当前自定义部门集失败: %w", err)
	}

	before := dataScopeState{
		DataScope: authority.DataScope,
		Label:     dataScopeLabels[authority.DataScope],
		DeptIDs:   beforeDeptsResp.Data,
	}

	if _, err := postUpstream[map[string]any](ctx, "/authority/setDataScope", map[string]any{
		"authorityId": authorityID,
		"dataScope":   scope,
		"deptIds":     deptIDs,
	}); err != nil {
		return nil, fmt.Errorf("设置数据权限失败: %w", err)
	}

	after := dataScopeState{
		DataScope: scope,
		Label:     dataScopeLabels[scope],
		DeptIDs:   deptIDs,
	}

	return textResultWithJSON("角色数据权限设置结果:", roleDataScopeResponse{
		Success:       true,
		Message:       fmt.Sprintf("角色 %s(ID:%d) 数据权限已设置为「%s」", authority.AuthorityName, authorityID, after.Label),
		AuthorityID:   authorityID,
		AuthorityName: authority.AuthorityName,
		Before:        before,
		After:         after,
		Changed:       before.DataScope != after.DataScope || !sameUintSet(before.DeptIDs, after.DeptIDs),
	})
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Read the wrapped cause to distinguish auth (401), route-missing (404), or validation (400) failures
  2. Refresh the auth token and retry
  3. Confirm the gva server version supports /authority/setDataScope
  4. Verify deptIds still exist server-side before retrying
  5. Check server logs for the corresponding failed request

Example fix

// before
if _, err := postUpstream[map[string]any](ctx, "/authority/setDataScope", payload); err != nil {
    return nil, fmt.Errorf("设置数据权限失败: %w", err)
}
// after
if _, err := postUpstream[map[string]any](ctx, "/authority/setDataScope", payload); err != nil {
    log.Printf("setDataScope failed (authorityId=%d, scope=%d): %v", authorityID, scope, err)
    return nil, fmt.Errorf("设置数据权限失败: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// re-validate preconditions right before the write
if scope == 5 && len(deptIds) == 0 {
    return fmt.Errorf("refusing to write empty custom dept set")
}

Try / catch

_, err := setDataScope(ctx, authorityID, scope, deptIds)
if err != nil {
    switch {
    case isAuthError(err):
        refreshToken(); retry()
    case isNotFound(err):
        return fmt.Errorf("server lacks setDataScope; upgrade gva")
    default:
        return err
    }
}

Prevention

When it happens

Trigger: POST /authority/setDataScope returns an error: auth failure, server-side validation rejection (e.g. one deptId rejected server-side), 404 on older servers lacking the route, or network interruption.

Common situations: Casbin/authority table migration missing on the server; concurrent admin modifications causing server-side conflict; token expired between the pre-read GET and the POST.

Related errors


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