penambahan All case Satu sehat
This commit is contained in:
+39
-15
@@ -13,17 +13,19 @@ import (
|
||||
|
||||
// HTTPErrorResponse represents standardized HTTP error response
|
||||
type HTTPErrorResponse struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Details map[string]interface{} `json:"details,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Retryable bool `json:"retryable,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Details map[string]interface{} `json:"details"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
Retryable bool `json:"retryable"`
|
||||
StackTrace interface{} `json:"stack_trace"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
} `json:"error"`
|
||||
Meta struct {
|
||||
HTTPStatus int `json:"http_status"`
|
||||
Category string `json:"category"`
|
||||
HTTPStatus int `json:"http_status"`
|
||||
} `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
@@ -50,18 +52,30 @@ func HandleHTTPError(c *gin.Context, err error) {
|
||||
|
||||
// Create error response
|
||||
response := &HTTPErrorResponse{}
|
||||
response.Status = "error"
|
||||
response.Error.Code = appErr.Code()
|
||||
response.Error.Message = appErr.GetLocalizedMessage(getLanguageFromContext(c))
|
||||
response.Error.Details = appErr.Metadata()
|
||||
if response.Error.Details == nil {
|
||||
response.Error.Details = make(map[string]interface{})
|
||||
}
|
||||
response.Error.Message = appErr.GetLocalizedMessage(getLanguageFromContext(c))
|
||||
response.Error.RequestID = c.GetString("request_id")
|
||||
response.Error.Timestamp = appErr.Metadata()["timestamp"].(string)
|
||||
response.Error.Retryable = IsRetryable(appErr.Code())
|
||||
response.Meta.HTTPStatus = appErr.HTTPStatus()
|
||||
response.Error.StackTrace = nil
|
||||
if ts, ok := appErr.Metadata()["timestamp"].(string); ok {
|
||||
response.Error.Timestamp = ts
|
||||
}
|
||||
response.Meta.Category = appErr.Category()
|
||||
response.Meta.HTTPStatus = appErr.HTTPStatus()
|
||||
|
||||
// Log error
|
||||
LogHTTPError(c, appErr)
|
||||
|
||||
// Cegah print ganda jika controller sudah print
|
||||
if c.Writer.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
// Send response
|
||||
c.JSON(appErr.HTTPStatus(), response)
|
||||
}
|
||||
@@ -120,13 +134,16 @@ func ValidationErrorHandler(c *gin.Context, err error) {
|
||||
if appErr.Category() == CategoryValidation {
|
||||
if details, ok := appErr.Metadata()["validation_errors"]; ok {
|
||||
response := gin.H{
|
||||
"status": "error",
|
||||
"error": gin.H{
|
||||
"code": appErr.Code(),
|
||||
"message": appErr.GetLocalizedMessage(getLanguageFromContext(c)),
|
||||
"fields": details,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
if !c.Writer.Written() {
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -158,13 +175,20 @@ func WriteErrorResponse(w http.ResponseWriter, err error) {
|
||||
}
|
||||
|
||||
response := &HTTPErrorResponse{}
|
||||
response.Status = "error"
|
||||
response.Error.Code = appErr.Code()
|
||||
response.Error.Message = appErr.GetLocalizedMessage("en")
|
||||
response.Error.Details = appErr.Metadata()
|
||||
response.Error.Timestamp = appErr.Metadata()["timestamp"].(string)
|
||||
if response.Error.Details == nil {
|
||||
response.Error.Details = make(map[string]interface{})
|
||||
}
|
||||
response.Error.Message = appErr.GetLocalizedMessage("en")
|
||||
response.Error.Retryable = IsRetryable(appErr.Code())
|
||||
response.Meta.HTTPStatus = appErr.HTTPStatus()
|
||||
response.Error.StackTrace = nil
|
||||
if ts, ok := appErr.Metadata()["timestamp"].(string); ok {
|
||||
response.Error.Timestamp = ts
|
||||
}
|
||||
response.Meta.Category = appErr.Category()
|
||||
response.Meta.HTTPStatus = appErr.HTTPStatus()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(appErr.HTTPStatus())
|
||||
|
||||
Reference in New Issue
Block a user