-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
60 lines (53 loc) · 1.28 KB
/
Copy pathhandler.go
File metadata and controls
60 lines (53 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package apperror
import (
"errors"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
)
func HandleHTTPError(c *gin.Context, err error) {
debugMode := os.Getenv("GIN_MODE") == "debug"
var initErr *InitError
if errors.As(err, &initErr) {
// Logging internal
if debugMode {
log.Printf("[ERROR] CODE=%s | MESSAGE=%s | DETAIL=%+v", initErr.Code, initErr.Message, initErr.Err)
}
// Cari mapping kode error
for _, mapping := range defaultErrorMap {
if mapping.Code == initErr.Code {
c.JSON(mapping.Status, gin.H{
"code": mapping.Status,
"status": "error",
"message": mapping.UserMessage,
})
return
}
}
responseStatus := initErr.ResponseStatus
if responseStatus == "" {
responseStatus = "error"
}
// Tidak ada mapping → gunakan custom status atau fallback 500
status := initErr.HTTPStatus
if status == 0 {
status = http.StatusInternalServerError
}
c.JSON(status, gin.H{
"code": status,
"status": responseStatus,
"message": initErr.Message,
})
return
}
// Bukan InitError → fallback umum
if debugMode {
log.Printf("[UNHANDLED ERROR]: %+v", err)
}
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"status": "error",
"message": "Terjadi kesalahan server",
})
}