trepa/internal/core/errors.go
2025-02-26 19:11:47 +00:00

42 lines
776 B
Go

package core
import (
"net/http"
)
var (
ErrInvalidToken = NewHTTPError(http.StatusUnauthorized, "Invalid token", nil)
)
type HTTPError struct {
Code int `json:"code"`
Message string `json:"message"`
Cause error `json:"-"`
Details string `json:"details,omitempty"`
}
func (e *HTTPError) Error() string {
return e.Message
}
func (e *HTTPError) Unwrap() error {
return e.Cause
}
func NewHTTPError(code int, message string, cause error) *HTTPError {
details := ""
if cause != nil {
details = cause.Error()
}
return &HTTPError{
Code: code,
Message: message,
Cause: cause,
Details: details,
}
}
func NewInternalServerError(cause error) *HTTPError {
return NewHTTPError(http.StatusInternalServerError, "Internal server error", cause)
}