fix: show error better for user upload (#4568)

This commit is contained in:
DacongDA
2025-11-23 21:52:44 +08:00
committed by GitHub
parent 0728a9716b
commit 9ab9c7c8e0
3 changed files with 21 additions and 17 deletions

View File

@@ -157,7 +157,7 @@ func UploadUsers(owner string, path string, userObj *User, lang string) (bool, e
}
if len(newUsers) == 0 {
return false, nil
return false, fmt.Errorf("no users are modified")
}
return AddUsersInBatch(newUsers)

View File

@@ -865,7 +865,7 @@ func StringArrayToStruct[T any](stringArray [][]string) ([]*T, error) {
instances := []*T{}
var err error
for _, m := range excelMap {
for idx, m := range excelMap {
instance := new(T)
reflectedInstance := reflect.ValueOf(instance).Elem()
@@ -892,7 +892,7 @@ func StringArrayToStruct[T any](stringArray [][]string) ([]*T, error) {
case reflect.Int:
intVal, err := strconv.Atoi(v)
if err != nil {
return nil, err
return nil, fmt.Errorf("line %d - column %s: %s", idx+1, fName, err.Error())
}
fv.SetInt(int64(intVal))
continue
@@ -920,7 +920,7 @@ func StringArrayToStruct[T any](stringArray [][]string) ([]*T, error) {
}
if err != nil {
return nil, err
return nil, fmt.Errorf("line %d: %s", idx, err.Error())
}
}
instances = append(instances, instance)

View File

@@ -197,20 +197,24 @@ class UserListPage extends BaseListPage {
reader.onload = (e) => {
const binary = e.target.result;
const workbook = XLSX.read(binary, {type: "array"});
if (!workbook.SheetNames || workbook.SheetNames.length === 0) {
Setting.showMessage("error", i18next.t("general:No sheets found in file"));
return;
try {
const workbook = XLSX.read(binary, {type: "array"});
if (!workbook.SheetNames || workbook.SheetNames.length === 0) {
Setting.showMessage("error", i18next.t("general:No sheets found in file"));
return;
}
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(worksheet);
this.setState({uploadJsonData: jsonData, file: file});
const columns = Setting.getUserColumns().map(el => {
return {title: el.split("#")[0], dataIndex: el, key: el};
});
this.setState({uploadColumns: columns}, () => {this.setState({showUploadModal: true});});
} catch (err) {
Setting.showMessage("error", `${i18next.t("general:Failed to upload")}: ${err.message}`);
}
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(worksheet);
this.setState({uploadJsonData: jsonData, file: file});
const columns = Setting.getUserColumns().map(el => {
return {title: el.split("#")[0], dataIndex: el, key: el};
});
this.setState({uploadColumns: columns}, () => {this.setState({showUploadModal: true});});
};
reader.onerror = (error) => {