forked from casdoor/casdoor
fix: show error better for user upload (#4568)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user