teldrive/pkg/httputil/error.go

24 lines
422 B
Go
Raw Permalink Normal View History

2023-12-03 03:47:23 +08:00
package httputil
import (
"github.com/gin-gonic/gin"
2024-08-31 23:50:17 +08:00
"github.com/tgdrive/teldrive/internal/logging"
)
2023-12-03 03:47:23 +08:00
func NewError(ctx *gin.Context, status int, err error) {
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(),
})
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
}