LicenseStore migration to return plain errors (#14837)

Automatic Merge
Этот коммит содержится в:
Rodrigo Villablanca
2020-06-23 23:56:35 -04:00
коммит произвёл GitHub
родитель 668a2aa856
Коммит 64d12c08e9
7 изменённых файлов: 33 добавлений и 42 удалений

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

@@ -5,6 +5,7 @@ package app
import ( import (
"bytes" "bytes"
"errors"
"net/http" "net/http"
"strings" "strings"
@@ -35,8 +36,8 @@ func (s *Server) LoadLicense() {
} }
} }
record, err := s.Store.License().Get(licenseId) record, nErr := s.Store.License().Get(licenseId)
if err != nil { if nErr != nil {
mlog.Info("License key from https://mattermost.com required to unlock enterprise features.") mlog.Info("License key from https://mattermost.com required to unlock enterprise features.")
s.SetLicense(nil) s.SetLicense(nil)
return return
@@ -74,10 +75,16 @@ func (s *Server) SaveLicense(licenseBytes []byte) (*model.License, *model.AppErr
record.Id = license.Id record.Id = license.Id
record.Bytes = string(licenseBytes) record.Bytes = string(licenseBytes)
_, err = s.Store.License().Save(record) _, nErr := s.Store.License().Save(record)
if err != nil { if nErr != nil {
s.RemoveLicense() s.RemoveLicense()
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) var appErr *model.AppError
switch {
case errors.As(nErr, &appErr):
return nil, appErr
default:
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, err.Error(), http.StatusInternalServerError)
}
} }
sysVar := &model.System{} sysVar := &model.System{}

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

@@ -6590,18 +6590,6 @@
"id": "store.sql_job.update.app_error", "id": "store.sql_job.update.app_error",
"translation": "Unable to update the job." "translation": "Unable to update the job."
}, },
{
"id": "store.sql_license.get.app_error",
"translation": "We encountered an error getting the license."
},
{
"id": "store.sql_license.get.missing.app_error",
"translation": "A license with that ID was not found."
},
{
"id": "store.sql_license.save.app_error",
"translation": "We encountered an error saving the license."
},
{ {
"id": "store.sql_oauth.delete.commit_transaction.app_error", "id": "store.sql_oauth.delete.commit_transaction.app_error",
"translation": "Unable to commit transaction." "translation": "Unable to commit transaction."

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

@@ -3864,7 +3864,7 @@ func (s *OpenTracingLayerJobStore) UpdateStatusOptimistically(id string, current
return resultVar0, resultVar1 return resultVar0, resultVar1
} }
func (s *OpenTracingLayerLicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) { func (s *OpenTracingLayerLicenseStore) Get(id string) (*model.LicenseRecord, error) {
origCtx := s.Root.Store.Context() origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "LicenseStore.Get") span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "LicenseStore.Get")
s.Root.Store.SetContext(newCtx) s.Root.Store.SetContext(newCtx)
@@ -3882,7 +3882,7 @@ func (s *OpenTracingLayerLicenseStore) Get(id string) (*model.LicenseRecord, *mo
return resultVar0, resultVar1 return resultVar0, resultVar1
} }
func (s *OpenTracingLayerLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) { func (s *OpenTracingLayerLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, error) {
origCtx := s.Root.Store.Context() origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "LicenseStore.Save") span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "LicenseStore.Save")
s.Root.Store.SetContext(newCtx) s.Root.Store.SetContext(newCtx)

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

@@ -4,7 +4,7 @@
package sqlstore package sqlstore
import ( import (
"net/http" "github.com/pkg/errors"
sq "github.com/Masterminds/squirrel" sq "github.com/Masterminds/squirrel"
@@ -39,7 +39,7 @@ func (ls SqlLicenseStore) createIndexesIfNotExists() {
// database it returns the license stored in the database. If not, it saves the // database it returns the license stored in the database. If not, it saves the
// new database and returns the created license with the CreateAt field // new database and returns the created license with the CreateAt field
// updated. // updated.
func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) { func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, error) {
license.PreSave() license.PreSave()
if err := license.IsValid(); err != nil { if err := license.IsValid(); err != nil {
return nil, err return nil, err
@@ -50,13 +50,13 @@ func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseReco
Where(sq.Eq{"Id": license.Id}) Where(sq.Eq{"Id": license.Id})
queryString, args, err := query.ToSql() queryString, args, err := query.ToSql()
if err != nil { if err != nil {
return nil, model.NewAppError("SqlLicenseStore.Save", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, errors.Wrap(err, "license_tosql")
} }
var storedLicense model.LicenseRecord var storedLicense model.LicenseRecord
if err := ls.GetReplica().SelectOne(&storedLicense, queryString, args...); err != nil { if err := ls.GetReplica().SelectOne(&storedLicense, queryString, args...); err != nil {
// Only insert if not exists // Only insert if not exists
if err := ls.GetMaster().Insert(license); err != nil { if err := ls.GetMaster().Insert(license); err != nil {
return nil, model.NewAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error(), http.StatusInternalServerError) return nil, errors.Wrapf(err, "failed to get License with licenseId=%s", license.Id)
} }
return license, nil return license, nil
} }
@@ -66,13 +66,13 @@ func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseReco
// Get obtains the license with the provided id parameter from the database. // Get obtains the license with the provided id parameter from the database.
// If the license doesn't exist it returns a model.AppError with // If the license doesn't exist it returns a model.AppError with
// http.StatusNotFound in the StatusCode field. // http.StatusNotFound in the StatusCode field.
func (ls SqlLicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) { func (ls SqlLicenseStore) Get(id string) (*model.LicenseRecord, error) {
obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id) obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id)
if err != nil { if err != nil {
return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError) return nil, errors.Wrapf(err, "failed to get License with licenseId=%s", id)
} }
if obj == nil { if obj == nil {
return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id, http.StatusNotFound) return nil, store.NewErrNotFound("License", id)
} }
return obj.(*model.LicenseRecord), nil return obj.(*model.LicenseRecord), nil
} }

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

@@ -493,8 +493,8 @@ type PreferenceStore interface {
} }
type LicenseStore interface { type LicenseStore interface {
Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) Save(license *model.LicenseRecord) (*model.LicenseRecord, error)
Get(id string) (*model.LicenseRecord, *model.AppError) Get(id string) (*model.LicenseRecord, error)
} }
type TokenStore interface { type TokenStore interface {

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

@@ -15,7 +15,7 @@ type LicenseStore struct {
} }
// Get provides a mock function with given fields: id // Get provides a mock function with given fields: id
func (_m *LicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) { func (_m *LicenseStore) Get(id string) (*model.LicenseRecord, error) {
ret := _m.Called(id) ret := _m.Called(id)
var r0 *model.LicenseRecord var r0 *model.LicenseRecord
@@ -27,20 +27,18 @@ func (_m *LicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) {
} }
} }
var r1 *model.AppError var r1 error
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok { if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(id) r1 = rf(id)
} else { } else {
if ret.Get(1) != nil { r1 = ret.Error(1)
r1 = ret.Get(1).(*model.AppError)
}
} }
return r0, r1 return r0, r1
} }
// Save provides a mock function with given fields: license // Save provides a mock function with given fields: license
func (_m *LicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) { func (_m *LicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, error) {
ret := _m.Called(license) ret := _m.Called(license)
var r0 *model.LicenseRecord var r0 *model.LicenseRecord
@@ -52,13 +50,11 @@ func (_m *LicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord
} }
} }
var r1 *model.AppError var r1 error
if rf, ok := ret.Get(1).(func(*model.LicenseRecord) *model.AppError); ok { if rf, ok := ret.Get(1).(func(*model.LicenseRecord) error); ok {
r1 = rf(license) r1 = rf(license)
} else { } else {
if ret.Get(1) != nil { r1 = ret.Error(1)
r1 = ret.Get(1).(*model.AppError)
}
} }
return r0, r1 return r0, r1

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

@@ -3518,7 +3518,7 @@ func (s *TimerLayerJobStore) UpdateStatusOptimistically(id string, currentStatus
return resultVar0, resultVar1 return resultVar0, resultVar1
} }
func (s *TimerLayerLicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) { func (s *TimerLayerLicenseStore) Get(id string) (*model.LicenseRecord, error) {
start := timemodule.Now() start := timemodule.Now()
resultVar0, resultVar1 := s.LicenseStore.Get(id) resultVar0, resultVar1 := s.LicenseStore.Get(id)
@@ -3534,7 +3534,7 @@ func (s *TimerLayerLicenseStore) Get(id string) (*model.LicenseRecord, *model.Ap
return resultVar0, resultVar1 return resultVar0, resultVar1
} }
func (s *TimerLayerLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) { func (s *TimerLayerLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, error) {
start := timemodule.Now() start := timemodule.Now()
resultVar0, resultVar1 := s.LicenseStore.Save(license) resultVar0, resultVar1 := s.LicenseStore.Save(license)