Idiomatic error handling for app/{job,license,login}.go (#9474)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ee672a72e4
Коммит
a551b375fa
18
app/job.go
18
app/job.go
@@ -8,11 +8,11 @@ import (
|
||||
)
|
||||
|
||||
func (a *App) GetJob(id string) (*model.Job, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Job().Get(id); result.Err != nil {
|
||||
result := <-a.Srv.Store.Job().Get(id)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.Job), nil
|
||||
}
|
||||
return result.Data.(*model.Job), nil
|
||||
}
|
||||
|
||||
func (a *App) GetJobsPage(page int, perPage int) ([]*model.Job, *model.AppError) {
|
||||
@@ -20,11 +20,11 @@ func (a *App) GetJobsPage(page int, perPage int) ([]*model.Job, *model.AppError)
|
||||
}
|
||||
|
||||
func (a *App) GetJobs(offset int, limit int) ([]*model.Job, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Job().GetAllPage(offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.Job().GetAllPage(offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.Job), nil
|
||||
}
|
||||
return result.Data.([]*model.Job), nil
|
||||
}
|
||||
|
||||
func (a *App) GetJobsByTypePage(jobType string, page int, perPage int) ([]*model.Job, *model.AppError) {
|
||||
@@ -32,11 +32,11 @@ func (a *App) GetJobsByTypePage(jobType string, page int, perPage int) ([]*model
|
||||
}
|
||||
|
||||
func (a *App) GetJobsByType(jobType string, offset int, limit int) ([]*model.Job, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Job().GetAllByTypePage(jobType, offset, limit); result.Err != nil {
|
||||
result := <-a.Srv.Store.Job().GetAllByTypePage(jobType, offset, limit)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.Job), nil
|
||||
}
|
||||
return result.Data.([]*model.Job), nil
|
||||
}
|
||||
|
||||
func (a *App) CreateJob(job *model.Job) (*model.Job, *model.AppError) {
|
||||
|
||||
@@ -46,47 +46,45 @@ func (a *App) LoadLicense() {
|
||||
}
|
||||
|
||||
func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
|
||||
var license *model.License
|
||||
|
||||
if success, licenseStr := utils.ValidateLicense(licenseBytes); success {
|
||||
license = model.LicenseFromJson(strings.NewReader(licenseStr))
|
||||
|
||||
if result := <-a.Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil {
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
uniqueUserCount := result.Data.(int64)
|
||||
|
||||
if uniqueUserCount > int64(*license.Features.Users) {
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
if ok := a.SetLicense(license); !ok {
|
||||
return nil, model.NewAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
record := &model.LicenseRecord{}
|
||||
record.Id = license.Id
|
||||
record.Bytes = string(licenseBytes)
|
||||
rchan := a.Srv.Store.License().Save(record)
|
||||
|
||||
if result := <-rchan; result.Err != nil {
|
||||
a.RemoveLicense()
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
sysVar := &model.System{}
|
||||
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
|
||||
sysVar.Value = license.Id
|
||||
schan := a.Srv.Store.System().SaveOrUpdate(sysVar)
|
||||
|
||||
if result := <-schan; result.Err != nil {
|
||||
a.RemoveLicense()
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
success, licenseStr := utils.ValidateLicense(licenseBytes)
|
||||
if !success {
|
||||
return nil, model.NewAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "", http.StatusBadRequest)
|
||||
}
|
||||
license := model.LicenseFromJson(strings.NewReader(licenseStr))
|
||||
|
||||
result := <-a.Srv.Store.User().AnalyticsUniqueUserCount("")
|
||||
if result.Err != nil {
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
uniqueUserCount := result.Data.(int64)
|
||||
|
||||
if uniqueUserCount > int64(*license.Features.Users) {
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if ok := a.SetLicense(license); !ok {
|
||||
return nil, model.NewAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
record := &model.LicenseRecord{}
|
||||
record.Id = license.Id
|
||||
record.Bytes = string(licenseBytes)
|
||||
rchan := a.Srv.Store.License().Save(record)
|
||||
|
||||
if result := <-rchan; result.Err != nil {
|
||||
a.RemoveLicense()
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
sysVar := &model.System{}
|
||||
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
|
||||
sysVar.Value = license.Id
|
||||
schan := a.Srv.Store.System().SaveOrUpdate(sysVar)
|
||||
|
||||
if result := <-schan; result.Err != nil {
|
||||
a.RemoveLicense()
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.ReloadConfig()
|
||||
a.InvalidateAllCaches()
|
||||
|
||||
14
app/login.go
14
app/login.go
@@ -96,17 +96,16 @@ func (a *App) GetUserForLogin(id, loginId string) (*model.User, *model.AppError)
|
||||
|
||||
// If we are given a userID then fail if we can't find a user with that ID
|
||||
if len(id) != 0 {
|
||||
if user, err := a.GetUser(id); err != nil {
|
||||
user, err := a.GetUser(id)
|
||||
if err != nil {
|
||||
if err.Id != store.MISSING_ACCOUNT_ERROR {
|
||||
err.StatusCode = http.StatusInternalServerError
|
||||
return nil, err
|
||||
} else {
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
return user, nil
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// Try to get the user by username/email
|
||||
@@ -200,7 +199,6 @@ func (a *App) DoLogin(w http.ResponseWriter, r *http.Request, user *model.User,
|
||||
func GetProtocol(r *http.Request) string {
|
||||
if r.Header.Get(model.HEADER_FORWARDED_PROTO) == "https" || r.TLS != nil {
|
||||
return "https"
|
||||
} else {
|
||||
return "http"
|
||||
}
|
||||
return "http"
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user