Files
isp-gate-service/httperrors/errors.go
T
txix f3fb9cff75
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
2026-03-18 17:07:55: merge [main:1bd032e5aa9a3d83735ea3fb30aeab719d917b82];
2026-03-18 17:07:55 +03:00

42 lines
834 B
Go

package httperrors
import (
"net/http"
"github.com/txix-open/isp-kit/json"
)
type HttpError struct {
statusCode int
userMessage string
details []any
err error
}
func New(statusCode int, userMessage string, internalError error) *HttpError {
return &HttpError{
statusCode: statusCode,
userMessage: userMessage,
err: internalError,
}
}
func (e *HttpError) Error() string {
return e.err.Error()
}
func (e *HttpError) WriteError(w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(e.statusCode)
data := map[string]any{
"errorCode": http.StatusText(e.statusCode),
"errorMessage": e.userMessage,
"details": e.details,
}
return json.NewEncoder(w).Encode(data)
}
func (e *HttpError) WithDetails(details ...any) {
e.details = details
}