Unverified Commit d937f412 authored by Jesús Espino's avatar Jesús Espino Committed by GitHub

Idiomatic error handling for app/import*.go (#9473)

parent 49e04737
......@@ -42,47 +42,48 @@ func (a *App) BulkImport(fileReader io.Reader, dryRun bool, workers int) (*model
var line LineImportData
if err := decoder.Decode(&line); err != nil {
return model.NewAppError("BulkImport", "app.import.bulk_import.json_decode.error", nil, err.Error(), http.StatusBadRequest), lineNumber
} else {
if lineNumber == 1 {
importDataFileVersion, apperr := processImportDataFileVersionLine(line)
if apperr != nil {
return apperr, lineNumber
}
}
if importDataFileVersion != 1 {
return model.NewAppError("BulkImport", "app.import.bulk_import.unsupported_version.error", nil, "", http.StatusBadRequest), lineNumber
}
} else {
if line.Type != lastLineType {
if lastLineType != "" {
// Changing type. Clear out the worker queue before continuing.
close(linesChan)
wg.Wait()
// Check no errors occurred while waiting for the queue to empty.
if len(errorsChan) != 0 {
err := <-errorsChan
return err.Error, err.LineNumber
}
}
// Set up the workers and channel for this type.
lastLineType = line.Type
linesChan = make(chan LineImportWorkerData, workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go a.bulkImportWorker(dryRun, &wg, linesChan, errorsChan)
}
}
if lineNumber == 1 {
importDataFileVersion, apperr := processImportDataFileVersionLine(line)
if apperr != nil {
return apperr, lineNumber
}
select {
case linesChan <- LineImportWorkerData{line, lineNumber}:
case err := <-errorsChan:
close(linesChan)
wg.Wait()
if importDataFileVersion != 1 {
return model.NewAppError("BulkImport", "app.import.bulk_import.unsupported_version.error", nil, "", http.StatusBadRequest), lineNumber
}
continue
}
if line.Type != lastLineType {
if lastLineType != "" {
// Changing type. Clear out the worker queue before continuing.
close(linesChan)
wg.Wait()
// Check no errors occurred while waiting for the queue to empty.
if len(errorsChan) != 0 {
err := <-errorsChan
return err.Error, err.LineNumber
}
}
// Set up the workers and channel for this type.
lastLineType = line.Type
linesChan = make(chan LineImportWorkerData, workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go a.bulkImportWorker(dryRun, &wg, linesChan, errorsChan)
}
}
select {
case linesChan <- LineImportWorkerData{line, lineNumber}:
case err := <-errorsChan:
close(linesChan)
wg.Wait()
return err.Error, err.LineNumber
}
}
......@@ -120,51 +121,43 @@ func (a *App) ImportLine(line LineImportData, dryRun bool) *model.AppError {
case line.Type == "scheme":
if line.Scheme == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_scheme.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportScheme(line.Scheme, dryRun)
}
return a.ImportScheme(line.Scheme, dryRun)
case line.Type == "team":
if line.Team == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_team.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportTeam(line.Team, dryRun)
}
return a.ImportTeam(line.Team, dryRun)
case line.Type == "channel":
if line.Channel == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_channel.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportChannel(line.Channel, dryRun)
}
return a.ImportChannel(line.Channel, dryRun)
case line.Type == "user":
if line.User == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_user.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportUser(line.User, dryRun)
}
return a.ImportUser(line.User, dryRun)
case line.Type == "post":
if line.Post == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_post.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportPost(line.Post, dryRun)
}
return a.ImportPost(line.Post, dryRun)
case line.Type == "direct_channel":
if line.DirectChannel == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_direct_channel.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportDirectChannel(line.DirectChannel, dryRun)
}
return a.ImportDirectChannel(line.DirectChannel, dryRun)
case line.Type == "direct_post":
if line.DirectPost == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_direct_post.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportDirectPost(line.DirectPost, dryRun)
}
return a.ImportDirectPost(line.DirectPost, dryRun)
case line.Type == "emoji":
if line.Emoji == nil {
return model.NewAppError("BulkImport", "app.import.import_line.null_emoji.error", nil, "", http.StatusBadRequest)
} else {
return a.ImportEmoji(line.Emoji, dryRun)
}
return a.ImportEmoji(line.Emoji, dryRun)
default:
return model.NewAppError("BulkImport", "app.import.import_line.unknown_line_type.error", map[string]interface{}{"Type": line.Type}, "", http.StatusBadRequest)
}
......
This diff is collapsed.
......@@ -283,9 +283,9 @@ func validateUserImportData(data *UserImportData) *model.AppError {
if data.Teams != nil {
return validateUserTeamsImportData(data.Teams)
} else {
return nil
}
return nil
}
func validateUserTeamsImportData(data *[]UserTeamImportData) *model.AppError {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment