refine the returnning errors

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-04-18 17:15:55 +08:00
parent 1f481e492c
commit 608354dd04
4 changed files with 62 additions and 28 deletions

View File

@ -16,7 +16,6 @@ package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
@ -27,7 +26,6 @@ import (
"github.com/goharbor/harbor/src/jobservice/core"
"github.com/goharbor/harbor/src/jobservice/errs"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/lcm"
"github.com/goharbor/harbor/src/jobservice/logger"
"github.com/pkg/errors"
)
@ -84,13 +82,19 @@ func (dh *DefaultHandler) HandleLaunchJobReq(w http.ResponseWriter, req *http.Re
// Pass request to the controller for the follow-up.
jobStats, err := dh.controller.LaunchJob(jobReq)
if err != nil {
if errs.IsConflictError(err) {
code := http.StatusInternalServerError
if errs.IsBadRequestError(err) {
// Bad request
code = http.StatusBadRequest
} else if errs.IsConflictError(err) {
// Conflict error
dh.handleError(w, req, http.StatusConflict, err)
code = http.StatusConflict
} else {
// General error
dh.handleError(w, req, http.StatusInternalServerError, errs.LaunchJobError(err))
err = errs.LaunchJobError(err)
}
dh.handleError(w, req, code, err)
return
}
@ -109,12 +113,14 @@ func (dh *DefaultHandler) HandleGetJobReq(w http.ResponseWriter, req *http.Reque
jobStats, err := dh.controller.GetJob(jobID)
if err != nil {
code := http.StatusInternalServerError
backErr := errs.GetJobStatsError(err)
if errs.IsObjectNotFoundError(err) {
code = http.StatusNotFound
backErr = err
} else if errs.IsBadRequestError(err) {
code = http.StatusBadRequest
} else {
err = errs.GetJobStatsError(err)
}
dh.handleError(w, req, code, backErr)
dh.handleError(w, req, code, err)
return
}
@ -144,21 +150,23 @@ func (dh *DefaultHandler) HandleJobActionReq(w http.ResponseWriter, req *http.Re
}
// Only support stop command now
cmd := lcm.OPCommand(jobActionReq.Action)
cmd := job.OPCommand(jobActionReq.Action)
if !cmd.IsStop() {
dh.handleError(w, req, http.StatusNotImplemented, errs.UnknownActionNameError(fmt.Errorf("command: %s", jobActionReq.Action)))
dh.handleError(w, req, http.StatusNotImplemented, errs.UnknownActionNameError(errors.Errorf("command: %s", jobActionReq.Action)))
return
}
// Stop job
if err := dh.controller.StopJob(jobID); err != nil {
code := http.StatusInternalServerError
backErr := errs.StopJobError(err)
if errs.IsObjectNotFoundError(err) {
code = http.StatusNotFound
backErr = err
} else if errs.IsBadRequestError(err) {
code = http.StatusBadRequest
} else {
err = errs.StopJobError(err)
}
dh.handleError(w, req, code, backErr)
dh.handleError(w, req, code, err)
return
}
@ -199,12 +207,14 @@ func (dh *DefaultHandler) HandleJobLogReq(w http.ResponseWriter, req *http.Reque
logData, err := dh.controller.GetJobLogData(jobID)
if err != nil {
code := http.StatusInternalServerError
backErr := errs.GetJobLogError(err)
if errs.IsObjectNotFoundError(err) {
code = http.StatusNotFound
backErr = err
} else if errs.IsBadRequestError(err) {
code = http.StatusBadRequest
} else {
err = errs.GetJobLogError(err)
}
dh.handleError(w, req, code, backErr)
dh.handleError(w, req, code, err)
return
}
@ -239,7 +249,7 @@ func (dh *DefaultHandler) handleError(w http.ResponseWriter, req *http.Request,
func (dh *DefaultHandler) preCheck(w http.ResponseWriter, req *http.Request) bool {
if dh.controller == nil {
dh.handleError(w, req, http.StatusInternalServerError, errs.MissingBackendHandlerError(fmt.Errorf("nil controller")))
dh.handleError(w, req, http.StatusInternalServerError, errs.MissingBackendHandlerError(errors.Errorf("nil controller")))
return false
}

View File

@ -22,6 +22,7 @@ import (
"github.com/goharbor/harbor/src/jobservice/common/query"
"github.com/goharbor/harbor/src/jobservice/common/utils"
"github.com/goharbor/harbor/src/jobservice/errs"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/lcm"
"github.com/goharbor/harbor/src/jobservice/worker"
@ -48,18 +49,18 @@ func NewController(backendWorker worker.Interface, ctl lcm.Controller) Interface
// LaunchJob is implementation of same method in core interface.
func (bc *basicController) LaunchJob(req *job.Request) (res *job.Stats, err error) {
if err := validJobReq(req); err != nil {
return nil, err
return nil, errs.BadRequestError(err)
}
// Validate job name
jobType, isKnownJob := bc.backendWorker.IsKnownJob(req.Job.Name)
if !isKnownJob {
return nil, errors.Errorf("job with name '%s' is unknown", req.Job.Name)
return nil, errs.BadRequestError(errors.Errorf("job with name '%s' is unknown", req.Job.Name))
}
// Validate parameters
if err := bc.backendWorker.ValidateJobParameters(jobType, req.Job.Parameters); err != nil {
return nil, err
return nil, errs.BadRequestError(err)
}
// Enqueue job regarding of the kind
@ -95,7 +96,7 @@ func (bc *basicController) LaunchJob(req *job.Request) (res *job.Stats, err erro
// GetJob is implementation of same method in core interface.
func (bc *basicController) GetJob(jobID string) (*job.Stats, error) {
if utils.IsEmptyStr(jobID) {
return nil, errors.New("empty job ID")
return nil, errs.BadRequestError(errors.New("empty job ID"))
}
t, err := bc.ctl.Track(jobID)
@ -109,7 +110,7 @@ func (bc *basicController) GetJob(jobID string) (*job.Stats, error) {
// StopJob is implementation of same method in core interface.
func (bc *basicController) StopJob(jobID string) error {
if utils.IsEmptyStr(jobID) {
return errors.New("empty job ID")
return errs.BadRequestError(errors.New("empty job ID"))
}
return bc.backendWorker.StopJob(jobID)
@ -118,7 +119,7 @@ func (bc *basicController) StopJob(jobID string) error {
// RetryJob is implementation of same method in core interface.
func (bc *basicController) RetryJob(jobID string) error {
if utils.IsEmptyStr(jobID) {
return errors.New("empty job ID")
return errs.BadRequestError(errors.New("empty job ID"))
}
return bc.backendWorker.RetryJob(jobID)
@ -127,7 +128,7 @@ func (bc *basicController) RetryJob(jobID string) error {
// GetJobLogData is used to return the log text data for the specified job if exists
func (bc *basicController) GetJobLogData(jobID string) ([]byte, error) {
if utils.IsEmptyStr(jobID) {
return nil, errors.New("empty job ID")
return nil, errs.BadRequestError(errors.New("empty job ID"))
}
logData, err := logger.Retrieve(jobID)
@ -146,7 +147,7 @@ func (bc *basicController) CheckStatus() (*worker.Stats, error) {
// GetPeriodicExecutions gets the periodic executions for the specified periodic job
func (bc *basicController) GetPeriodicExecutions(periodicJobID string, query *query.Parameter) ([]*job.Stats, int64, error) {
if utils.IsEmptyStr(periodicJobID) {
return nil, 0, errors.New("nil periodic job ID")
return nil, 0, errs.BadRequestError(errors.New("nil periodic job ID"))
}
t, err := bc.ctl.Track(periodicJobID)

View File

@ -23,8 +23,6 @@ import (
const (
// JobStoppedErrorCode is code for jobStoppedError
JobStoppedErrorCode = 10000 + iota
// JobCancelledErrorCode is code for jobCancelledError
JobCancelledErrorCode
// ReadRequestBodyErrorCode is code for the error of reading http request body error
ReadRequestBodyErrorCode
// HandleJSONDataErrorCode is code for the error of handling json data error
@ -51,6 +49,8 @@ const (
UnAuthorizedErrorCode
// ResourceConflictsErrorCode is code for the error of resource conflicting
ResourceConflictsErrorCode
// BadRequestErrorCode is code for the error of bad request
BadRequestErrorCode
)
// baseError ...
@ -180,6 +180,22 @@ func ConflictError(object string) error {
}
}
// badRequestError is designed for the case of bad request
type badRequestError struct {
baseError
}
// BadRequestError returns the error of handing bad request case
func BadRequestError(object interface{}) error {
return badRequestError{
baseError{
Code: BadRequestErrorCode,
Err: "bad request",
Description: fmt.Sprintf("%s", object),
},
}
}
// IsJobStoppedError return true if the error is jobStoppedError
func IsJobStoppedError(err error) bool {
_, ok := err.(jobStoppedError)
@ -197,3 +213,9 @@ func IsConflictError(err error) bool {
_, ok := err.(conflictError)
return ok
}
// IsBadRequestError returns true if the error is badRequestError
func IsBadRequestError(err error) bool {
_, ok := err.(badRequestError)
return ok
}

View File

@ -20,6 +20,7 @@ import (
"github.com/goharbor/harbor/src/jobservice/common/query"
"github.com/goharbor/harbor/src/jobservice/common/rds"
"github.com/goharbor/harbor/src/jobservice/common/utils"
"github.com/goharbor/harbor/src/jobservice/errs"
"github.com/goharbor/harbor/src/jobservice/logger"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
@ -501,7 +502,7 @@ func (bt *basicTracker) retrieve() error {
}
if vals == nil || len(vals) == 0 {
return errors.Errorf("nothing got from backend for job '%s'", bt.jobID)
return errs.NoObjectFoundError(bt.jobID)
}
res := &Stats{