Skip to content

Commit

Permalink
Log error message in server side
Browse files Browse the repository at this point in the history
Record the errors which are only returned to frontend in the server side for easy debugging.
  • Loading branch information
ywk253100 committed May 21, 2018
1 parent 63bb991 commit 34d6e9b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src/ui/api/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
package api

import (
"net/http"

"github.com/vmware/harbor/src/common"
"github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/utils/log"
"net/http"
)

// InternalAPI handles request of harbor admin...
Expand All @@ -44,7 +45,8 @@ func (ia *InternalAPI) Prepare() {
func (ia *InternalAPI) SyncRegistry() {
err := SyncRegistry(ia.ProjectMgr)
if err != nil {
ia.CustomAbort(http.StatusInternalServerError, "internal error")
ia.HandleInternalServerError(err.Error())
return
}
}

Expand Down
30 changes: 20 additions & 10 deletions src/ui/api/replication_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func (ra *RepJobAPI) Prepare() {
if len(ra.GetStringFromPath(":id")) != 0 {
id, err := ra.GetInt64FromPath(":id")
if err != nil {
ra.CustomAbort(http.StatusBadRequest, "ID is invalid")
ra.HandleBadRequest(fmt.Sprintf("invalid ID: %s", ra.GetStringFromPath(":id")))
return
}
ra.jobID = id
}
Expand All @@ -63,7 +64,8 @@ func (ra *RepJobAPI) List() {

policyID, err := ra.GetInt64("policy_id")
if err != nil || policyID <= 0 {
ra.CustomAbort(http.StatusBadRequest, "invalid policy_id")
ra.HandleBadRequest(fmt.Sprintf("invalid policy_id: %s", ra.GetString("policy_id")))
return
}

policy, err := core.GlobalController.GetPolicy(policyID)
Expand All @@ -73,7 +75,8 @@ func (ra *RepJobAPI) List() {
}

if policy.ID == 0 {
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("policy %d not found", policyID))
ra.HandleNotFound(fmt.Sprintf("policy %d not found", policyID))
return
}

if !ra.SecurityCtx.HasAllPerm(policy.ProjectIDs[0]) {
Expand All @@ -95,7 +98,8 @@ func (ra *RepJobAPI) List() {
if len(startTimeStr) != 0 {
i, err := strconv.ParseInt(startTimeStr, 10, 64)
if err != nil {
ra.CustomAbort(http.StatusBadRequest, "invalid start_time")
ra.HandleBadRequest(fmt.Sprintf("invalid start_time: %s", startTimeStr))
return
}
t := time.Unix(i, 0)
query.StartTime = &t
Expand All @@ -105,7 +109,8 @@ func (ra *RepJobAPI) List() {
if len(endTimeStr) != 0 {
i, err := strconv.ParseInt(endTimeStr, 10, 64)
if err != nil {
ra.CustomAbort(http.StatusBadRequest, "invalid end_time")
ra.HandleBadRequest(fmt.Sprintf("invalid end_time: %s", endTimeStr))
return
}
t := time.Unix(i, 0)
query.EndTime = &t
Expand Down Expand Up @@ -133,7 +138,8 @@ func (ra *RepJobAPI) List() {
// Delete ...
func (ra *RepJobAPI) Delete() {
if ra.jobID == 0 {
ra.CustomAbort(http.StatusBadRequest, "id is nil")
ra.HandleBadRequest("ID is nil")
return
}

job, err := dao.GetRepJob(ra.jobID)
Expand All @@ -143,11 +149,13 @@ func (ra *RepJobAPI) Delete() {
}

if job == nil {
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("job %d not found", ra.jobID))
ra.HandleNotFound(fmt.Sprintf("job %d not found", ra.jobID))
return
}

if job.Status == models.JobPending || job.Status == models.JobRunning {
ra.CustomAbort(http.StatusBadRequest, fmt.Sprintf("job is %s, can not be deleted", job.Status))
ra.HandleBadRequest(fmt.Sprintf("job is %s, can not be deleted", job.Status))
return
}

if err = dao.DeleteRepJob(ra.jobID); err != nil {
Expand All @@ -159,7 +167,8 @@ func (ra *RepJobAPI) Delete() {
// GetLog ...
func (ra *RepJobAPI) GetLog() {
if ra.jobID == 0 {
ra.CustomAbort(http.StatusBadRequest, "id is nil")
ra.HandleBadRequest("ID is nil")
return
}

job, err := dao.GetRepJob(ra.jobID)
Expand Down Expand Up @@ -211,7 +220,8 @@ func (ra *RepJobAPI) StopJobs() {
}

if policy.ID == 0 {
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("policy %d not found", req.PolicyID))
ra.HandleNotFound(fmt.Sprintf("policy %d not found", req.PolicyID))
return
}

jobs, err := dao.GetRepJobs(&models.RepJobQuery{
Expand Down
12 changes: 8 additions & 4 deletions src/ui/api/replication_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (pa *RepPolicyAPI) Get() {
}

if policy.ID == 0 {
pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
pa.HandleNotFound(fmt.Sprintf("policy %d not found", id))
return
}

if !pa.SecurityCtx.HasAllPerm(policy.ProjectIDs[0]) {
Expand All @@ -85,7 +86,8 @@ func (pa *RepPolicyAPI) List() {
if len(projectIDStr) > 0 {
projectID, err := strconv.ParseInt(projectIDStr, 10, 64)
if err != nil || projectID <= 0 {
pa.CustomAbort(http.StatusBadRequest, "invalid project ID")
pa.HandleBadRequest(fmt.Sprintf("invalid project ID: %s", projectIDStr))
return
}
queryParam.ProjectID = projectID
}
Expand Down Expand Up @@ -183,7 +185,8 @@ func (pa *RepPolicyAPI) Put() {
}

if originalPolicy.ID == 0 {
pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
pa.HandleNotFound(fmt.Sprintf("policy %d not found", id))
return
}

policy := &api_models.ReplicationPolicy{}
Expand Down Expand Up @@ -246,7 +249,8 @@ func (pa *RepPolicyAPI) Delete() {
}

if policy.ID == 0 {
pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
pa.HandleNotFound(fmt.Sprintf("policy %d not found", id))
return
}

count, err := dao.GetTotalCountOfRepJobs(&models.RepJobQuery{
Expand Down
16 changes: 11 additions & 5 deletions src/ui/api/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,19 @@ func (ra *RepositoryAPI) Delete() {
if len(tag) == 0 {
tagList, err := rc.ListTag()
if err != nil {
log.Errorf("error occurred while listing tags of %s: %v", repoName, err)

if regErr, ok := err.(*registry_error.HTTPError); ok {
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
}

log.Errorf("error occurred while listing tags of %s: %v", repoName, err)
ra.CustomAbort(http.StatusInternalServerError, "internal error")
}

// TODO remove the logic if the bug of registry is fixed
if len(tagList) == 0 {
ra.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
ra.HandleNotFound(fmt.Sprintf("no tags found for repository %s", repoName))
return
}

tags = append(tags, tagList...)
Expand Down Expand Up @@ -279,6 +281,7 @@ func (ra *RepositoryAPI) Delete() {
if regErr.StatusCode == http.StatusNotFound {
continue
}
log.Errorf("failed to delete tag %s: %v", t, err)
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
}
log.Errorf("error occurred while deleting tag %s:%s: %v", repoName, t, err)
Expand Down Expand Up @@ -588,7 +591,8 @@ func (ra *RepositoryAPI) GetManifests() {
}

if version != "v1" && version != "v2" {
ra.CustomAbort(http.StatusBadRequest, "version should be v1 or v2")
ra.HandleBadRequest("version should be v1 or v2")
return
}

projectName, _ := utils.ParseRepository(repoName)
Expand Down Expand Up @@ -622,11 +626,12 @@ func (ra *RepositoryAPI) GetManifests() {

manifest, err := getManifest(rc, tag, version)
if err != nil {
log.Errorf("error occurred while getting manifest of %s:%s: %v", repoName, tag, err)

if regErr, ok := err.(*registry_error.HTTPError); ok {
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
}

log.Errorf("error occurred while getting manifest of %s:%s: %v", repoName, tag, err)
ra.CustomAbort(http.StatusInternalServerError, "internal error")
}

Expand Down Expand Up @@ -680,7 +685,8 @@ func getManifest(client *registry.Repository,
func (ra *RepositoryAPI) GetTopRepos() {
count, err := ra.GetInt("count", 10)
if err != nil || count <= 0 {
ra.CustomAbort(http.StatusBadRequest, "invalid count")
ra.HandleBadRequest(fmt.Sprintf("invalid count: %s", ra.GetString("count")))
return
}

projectIDs := []int64{}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/api/scan_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func (sj *ScanJobAPI) Prepare() {
}
id, err := sj.GetInt64FromPath(":id")
if err != nil {
sj.CustomAbort(http.StatusBadRequest, "ID is invalid")
sj.HandleBadRequest("invalid ID")
return
}
sj.jobID = id

Expand Down
25 changes: 17 additions & 8 deletions src/ui/api/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func (t *TargetAPI) Get() {
}

if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
t.HandleNotFound(fmt.Sprintf("target %d not found", id))
return
}

target.Password = ""
Expand Down Expand Up @@ -174,7 +175,8 @@ func (t *TargetAPI) Post() {
}

if ta != nil {
t.CustomAbort(http.StatusConflict, "name is already used")
t.HandleConflict("name is already used")
return
}

ta, err = dao.GetRepTargetByEndpoint(target.URL)
Expand All @@ -184,7 +186,8 @@ func (t *TargetAPI) Post() {
}

if ta != nil {
t.CustomAbort(http.StatusConflict, fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
t.HandleConflict(fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
return
}

if len(target.Password) != 0 {
Expand Down Expand Up @@ -215,7 +218,8 @@ func (t *TargetAPI) Put() {
}

if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
t.HandleNotFound(fmt.Sprintf("target %d not found", id))
return
}

if len(target.Password) != 0 {
Expand Down Expand Up @@ -264,7 +268,8 @@ func (t *TargetAPI) Put() {
}

if ta != nil {
t.CustomAbort(http.StatusConflict, "name is already used")
t.HandleConflict("name is already used")
return
}
}

Expand All @@ -276,7 +281,8 @@ func (t *TargetAPI) Put() {
}

if ta != nil {
t.CustomAbort(http.StatusConflict, fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
t.HandleConflict(fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
return
}
}

Expand Down Expand Up @@ -305,7 +311,8 @@ func (t *TargetAPI) Delete() {
}

if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
t.HandleNotFound(fmt.Sprintf("target %d not found", id))
return
}

policies, err := dao.GetRepPolicyByTarget(id)
Expand All @@ -315,6 +322,7 @@ func (t *TargetAPI) Delete() {
}

if len(policies) > 0 {
log.Error("the target is used by policies, can not be deleted")
t.CustomAbort(http.StatusPreconditionFailed, "the target is used by policies, can not be deleted")
}

Expand Down Expand Up @@ -346,7 +354,8 @@ func (t *TargetAPI) ListPolicies() {
}

if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
t.HandleNotFound(fmt.Sprintf("target %d not found", id))
return
}

policies, err := dao.GetRepPolicyByTarget(id)
Expand Down
3 changes: 2 additions & 1 deletion src/ui/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ func (ua *UserAPI) ChangePassword() {
}

if req.NewPassword == "" {
ua.CustomAbort(http.StatusBadRequest, "please_input_new_password")
ua.HandleBadRequest("new password is null")
return
}
updateUser := models.User{UserID: ua.userID, Password: req.NewPassword, Salt: user.Salt}
err = dao.ChangeUserPassword(updateUser, req.OldPassword)
Expand Down

0 comments on commit 34d6e9b

Please sign in to comment.