{"record":{"id":"e72362260024291d","repo":"flipped-aurora/gin-vue-admin","slug":"w-e72362","errorCode":null,"errorMessage":"设置数据权限失败: %w","messagePattern":"设置数据权限失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/mcp/role_data_scope_setter.go","lineNumber":135,"sourceCode":"\tquery := url.Values{}\n\tquery.Set(\"authorityId\", strconv.FormatUint(uint64(authorityID), 10))\n\tbeforeDeptsResp, err := getUpstream[[]uint](ctx, \"/authority/getDataScopeDepts\", query)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"读取角色当前自定义部门集失败: %w\", err)\n\t}\n\n\tbefore := dataScopeState{\n\t\tDataScope: authority.DataScope,\n\t\tLabel:     dataScopeLabels[authority.DataScope],\n\t\tDeptIDs:   beforeDeptsResp.Data,\n\t}\n\n\tif _, err := postUpstream[map[string]any](ctx, \"/authority/setDataScope\", map[string]any{\n\t\t\"authorityId\": authorityID,\n\t\t\"dataScope\":   scope,\n\t\t\"deptIds\":     deptIDs,\n\t}); err != nil {\n\t\treturn nil, fmt.Errorf(\"设置数据权限失败: %w\", err)\n\t}\n\n\tafter := dataScopeState{\n\t\tDataScope: scope,\n\t\tLabel:     dataScopeLabels[scope],\n\t\tDeptIDs:   deptIDs,\n\t}\n\n\treturn textResultWithJSON(\"角色数据权限设置结果：\", roleDataScopeResponse{\n\t\tSuccess:       true,\n\t\tMessage:       fmt.Sprintf(\"角色 %s(ID:%d) 数据权限已设置为「%s」\", authority.AuthorityName, authorityID, after.Label),\n\t\tAuthorityID:   authorityID,\n\t\tAuthorityName: authority.AuthorityName,\n\t\tBefore:        before,\n\t\tAfter:         after,\n\t\tChanged:       before.DataScope != after.DataScope || !sameUintSet(before.DeptIDs, after.DeptIDs),\n\t})\n}","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/mcp/role_data_scope_setter.go#L117-L153","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped cause to distinguish auth (401), route-missing (404), or validation (400) failures","Refresh the auth token and retry","Confirm the gva server version supports /authority/setDataScope","Verify deptIds still exist server-side before retrying","Check server logs for the corresponding failed request"],"exampleFix":"// before\nif _, err := postUpstream[map[string]any](ctx, \"/authority/setDataScope\", payload); err != nil {\n    return nil, fmt.Errorf(\"设置数据权限失败: %w\", err)\n}\n// after\nif _, err := postUpstream[map[string]any](ctx, \"/authority/setDataScope\", payload); err != nil {\n    log.Printf(\"setDataScope failed (authorityId=%d, scope=%d): %v\", authorityID, scope, err)\n    return nil, fmt.Errorf(\"设置数据权限失败: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"// re-validate preconditions right before the write\nif scope == 5 && len(deptIds) == 0 {\n    return fmt.Errorf(\"refusing to write empty custom dept set\")\n}","typeGuard":null,"tryCatchPattern":"_, err := setDataScope(ctx, authorityID, scope, deptIds)\nif err != nil {\n    switch {\n    case isAuthError(err):\n        refreshToken(); retry()\n    case isNotFound(err):\n        return fmt.Errorf(\"server lacks setDataScope; upgrade gva\")\n    default:\n        return err\n    }\n}","preventionTips":["Verify server version supports /authority/setDataScope","Re-check deptIds validity just before writing","Keep auth token fresh across the read-then-write sequence","Check server logs when writes fail"],"tags":["mcp","upstream-api","data-scope"],"backgroundTag":"upstream-request-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}