Skip to content

Commit

Permalink
feat: refactors http response
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYa-0110 committed Jan 24, 2025
1 parent 9a4d108 commit 62cfc59
Show file tree
Hide file tree
Showing 43 changed files with 647 additions and 598 deletions.
3 changes: 2 additions & 1 deletion server/controller/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ const (
HEADER_KEY_CONTENT_TYPE = "Content-Type"
HEADER_KEY_ACCEPT = "Accept"
CONTENT_TYPE_JSON = "application/json"
CONTEXT_TYPE_FORM = "application/x-www-form-urlencoded"
CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"
CONTENT_TYPE_CSV = "text/csv"
ACCEPT_JSON = "application/json, text/plain"

HEADER_KEY_X_ORG_ID = "X-Org-Id"
Expand Down
2 changes: 1 addition & 1 deletion server/controller/common/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ func CURLForm(method string, url string, values url.Values, options ...HeaderOpt
log.Error(err)
return errResponse, err
}
req.Header.Set(HEADER_KEY_CONTENT_TYPE, CONTEXT_TYPE_FORM)
req.Header.Set(HEADER_KEY_CONTENT_TYPE, CONTENT_TYPE_FORM)
return doRequest(req, url, options...)
}
62 changes: 50 additions & 12 deletions server/controller/http/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,68 @@

package common

import "errors"
import (
"errors"
"net/http"
)

const (
SUCCESS = "SUCCESS"
FAIL = "FAIL"
CONFIG_PENDING = "CONFIG_PENDING"
// map to http.StatusOK
SUCCESS = "SUCCESS"

// map to http.StatusBadRequest
INVALID_PARAMETERS = "INVALID_PARAMETERS"
RESOURCE_NOT_FOUND = "RESOURCE_NOT_FOUND"
RESOURCE_ALREADY_EXIST = "RESOURCE_ALREADY_EXIST"
PARAMETER_ILLEGAL = "PARAMETER_ILLEGAL"
INVALID_POST_DATA = "INVALID_POST_DATA"
SERVER_ERROR = "SERVER_ERROR"
RESOURCE_NUM_EXCEEDED = "RESOURCE_NUM_EXCEEDED"
SELECTED_RESOURCES_NUM_EXCEEDED = "SELECTED_RESOURCES_NUM_EXCEEDED"
SERVICE_UNAVAILABLE = "SERVICE_UNAVAILABLE"
GET_ORG_DB_FAIL = "GET_ORG_DB_FAIL"
K8S_SET_VTAP_FAIL = "K8S_SET_VTAP_FAIL"
ORG_ID_INVALID = "ORG_ID_INVALID"
CHECK_SCOPE_TEAMS_FAIL = "CHECK_SCOPE_TEAMS_FAIL"
SET_RESOUORCE_FAIL = "SET_RESOUORCE_FAIL"
NO_PERMISSIONS = "NO_PERMISSIONS"
RESOURCE_NOT_FOUND = "RESOURCE_NOT_FOUND" // TODO map to http.StatusNotFound ?
// K8S_SET_VTAP_FAIL = "K8S_SET_VTAP_FAIL"
// SET_RESOUORCE_FAIL = "SET_RESOUORCE_FAIL"

// map to http.StatusInternalServerError
FAIL = "FAIL"
SERVER_ERROR = "SERVER_ERROR"
CONFIG_PENDING = "CONFIG_PENDING"

// map to http.StatusServiceUnavailable
SERVICE_UNAVAILABLE = "SERVICE_UNAVAILABLE"

// map to http.StatusForbidden
NO_PERMISSIONS = "NO_PERMISSIONS"

// map to http.StatusPartialContent
PARTIAL_CONTENT = "PARTIAL_RESULT"
)

var (
OPT_STATUS_TO_HTTP_STATUS = map[string]int{
SUCCESS: http.StatusOK,

INVALID_PARAMETERS: http.StatusBadRequest,
RESOURCE_ALREADY_EXIST: http.StatusBadRequest,
PARAMETER_ILLEGAL: http.StatusBadRequest,
INVALID_POST_DATA: http.StatusBadRequest,
RESOURCE_NUM_EXCEEDED: http.StatusBadRequest,
SELECTED_RESOURCES_NUM_EXCEEDED: http.StatusBadRequest,
GET_ORG_DB_FAIL: http.StatusBadRequest,
ORG_ID_INVALID: http.StatusBadRequest,
CHECK_SCOPE_TEAMS_FAIL: http.StatusBadRequest,
RESOURCE_NOT_FOUND: http.StatusBadRequest,

FAIL: http.StatusInternalServerError,
SERVER_ERROR: http.StatusInternalServerError,

SERVICE_UNAVAILABLE: http.StatusServiceUnavailable,

NO_PERMISSIONS: http.StatusForbidden,

// http status codes
STATUES_PARTIAL_CONTENT = "STATUES_PARTIAL_CONTENT" // 206
PARTIAL_CONTENT: http.StatusPartialContent,
}
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
* limitations under the License.
*/

package common
package response

import (
"encoding/json"
)

type ServiceError struct {
type serviceError struct {
Status string
Message string
}

func (e *ServiceError) Error() string {
func (e *serviceError) Error() string {
err, _ := json.Marshal(e)
return string(err)
}

func NewError(status string, message string) error {
return &ServiceError{
func ServiceError(status string, message string) error {
return &serviceError{
Status: status,
Message: message,
}
Expand Down
Loading

0 comments on commit 62cfc59

Please sign in to comment.