Skip to content
Snippets Groups Projects
Commit 9e3825e9 authored by Johan de Klerk's avatar Johan de Klerk
Browse files

Added GenericJSONResponseWithMessage

parent 4fa38540
No related branches found
No related tags found
No related merge requests found
......@@ -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),
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment