2023-12-03 03:47:23 +08:00
|
|
|
package httputil
|
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
import (
|
2024-06-03 01:41:18 +08:00
|
|
|
"github.com/divyam234/teldrive/internal/logging"
|
2024-02-12 04:52:41 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
2023-12-03 03:47:23 +08:00
|
|
|
|
|
|
|
func NewError(ctx *gin.Context, status int, err error) {
|
2024-02-12 04:52:41 +08:00
|
|
|
logger := logging.FromContext(ctx)
|
|
|
|
logger.Error(err)
|
|
|
|
if status == 0 {
|
|
|
|
status = 500
|
|
|
|
}
|
|
|
|
ctx.JSON(status, HTTPError{
|
2023-12-03 03:47:23 +08:00
|
|
|
Code: status,
|
|
|
|
Message: err.Error(),
|
2024-02-12 04:52:41 +08:00
|
|
|
})
|
2023-12-03 03:47:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPError struct {
|
2023-12-03 14:52:25 +08:00
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
2023-12-03 03:47:23 +08:00
|
|
|
}
|