fix: check if Error is null in json response
This commit is contained in:
parent
8e6c578dbd
commit
03c5908605
@ -12,12 +12,18 @@ func HandleError(c *gin.Context) func(error) any {
|
|||||||
return func(err error) any {
|
return func(err error) any {
|
||||||
// Check if the error is of type *AppError
|
// Check if the error is of type *AppError
|
||||||
if appErr, ok := err.(*app_error.AppError); ok {
|
if appErr, ok := err.(*app_error.AppError); ok {
|
||||||
// Use the AppError fields for the JSON response
|
response := gin.H{
|
||||||
c.JSON(appErr.Status, gin.H{
|
|
||||||
"code": appErr.Code,
|
"code": appErr.Code,
|
||||||
"message": appErr.Message,
|
"message": appErr.Message,
|
||||||
"details": appErr.Err.Error(), // Original error if available
|
}
|
||||||
})
|
|
||||||
|
// Add details if available
|
||||||
|
if appErr.Err != nil {
|
||||||
|
response["details"] = appErr.Err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the AppError fields for the JSON response
|
||||||
|
c.JSON(appErr.Status, response)
|
||||||
} else {
|
} else {
|
||||||
// Fallback for non-AppError errors
|
// Fallback for non-AppError errors
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user