[#217] Add errors for cors

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
Angira Kekteeva 2021-10-08 16:58:21 +03:00 committed by Kirillov Denis
parent bbb6727054
commit 5b4b9df031
2 changed files with 23 additions and 3 deletions

View file

@ -312,6 +312,10 @@ const (
ErrAdminAccountNotEligible ErrAdminAccountNotEligible
ErrServiceAccountNotFound ErrServiceAccountNotFound
ErrPostPolicyConditionInvalidFormat ErrPostPolicyConditionInvalidFormat
//CORS configuration errors.
ErrCORSUnsupportedMethod
ErrCORSWildcardExposeHeaders
) )
// error code to Error structure, these fields carry respective // error code to Error structure, these fields carry respective
@ -1909,6 +1913,18 @@ var errorCodes = errorCodeMap{
Description: "Invalid according to Policy: Policy Condition failed", Description: "Invalid according to Policy: Policy Condition failed",
HTTPStatusCode: http.StatusForbidden, HTTPStatusCode: http.StatusForbidden,
}, },
ErrCORSUnsupportedMethod: {
ErrCode: ErrCORSUnsupportedMethod,
Code: "InvalidRequest",
Description: "Found unsupported HTTP method in CORS config",
HTTPStatusCode: http.StatusBadRequest,
},
ErrCORSWildcardExposeHeaders: {
ErrCode: ErrCORSWildcardExposeHeaders,
Code: "InvalidRequest",
Description: "ExposeHeader \"*\" contains wildcard. We currently do not support wildcard for ExposeHeader",
HTTPStatusCode: http.StatusBadRequest,
},
// Add your error structure here. // Add your error structure here.
} }
@ -1945,6 +1961,11 @@ func GetAPIError(code ErrorCode) Error {
return errorCodes.toAPIErr(ErrInternalError) return errorCodes.toAPIErr(ErrInternalError)
} }
// GetAPIErrorWithError provides API Error with additional error message for input API error code.
func GetAPIErrorWithError(code ErrorCode, err error) Error {
return errorCodes.toAPIErrWithErr(code, err)
}
// ObjectError - error that linked to specific object. // ObjectError - error that linked to specific object.
type ObjectError struct { type ObjectError struct {
Err error Err error

View file

@ -262,13 +262,12 @@ func checkCORS(cors *CORSConfiguration) error {
for _, r := range cors.CORSRules { for _, r := range cors.CORSRules {
for _, m := range r.AllowedMethods { for _, m := range r.AllowedMethods {
if _, ok := supportedMethods[m]; !ok { if _, ok := supportedMethods[m]; !ok {
return fmt.Errorf("unsupported HTTP method in CORS config %s", m) return errors.GetAPIErrorWithError(errors.ErrCORSUnsupportedMethod, fmt.Errorf("unsupported method is %s", m))
} }
} }
for _, h := range r.ExposeHeaders { for _, h := range r.ExposeHeaders {
if h == wildcard { if h == wildcard {
return fmt.Errorf("ExposeHeader \"*\" contains wildcard. We currently do not support wildcard " + return errors.GetAPIError(errors.ErrCORSWildcardExposeHeaders)
"for ExposeHeader")
} }
} }
} }