MM-45198: Migrate app/import and api4/import (#20799)

We migrate them to use logger context.

https://mattermost.atlassian.net/browse/MM-45198

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2022-08-17 19:58:52 +05:30
коммит произвёл GitHub
родитель 5e00295b5a
Коммит d9d75b1a7d
4 изменённых файлов: 17 добавлений и 21 удалений

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"github.com/mattermost/mattermost-server/v6/model" "github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
) )
func (api *API) InitImport() { func (api *API) InitImport() {
@@ -26,11 +27,7 @@ func listImports(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
data, err := json.Marshal(imports) if err := json.NewEncoder(w).Encode(imports); err != nil {
if err != nil { c.Logger.Warn("Error writing imports", mlog.Err(err))
c.Err = model.NewAppError("listImports", "app.import.marshal.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
return
} }
w.Write(data)
} }

Просмотреть файл

@@ -25,13 +25,13 @@ const (
maxScanTokenSize = 16 * 1024 * 1024 // Need to set a higher limit than default because some customers cross the limit. See MM-22314 maxScanTokenSize = 16 * 1024 * 1024 // Need to set a higher limit than default because some customers cross the limit. See MM-22314
) )
func stopOnError(err LineImportWorkerError) bool { func stopOnError(c request.CTX, err LineImportWorkerError) bool {
switch err.Error.Id { switch err.Error.Id {
case "api.file.upload_file.large_image.app_error": case "api.file.upload_file.large_image.app_error":
mlog.Warn("Large image import error", mlog.Err(err.Error)) c.Logger().Warn("Large image import error", mlog.Err(err.Error))
return false return false
case "app.import.validate_direct_channel_import_data.members_too_few.error", "app.import.validate_direct_channel_import_data.members_too_many.error": case "app.import.validate_direct_channel_import_data.members_too_few.error", "app.import.validate_direct_channel_import_data.members_too_many.error":
mlog.Warn("Invalid direct channel import data", mlog.Err(err.Error)) c.Logger().Warn("Invalid direct channel import data", mlog.Err(err.Error))
return false return false
default: default:
return true return true
@@ -226,7 +226,7 @@ func (a *App) bulkImport(c request.CTX, jsonlReader io.Reader, attachmentsReader
// Check no errors occurred while waiting for the queue to empty. // Check no errors occurred while waiting for the queue to empty.
if len(errorsChan) != 0 { if len(errorsChan) != 0 {
err := <-errorsChan err := <-errorsChan
if stopOnError(err) { if stopOnError(c, err) {
return err.Error, err.LineNumber return err.Error, err.LineNumber
} }
} }
@@ -244,7 +244,7 @@ func (a *App) bulkImport(c request.CTX, jsonlReader io.Reader, attachmentsReader
select { select {
case linesChan <- LineImportWorkerData{line, lineNumber}: case linesChan <- LineImportWorkerData{line, lineNumber}:
case err := <-errorsChan: case err := <-errorsChan:
if stopOnError(err) { if stopOnError(c, err) {
close(linesChan) close(linesChan)
wg.Wait() wg.Wait()
return err.Error, err.LineNumber return err.Error, err.LineNumber
@@ -261,7 +261,7 @@ func (a *App) bulkImport(c request.CTX, jsonlReader io.Reader, attachmentsReader
// Check no errors occurred while waiting for the queue to empty. // Check no errors occurred while waiting for the queue to empty.
if len(errorsChan) != 0 { if len(errorsChan) != 0 {
err := <-errorsChan err := <-errorsChan
if stopOnError(err) { if stopOnError(c, err) {
return err.Error, err.LineNumber return err.Error, err.LineNumber
} }
} }

Просмотреть файл

@@ -127,27 +127,30 @@ func TestImportImportLine(t *testing.T) {
} }
func TestStopOnError(t *testing.T) { func TestStopOnError(t *testing.T) {
assert.True(t, stopOnError(LineImportWorkerError{ th := Setup(t)
defer th.TearDown()
assert.True(t, stopOnError(th.Context, LineImportWorkerError{
model.NewAppError("test", "app.import.attachment.bad_file.error", nil, "", http.StatusBadRequest), model.NewAppError("test", "app.import.attachment.bad_file.error", nil, "", http.StatusBadRequest),
1, 1,
})) }))
assert.True(t, stopOnError(LineImportWorkerError{ assert.True(t, stopOnError(th.Context, LineImportWorkerError{
model.NewAppError("test", "app.import.attachment.file_upload.error", nil, "", http.StatusBadRequest), model.NewAppError("test", "app.import.attachment.file_upload.error", nil, "", http.StatusBadRequest),
1, 1,
})) }))
assert.False(t, stopOnError(LineImportWorkerError{ assert.False(t, stopOnError(th.Context, LineImportWorkerError{
model.NewAppError("test", "api.file.upload_file.large_image.app_error", nil, "", http.StatusBadRequest), model.NewAppError("test", "api.file.upload_file.large_image.app_error", nil, "", http.StatusBadRequest),
1, 1,
})) }))
assert.False(t, stopOnError(LineImportWorkerError{ assert.False(t, stopOnError(th.Context, LineImportWorkerError{
model.NewAppError("test", "app.import.validate_direct_channel_import_data.members_too_few.error", nil, "", http.StatusBadRequest), model.NewAppError("test", "app.import.validate_direct_channel_import_data.members_too_few.error", nil, "", http.StatusBadRequest),
1, 1,
})) }))
assert.False(t, stopOnError(LineImportWorkerError{ assert.False(t, stopOnError(th.Context, LineImportWorkerError{
model.NewAppError("test", "app.import.validate_direct_channel_import_data.members_too_many.error", nil, "", http.StatusBadRequest), model.NewAppError("test", "app.import.validate_direct_channel_import_data.members_too_many.error", nil, "", http.StatusBadRequest),
1, 1,
})) }))

Просмотреть файл

@@ -5015,10 +5015,6 @@
"id": "app.import.import_user_teams.save_preferences.error", "id": "app.import.import_user_teams.save_preferences.error",
"translation": "Unable to save the team theme preferences" "translation": "Unable to save the team theme preferences"
}, },
{
"id": "app.import.marshal.app_error",
"translation": "Unable to marshal response."
},
{ {
"id": "app.import.process_import_data_file_version_line.invalid_version.error", "id": "app.import.process_import_data_file_version_line.invalid_version.error",
"translation": "Unable to read the version of the data import file." "translation": "Unable to read the version of the data import file."