help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-rest-api
/
internal
/
response
RSK World
go-rest-api
Go REST API - Enterprise-grade REST API with JWT Authentication + PostgreSQL + Redis Caching + Docker + Comprehensive Testing + Educational Design
response
  • response.go1.9 KB
response.go
internal/response/response.go
Raw Download
Find: Go to:
/*
* Author: RSK World
* Email: help@rskworld.in / support@rskworld.in
* Website: https://rskworld.in/contact.php
* Year: 2026
*/

package response

import (
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Status  string      `json:"status"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
	Error   *ErrorDetail `json:"error,omitempty"`
}

type ErrorDetail struct {
	Code    string `json:"code,omitempty"`
	Details string `json:"details,omitempty"`
}

func Success(c *gin.Context, code int, message string, data interface{}) {
	c.JSON(code, Response{
		Status:  "success",
		Message: message,
		Data:    data,
	})
}

func Error(c *gin.Context, code int, message string) {
	ErrorWithDetails(c, code, message, "", "")
}

func ErrorWithDetails(c *gin.Context, code int, message, errorCode, details string) {
	response := Response{
		Status:  "error",
		Message: message,
	}

	if errorCode != "" || details != "" {
		response.Error = &ErrorDetail{
			Code:    errorCode,
			Details: details,
		}
	}

	// Log server errors
	if code >= 500 {
		log.Printf("Server Error [%d]: %s - %s", code, message, details)
	}

	c.JSON(code, response)
}

func ValidationError(c *gin.Context, message string) {
	ErrorWithDetails(c, http.StatusBadRequest, message, "VALIDATION_ERROR", "")
}

func NotFound(c *gin.Context, resource string) {
	ErrorWithDetails(c, http.StatusNotFound, resource+" not found", "NOT_FOUND", "")
}

func Unauthorized(c *gin.Context, message string) {
	ErrorWithDetails(c, http.StatusUnauthorized, message, "UNAUTHORIZED", "")
}

func Forbidden(c *gin.Context, message string) {
	ErrorWithDetails(c, http.StatusForbidden, message, "FORBIDDEN", "")
}

func InternalServerError(c *gin.Context, message string) {
	ErrorWithDetails(c, http.StatusInternalServerError, message, "INTERNAL_SERVER_ERROR", "")
}
81 lines•1.9 KB
go

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer