From 5b4b9df0317bd9821f604f523c75ed17041afcd9 Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Fri, 8 Oct 2021 16:58:21 +0300 Subject: [PATCH] [#217] Add errors for cors Signed-off-by: Angira Kekteeva --- api/errors/errors.go | 21 +++++++++++++++++++++ api/handler/cors.go | 5 ++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/errors/errors.go b/api/errors/errors.go index aeda1cacd..064a6dbc9 100644 --- a/api/errors/errors.go +++ b/api/errors/errors.go @@ -312,6 +312,10 @@ const ( ErrAdminAccountNotEligible ErrServiceAccountNotFound ErrPostPolicyConditionInvalidFormat + + //CORS configuration errors. + ErrCORSUnsupportedMethod + ErrCORSWildcardExposeHeaders ) // error code to Error structure, these fields carry respective @@ -1909,6 +1913,18 @@ var errorCodes = errorCodeMap{ Description: "Invalid according to Policy: Policy Condition failed", 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. } @@ -1945,6 +1961,11 @@ func GetAPIError(code ErrorCode) Error { 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. type ObjectError struct { Err error diff --git a/api/handler/cors.go b/api/handler/cors.go index de0b24af5..b0b21e01b 100644 --- a/api/handler/cors.go +++ b/api/handler/cors.go @@ -262,13 +262,12 @@ func checkCORS(cors *CORSConfiguration) error { for _, r := range cors.CORSRules { for _, m := range r.AllowedMethods { 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 { if h == wildcard { - return fmt.Errorf("ExposeHeader \"*\" contains wildcard. We currently do not support wildcard " + - "for ExposeHeader") + return errors.GetAPIError(errors.ErrCORSWildcardExposeHeaders) } } }