Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions lib/go-tc/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ func GetHandleErrorsFunc(w http.ResponseWriter, r *http.Request) func(status int
}
}

func HandleErrorsWithType(errs []error, errType ApiErrorType, handleErrs func(status int, errs ...error)) {
switch errType {
case SystemError:
handleErrs(http.StatusInternalServerError, errs...)
case DataConflictError:
handleErrs(http.StatusBadRequest, errs...)
case DataMissingError:
handleErrs(http.StatusNotFound, errs...)
case ForbiddenError:
handleErrs(http.StatusForbidden, errs...)
default:
log.Errorf("received unknown ApiErrorType from read: %s\n", errType.String())
handleErrs(http.StatusInternalServerError, errs...)
}
}

func (alerts *Alerts) ToStrings() []string {
alertStrs := []string{}
for _, alrt := range alerts.Alerts {
Expand Down
16 changes: 0 additions & 16 deletions lib/go-tc/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,4 @@ func (a AlertLevel) String() string {
return alertLevels[a]
}

type ApiErrorType int

const (
NoError ApiErrorType = iota
SystemError
DataConflictError
DataMissingError
ForbiddenError
)

var apiErrorTypes = []string{"noError", "systemError", "dataConflictError", "dataMissingError", "forbiddenError"}

func (a ApiErrorType) String() string {
return apiErrorTypes[a]
}

const CachegroupCoordinateNamePrefix = "from_cachegroup_"
39 changes: 0 additions & 39 deletions traffic_ops/traffic_ops_golang/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-rfc"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/lib/go-util"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tocookie"
Expand Down Expand Up @@ -506,44 +505,6 @@ func respWritten(r *http.Request) bool {
return r.Context().Value(APIRespWrittenKey) != nil
}

// TypeErrToAPIErr takes a slice of errors and an ApiErrorType, and converts them to the (userErr, sysErr, errCode) idiom used by the api package.
func TypeErrsToAPIErr(errs []error, errType tc.ApiErrorType) (error, error, int) {
if len(errs) == 0 {
return nil, nil, http.StatusOK
}
switch errType {
case tc.SystemError:
return nil, util.JoinErrs(errs), http.StatusInternalServerError
case tc.DataConflictError:
return util.JoinErrs(errs), nil, http.StatusBadRequest
case tc.DataMissingError:
return util.JoinErrs(errs), nil, http.StatusNotFound
default:
log.Errorln("TypeErrsToAPIErr received unknown ApiErrorType from read: " + errType.String())
return nil, util.JoinErrs(errs), http.StatusInternalServerError
}
}

// TypeErrToAPIErr takes an error and an ApiErrorType, and converts them to the (userErr, sysErr, errCode) idiom used by the api package.
func TypeErrToAPIErr(err error, errType tc.ApiErrorType) (error, error, int) {
if err == nil {
return nil, nil, http.StatusOK
}
switch errType {
case tc.SystemError:
return nil, err, http.StatusInternalServerError
case tc.DataConflictError:
return err, nil, http.StatusBadRequest
case tc.DataMissingError:
return err, nil, http.StatusNotFound
case tc.ForbiddenError:
return err, nil, http.StatusForbidden
default:
log.Errorln("TypeErrToAPIErr received unknown ApiErrorType from read: " + errType.String())
return nil, err, http.StatusInternalServerError
}
}

// small helper function to help with parsing below
func toCamelCase(str string) string {
mutable := []byte(str)
Expand Down
23 changes: 11 additions & 12 deletions traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,12 @@ func (cg *TOCacheGroup) Update() (error, error, int) {
cg.FallbackToClosest = &fbc
}

coordinateID, err, errType := cg.handleCoordinateUpdate()
if err != nil {
return api.TypeErrToAPIErr(err, errType)
coordinateID, userErr, sysErr, errCode := cg.handleCoordinateUpdate()
if userErr != nil || sysErr != nil {
return userErr, sysErr, errCode
}

err = cg.ReqInfo.Tx.Tx.QueryRow(
err := cg.ReqInfo.Tx.Tx.QueryRow(
UpdateQuery(),
cg.Name,
cg.ShortName,
Expand Down Expand Up @@ -484,19 +484,18 @@ func (cg *TOCacheGroup) Update() (error, error, int) {
return nil, nil, http.StatusOK
}

// TODO: Remove tc.ApiErrorType (see #3349)
func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, tc.ApiErrorType) {
func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {

coordinateID, err := cg.getCoordinateID()

// This is not a logic error. Because the coordinate id is recieved from the
// cachegroup table, not being able to find the coordinate is equivalent to
// not being able to find the cachegroup.
if err == sql.ErrNoRows {
return nil, fmt.Errorf("no cachegroup with id %d found", *cg.ID), tc.DataMissingError
return nil, fmt.Errorf("no cachegroup with id %d found", *cg.ID), nil, http.StatusNotFound
}
if err != nil {
return nil, tc.DBError, tc.SystemError
return nil, nil, tc.DBError, http.StatusInternalServerError
}

// If partial coordinate information is given or the coordinate information is wholly
Expand All @@ -513,17 +512,17 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, tc.ApiErrorType)
//
if cg.Latitude == nil || cg.Longitude == nil {
if err = cg.deleteCoordinate(*coordinateID); err != nil {
return nil, tc.DBError, tc.SystemError
return nil, nil, tc.DBError, http.StatusInternalServerError
}
cg.Latitude = nil
cg.Longitude = nil
return nil, nil, tc.NoError
return nil, nil, nil, http.StatusOK
}

if err = cg.updateCoordinate(); err != nil {
return nil, tc.DBError, tc.SystemError
return nil, nil, tc.DBError, http.StatusInternalServerError
}
return coordinateID, nil, tc.NoError
return coordinateID, nil, nil, http.StatusOK
}

func (cg *TOCacheGroup) getCoordinateID() (*int, error) {
Expand Down
32 changes: 16 additions & 16 deletions traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@ func (ds *TODeliveryService) Read() ([]interface{}, error, error, int) {
}

returnable := []interface{}{}
dses, errs, _ := readGetDeliveryServices(ds.APIInfo().Params, ds.APIInfo().Tx, ds.APIInfo().User)
if len(errs) > 0 {
for _, err := range errs {
if err.Error() == `id cannot parse to integer` { // TODO create const for string
return nil, errors.New("Resource not found."), nil, http.StatusNotFound //matches perl response
}
}
return nil, nil, errors.New("reading dses: " + util.JoinErrsStr(errs)), http.StatusInternalServerError
dses, userErr, sysErr, errCode := readGetDeliveryServices(ds.APIInfo().Params, ds.APIInfo().Tx, ds.APIInfo().User)

if sysErr != nil {
sysErr = errors.New("reading dses: " + sysErr.Error())
errCode = http.StatusInternalServerError
}
if userErr != nil || sysErr != nil {
return nil, userErr, sysErr, errCode
}

for _, ds := range dses {
Expand Down Expand Up @@ -797,7 +797,7 @@ func (v *TODeliveryService) DeleteQuery() string {
return `DELETE FROM deliveryservice WHERE id = :id`
}

func readGetDeliveryServices(params map[string]string, tx *sqlx.Tx, user *auth.CurrentUser) ([]tc.DeliveryServiceNullable, []error, tc.ApiErrorType) {
func readGetDeliveryServices(params map[string]string, tx *sqlx.Tx, user *auth.CurrentUser) ([]tc.DeliveryServiceNullable, error, error, int) {
if strings.HasSuffix(params["id"], ".json") {
params["id"] = params["id"][:len(params["id"])-len(".json")]
}
Expand All @@ -821,14 +821,14 @@ func readGetDeliveryServices(params map[string]string, tx *sqlx.Tx, user *auth.C

where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(params, queryParamsToSQLCols)
if len(errs) > 0 {
return nil, errs, tc.DataConflictError
return nil, util.JoinErrs(errs), nil, http.StatusBadRequest
}

tenantIDs, err := tenant.GetUserTenantIDListTx(tx.Tx, user.TenantID)

if err != nil {
log.Errorln("received error querying for user's tenants: " + err.Error())
return nil, []error{tc.DBError}, tc.SystemError
return nil, nil, tc.DBError, http.StatusInternalServerError
}

where, queryValues = dbhelpers.AddTenancyCheck(where, queryValues, "ds.tenant_id", tenantIDs)
Expand Down Expand Up @@ -957,10 +957,10 @@ func getDSType(tx *sql.Tx, xmlid string) (tc.DSType, bool, error) {
return tc.DSTypeFromString(name), true, nil
}

func GetDeliveryServices(query string, queryValues map[string]interface{}, tx *sqlx.Tx) ([]tc.DeliveryServiceNullable, []error, tc.ApiErrorType) {
func GetDeliveryServices(query string, queryValues map[string]interface{}, tx *sqlx.Tx) ([]tc.DeliveryServiceNullable, error, error, int) {
rows, err := tx.NamedQuery(query, queryValues)
if err != nil {
return nil, []error{fmt.Errorf("querying: %v", err)}, tc.SystemError
return nil, nil, fmt.Errorf("querying: %v", err), http.StatusInternalServerError
}
defer rows.Close()

Expand Down Expand Up @@ -1037,7 +1037,7 @@ func GetDeliveryServices(query string, queryValues map[string]interface{}, tx *s
&cdnDomain)

if err != nil {
return nil, []error{fmt.Errorf("getting delivery services: %v", err)}, tc.SystemError
return nil, nil, fmt.Errorf("getting delivery services: %v", err), http.StatusInternalServerError
}

ds.ConsistentHashQueryParams = []string{}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ func GetDeliveryServices(query string, queryValues map[string]interface{}, tx *s

matchLists, err := GetDeliveryServicesMatchLists(dsNames, tx.Tx)
if err != nil {
return nil, []error{errors.New("getting delivery service matchlists: " + err.Error())}, tc.SystemError
return nil, nil, errors.New("getting delivery service matchlists: " + err.Error()), http.StatusInternalServerError
}
for i, ds := range dses {
matchList, ok := matchLists[*ds.XMLID]
Expand All @@ -1081,7 +1081,7 @@ func GetDeliveryServices(query string, queryValues map[string]interface{}, tx *s
dses[i] = ds
}

return dses, nil, tc.NoError
return dses, nil, nil, http.StatusOK
}

func updateSSLKeys(ds *tc.DeliveryServiceNullable, hostName string, tx *sql.Tx, cfg *config.Config) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func (dss *TODSSDeliveryService) Read() ([]interface{}, error, error, int) {
user := dss.APIInfo().User

if err := api.IsInt(params["id"]); err != nil {
return nil, errors.New("Resource not found."), nil, http.StatusNotFound //matches perl response
return nil, err, nil, http.StatusBadRequest
}

if _, ok := params["orderby"]; !ok {
Expand Down Expand Up @@ -597,9 +597,12 @@ func (dss *TODSSDeliveryService) Read() ([]interface{}, error, error, int) {
log.Debugln("generated deliveryServices query: " + query)
log.Debugf("executing with values: %++v\n", queryValues)

dses, errs, _ := deliveryservice.GetDeliveryServices(query, queryValues, dss.APIInfo().Tx)
if len(errs) > 0 {
return nil, nil, errors.New("reading server dses: " + util.JoinErrsStr(errs)), http.StatusInternalServerError
dses, userErr, sysErr, _ := deliveryservice.GetDeliveryServices(query, queryValues, dss.APIInfo().Tx)
if sysErr != nil {
sysErr = fmt.Errorf("reading server dses: %v ", sysErr)
}
if userErr != nil || sysErr != nil {
return nil, userErr, sysErr, http.StatusInternalServerError
}

for _, ds := range dses {
Expand Down
Loading