Remove usages of AppError on filesstore service (#15841)

* Remove usages of AppError on filesstore service

* Fixing a golint error

* Fixing shadowed variable

* Adding err.Error() to the NewAppError calls

* Fixing tests

* Adding missed translations

* Fix error handling and updating the translation that affects it

* Fixing two typos
Этот коммит содержится в:
Jesús Espino
2020-12-20 12:53:07 +01:00
коммит произвёл GitHub
родитель 10be00f005
Коммит 7419898449
13 изменённых файлов: 366 добавлений и 289 удалений

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

@@ -393,6 +393,7 @@ type AppIface interface {
ChannelMembersToRemove(teamID *string) ([]*model.ChannelMember, *model.AppError)
CheckAndSendUserLimitWarningEmails() *model.AppError
CheckForClientSideCert(r *http.Request) (string, string, string)
CheckMandatoryS3Fields(settings *model.FileSettings) *model.AppError
CheckPasswordAndAllCriteria(user *model.User, password string, mfaToken string) *model.AppError
CheckRolesExist(roleNames []string) *model.AppError
CheckUserAllAuthenticationCriteria(user *model.User, mfaToken string) *model.AppError
@@ -834,6 +835,7 @@ type AppIface interface {
ReloadConfig() error
RemoveAllDeactivatedMembersFromChannel(channel *model.Channel) *model.AppError
RemoveConfigListener(id string)
RemoveDirectory(path string) *model.AppError
RemoveFile(path string) *model.AppError
RemoveLdapPrivateCertificate() *model.AppError
RemoveLdapPublicCertificate() *model.AppError
@@ -957,6 +959,8 @@ type AppIface interface {
TelemetryId() string
TestElasticsearch(cfg *model.Config) *model.AppError
TestEmail(userId string, cfg *model.Config) *model.AppError
TestFilesStoreConnection() *model.AppError
TestFilesStoreConnectionWithConfig(cfg *model.FileSettings) *model.AppError
TestLdap() *model.AppError
TestSiteURL(siteURL string) *model.AppError
Timezones() *timezones.Timezones

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

@@ -76,12 +76,49 @@ func (a *App) FileBackend() (filesstore.FileBackend, *model.AppError) {
return a.Srv().FileBackend()
}
func (a *App) CheckMandatoryS3Fields(settings *model.FileSettings) *model.AppError {
err := filesstore.CheckMandatoryS3Fields(settings)
if err != nil {
return model.NewAppError("CheckMandatoryS3Fields", "api.admin.test_s3.missing_s3_bucket", nil, err.Error(), http.StatusBadRequest)
}
return nil
}
func (a *App) TestFilesStoreConnection() *model.AppError {
backend, err := a.FileBackend()
if err != nil {
return err
}
nErr := backend.TestConnection()
if nErr != nil {
return model.NewAppError("TestConnection", "api.file.test_connection.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) TestFilesStoreConnectionWithConfig(cfg *model.FileSettings) *model.AppError {
license := a.Srv().License()
backend, err := filesstore.NewFileBackend(cfg, license != nil && *license.Features.Compliance)
if err != nil {
return model.NewAppError("FileBackend", "api.file.no_driver.app_error", nil, err.Error(), http.StatusInternalServerError)
}
nErr := backend.TestConnection()
if nErr != nil {
return model.NewAppError("TestConnection", "api.file.test_connection.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) ReadFile(path string) ([]byte, *model.AppError) {
backend, err := a.FileBackend()
if err != nil {
return nil, err
}
return backend.ReadFile(path)
result, nErr := backend.ReadFile(path)
if nErr != nil {
return nil, model.NewAppError("ReadFile", "api.file.read_file.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return result, nil
}
// Caller must close the first return value
@@ -90,7 +127,11 @@ func (a *App) FileReader(path string) (filesstore.ReadCloseSeeker, *model.AppErr
if err != nil {
return nil, err
}
return backend.Reader(path)
result, nErr := backend.Reader(path)
if nErr != nil {
return nil, model.NewAppError("FileReader", "api.file.file_reader.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return result, nil
}
func (a *App) FileExists(path string) (bool, *model.AppError) {
@@ -98,7 +139,11 @@ func (a *App) FileExists(path string) (bool, *model.AppError) {
if err != nil {
return false, err
}
return backend.FileExists(path)
result, nErr := backend.FileExists(path)
if nErr != nil {
return false, model.NewAppError("FileExists", "api.file.file_exists.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return result, nil
}
func (a *App) FileSize(path string) (int64, *model.AppError) {
@@ -106,7 +151,11 @@ func (a *App) FileSize(path string) (int64, *model.AppError) {
if err != nil {
return 0, err
}
return backend.FileSize(path)
size, nErr := backend.FileSize(path)
if nErr != nil {
return 0, model.NewAppError("FileSize", "api.file.file_size.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return size, nil
}
func (a *App) MoveFile(oldPath, newPath string) *model.AppError {
@@ -114,7 +163,11 @@ func (a *App) MoveFile(oldPath, newPath string) *model.AppError {
if err != nil {
return err
}
return backend.MoveFile(oldPath, newPath)
nErr := backend.MoveFile(oldPath, newPath)
if nErr != nil {
return model.NewAppError("MoveFile", "api.file.move_file.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) WriteFile(fr io.Reader, path string) (int64, *model.AppError) {
@@ -123,7 +176,11 @@ func (a *App) WriteFile(fr io.Reader, path string) (int64, *model.AppError) {
return 0, err
}
return backend.WriteFile(fr, path)
result, nErr := backend.WriteFile(fr, path)
if nErr != nil {
return result, model.NewAppError("WriteFile", "api.file.write_file.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return result, nil
}
func (a *App) AppendFile(fr io.Reader, path string) (int64, *model.AppError) {
@@ -132,7 +189,11 @@ func (a *App) AppendFile(fr io.Reader, path string) (int64, *model.AppError) {
return 0, err
}
return backend.AppendFile(fr, path)
result, nErr := backend.AppendFile(fr, path)
if nErr != nil {
return result, model.NewAppError("AppendFile", "api.file.append_file.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return result, nil
}
func (a *App) RemoveFile(path string) *model.AppError {
@@ -140,7 +201,11 @@ func (a *App) RemoveFile(path string) *model.AppError {
if err != nil {
return err
}
return backend.RemoveFile(path)
nErr := backend.RemoveFile(path)
if nErr != nil {
return model.NewAppError("RemoveFile", "api.file.remove_file.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) ListDirectory(path string) ([]string, *model.AppError) {
@@ -148,14 +213,27 @@ func (a *App) ListDirectory(path string) ([]string, *model.AppError) {
if err != nil {
return nil, err
}
paths, err := backend.ListDirectory(path)
if err != nil {
return nil, err
paths, nErr := backend.ListDirectory(path)
if nErr != nil {
return nil, model.NewAppError("ListDirectory", "api.file.list_directory.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return *paths, nil
}
func (a *App) RemoveDirectory(path string) *model.AppError {
backend, err := a.FileBackend()
if err != nil {
return err
}
nErr := backend.RemoveDirectory(path)
if nErr != nil {
return model.NewAppError("RemoveDirectory", "api.file.remove_directory.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) getInfoForFilename(post *model.Post, teamId, channelId, userId, oldId, filename string) *model.FileInfo {
name, _ := url.QueryUnescape(filename)
pathPrefix := fmt.Sprintf("teams/%s/channels/%s/users/%s/%s/", teamId, channelId, userId, oldId)

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

@@ -1071,6 +1071,28 @@ func (a *OpenTracingAppLayer) CheckForClientSideCert(r *http.Request) (string, s
return resultVar0, resultVar1, resultVar2
}
func (a *OpenTracingAppLayer) CheckMandatoryS3Fields(settings *model.FileSettings) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.CheckMandatoryS3Fields")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.CheckMandatoryS3Fields(settings)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) CheckPasswordAndAllCriteria(user *model.User, password string, mfaToken string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.CheckPasswordAndAllCriteria")
@@ -11709,6 +11731,28 @@ func (a *OpenTracingAppLayer) RemoveConfigListener(id string) {
a.app.RemoveConfigListener(id)
}
func (a *OpenTracingAppLayer) RemoveDirectory(path string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RemoveDirectory")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.RemoveDirectory(path)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) RemoveFile(path string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RemoveFile")
@@ -14300,6 +14344,50 @@ func (a *OpenTracingAppLayer) TestEmail(userId string, cfg *model.Config) *model
return resultVar0
}
func (a *OpenTracingAppLayer) TestFilesStoreConnection() *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.TestFilesStoreConnection")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.TestFilesStoreConnection()
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) TestFilesStoreConnectionWithConfig(cfg *model.FileSettings) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.TestFilesStoreConnectionWithConfig")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.TestFilesStoreConnectionWithConfig(cfg)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) TestLdap() *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.TestLdap")

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

@@ -465,11 +465,13 @@ func NewServer(options ...Option) (*Server, error) {
}
backend, appErr := s.FileBackend()
if appErr == nil {
appErr = backend.TestConnection()
}
if appErr != nil {
mlog.Error("Problem with file storage settings", mlog.Err(appErr))
} else {
nErr := backend.TestConnection()
if nErr != nil {
mlog.Error("Problem with file storage settings", mlog.Err(nErr))
}
}
s.timezones = timezones.New()
@@ -1531,7 +1533,11 @@ func (s *Server) stopSearchEngine() {
func (s *Server) FileBackend() (filesstore.FileBackend, *model.AppError) {
license := s.License()
return filesstore.NewFileBackend(&s.Config().FileSettings, license != nil && *license.Features.Compliance)
backend, err := filesstore.NewFileBackend(&s.Config().FileSettings, license != nil && *license.Features.Compliance)
if err != nil {
return nil, model.NewAppError("FileBackend", "api.file.no_driver.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return backend, nil
}
func (s *Server) TotalWebsocketConnections() int {