trepa/internal/core/errors.go
2025-02-20 12:09:38 +00:00

38 lines
688 B
Go

package core
import (
"net/http"
)
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)
}