This commit is contained in:
2025-12-04 10:55:25 +08:00
commit 60992c1e44
19 changed files with 1058 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
package helper
const (
ContentTypeHeader = "Content-Type"
ApplicationJSON = "application/json"
ErrorLabel = "error"
MessageLabel = "message"
ErrorEncodingResponse = "Error encoding response"
ErrorFailedtoLogLoginEvent = "Failed to log login event"
WarningLabel = "WARNING:"
)
+89
View File
@@ -0,0 +1,89 @@
package helper
import (
"log"
"os"
"github.com/getsentry/sentry-go"
)
// LogInfo logs an info message to both the local log and Sentry based on the environment.
func LogInfo(message string) {
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
log.Fatal("GO_ENV is not set in error_logging LogInfo. Please set the GO_ENV environment variable.")
}
if goEnv == "development" || goEnv == "debug" {
log.Println("INFO:", message)
}
if goEnv == "production" || goEnv == "canary" {
log.Println("INFO:", message)
}
}
// LogWarn logs a warning message to both the local log and Sentry based on the environment.
func LogWarn(message string) {
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
log.Fatal("GO_ENV is not set in error_logging LogWarn. Please set the GO_ENV environment variable.")
}
switch goEnv {
case "production", "canary":
sentry.CaptureMessage("WARNING: " + message)
case "development", "debug":
log.Println("WARNING:", message)
}
}
// LogError logs an error message to both the local log and Sentry based on the environment.
func LogError(err error, message string) {
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
log.Fatal("GO_ENV is not set in error_logging LogError. Please set the GO_ENV environment variable.")
}
switch goEnv {
case "production", "canary":
if err != nil {
sentry.CaptureException(err)
} else {
sentry.CaptureMessage("ERROR: " + message)
}
log.Printf("ERROR: %s: %v", message, err)
case "development", "debug":
if err != nil {
log.Printf("ERROR: %s: %v", message, err)
} else {
log.Println("ERROR:", message)
}
}
}
// LogFatal logs a fatal error message to both the local log and Sentry based on the environment and then exits the application.
func LogFatal(err error, message string) {
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
log.Fatal("GO_ENV is not set in error_logging LogFatal. Please set the GO_ENV environment variable.")
}
switch goEnv {
case "production", "canary":
if err != nil {
sentry.CaptureException(err)
} else {
sentry.CaptureMessage("FATAL: " + message)
}
log.Fatalf("FATAL: %s: %v", message, err)
case "development", "debug":
if err != nil {
log.Fatalf("FATAL: %s: %v", message, err)
} else {
log.Fatalf("FATAL: %s", message)
}
}
}
+28
View File
@@ -0,0 +1,28 @@
package helper
import (
"encoding/json"
"net/http"
)
func RespondWithError(w http.ResponseWriter, statusCode int, message string) {
w.Header().Set(ContentTypeHeader, ApplicationJSON)
w.WriteHeader(statusCode)
if encodeErr := json.NewEncoder(w).Encode(map[string]string{ErrorLabel: message}); encodeErr != nil {
LogError(encodeErr, ErrorEncodingResponse)
}
}
func RespondWithMessage(w http.ResponseWriter, message string) {
if encodeErr := json.NewEncoder(w).Encode(map[string]string{MessageLabel: message}); encodeErr != nil {
LogError(encodeErr, ErrorEncodingResponse)
}
}
func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set(ContentTypeHeader, ApplicationJSON)
w.WriteHeader(statusCode)
if encodeErr := json.NewEncoder(w).Encode(data); encodeErr != nil {
LogError(encodeErr, ErrorEncodingResponse)
}
}