penambahan All case Satu sehat

This commit is contained in:
meninjar
2026-05-04 03:48:43 +00:00
parent 135c631021
commit af32d9cfdd
127 changed files with 617698 additions and 1168 deletions
+75 -9
View File
@@ -1,6 +1,10 @@
package response
import (
"fmt"
"net/http"
"service/pkg/errors"
"github.com/gin-gonic/gin"
)
@@ -13,6 +17,26 @@ type Response struct {
Meta interface{} `json:"meta,omitempty"`
}
// getErrorCodeAndCategory memetakan HTTP Status Code ke Error Code terpusat
func getErrorCodeAndCategory(statusCode int) (string, string) {
switch statusCode {
case http.StatusBadRequest:
return errors.ErrCodeInvalidInput, errors.CategoryValidation
case http.StatusUnauthorized:
return errors.ErrCodeUnauthorized, errors.CategoryUnauthorized
case http.StatusForbidden:
return errors.ErrCodeForbidden, errors.CategoryForbidden
case http.StatusNotFound:
return errors.ErrCodeNotFound, errors.CategoryNotFound
case http.StatusConflict:
return errors.ErrCodeConflict, errors.CategoryConflict
case http.StatusTooManyRequests:
return errors.ErrCodeRateLimitExceeded, errors.CategoryRateLimit
default:
return errors.ErrCodeInternalError, errors.CategoryInternal
}
}
// Success sends a success response
func Success(c *gin.Context, statusCode int, message string, data interface{}) {
c.JSON(statusCode, Response{
@@ -24,16 +48,35 @@ func Success(c *gin.Context, statusCode int, message string, data interface{}) {
// Error sends an error response
func Error(c *gin.Context, statusCode int, message string, err interface{}) {
// Handle jika tipe yang dikirim adalah native error Go agar tidak menjadi objek kosong '{}' di JSON
if e, ok := err.(error); ok {
err = e.Error()
var e error
if err != nil {
if errVal, ok := err.(error); ok {
e = errVal
} else {
e = fmt.Errorf("%v", err)
}
} else {
e = fmt.Errorf("%s", message)
}
c.JSON(statusCode, Response{
Status: "error",
Message: message,
Error: err,
})
// Jika error sudah berupa AppError dari pkg/errors, cetak langsung formatnya
if errors.IsAppError(e) {
errors.HandleHTTPError(c, e.(errors.Error))
return
}
// Jika berupa error standard/native biasa, konversi menjadi format terpusat AppError
code, category := getErrorCodeAndCategory(statusCode)
wrappedErr := errors.NewBuilder().
Code(code).
Category(category).
HTTPStatus(statusCode).
Message(message).
Cause(e).
Metadata("reason", e.Error()).
Build()
errors.HandleHTTPError(c, wrappedErr)
}
// ErrorWithLog mengirimkan respons error HTTP sekaligus menyisipkan error asli
@@ -41,8 +84,31 @@ func Error(c *gin.Context, statusCode int, message string, err interface{}) {
func ErrorWithLog(c *gin.Context, err error, statusCode int, message string, details interface{}) {
if err != nil {
c.Error(err) // Meneruskan error asli ke Gin Context untuk dicatat logger
} else {
err = fmt.Errorf("%s", message)
}
Error(c, statusCode, message, details) // Panggil fungsi Error() standar
// Sama seperti fungsi Error(), kita teruskan ke format sentral
if errors.IsAppError(err) {
errors.HandleHTTPError(c, err.(errors.Error))
return
}
code, category := getErrorCodeAndCategory(statusCode)
builder := errors.NewBuilder().
Code(code).
Category(category).
HTTPStatus(statusCode).
Message(message).
Cause(err).
Metadata("reason", err.Error())
// Sisipkan details tambahan ke dalam metadata error builder
if details != nil {
builder.Metadata("additional_details", details)
}
errors.HandleHTTPError(c, builder.Build())
}
// Meta contains pagination metadata