Think of your backend as layers with different responsibilities:
HTTP request
↓
HANDLER → HTTP things (JSON, status codes)
↓
SERVICE → business meaning (rules, validations)
↓
REPOSITORY →database talking (SQL)
↓
DATABASE
Now the golden rule :
❗ Errors move UPWARD
❗ Meaning is added as they move up
POST /websites
{
"name": "Google",
"url": "<https://google.com>"
}
func(h *WebsiteHandler) CreateWebsite(c *gin.Context) {
var req dto.CreateWebsiteRequest
if err := c.ShouldBindJSON(&req); err !=nil {
So the handler immediately responds:
c.JSON(http.StatusBadRequest, response.APIResponse{
Success:false,
Error:"invalid request body",
})
return