diff --git a/api_responses/api_responses.go b/api_responses/api_responses.go index bb2810a05f33f3a09002c6116f5bf5e4d456d3a9..bd6d4deb858d79c65c59ca73c94defd3dcabaa4d 100644 --- a/api_responses/api_responses.go +++ b/api_responses/api_responses.go @@ -5,6 +5,7 @@ import ( "fmt" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/map_utils" + "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils" "net/http" "regexp" "strconv" @@ -266,3 +267,27 @@ func GenericJSONResponseWithContentAndHeaders(code int, content string, headers } return response } + +func GenericJSONResponseWithMessage(code int, err error) events.APIGatewayProxyResponse { + var message string + var body map[string]string + + if err != nil { + customErr := err.(*errors.CustomError) + message = customErr.Formatted(errors.FormattingOptions{NewLines: false, Causes: true}) + body = map[string]string{ + "message": string_utils.Capitalize(message), + } + } + + responseBody := message + if bodyBytes, err := json.Marshal(body); err == nil { + responseBody = string(bodyBytes) + } + + return events.APIGatewayProxyResponse{ + StatusCode: code, + Body: responseBody, + Headers: map_utils.MergeMaps(utils.CorsHeaders(), ContentTypeJSONHeader), + } +}