2026-03-03 17:08:58 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ErrorWithStatus struct {
|
|
|
|
|
Message string
|
|
|
|
|
Status int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *ErrorWithStatus) Error() string {
|
|
|
|
|
return e.Message
|
|
|
|
|
}
|
2026-04-01 22:01:31 +00:00
|
|
|
func NewBadRequest(mesg_format string, args ...any) *ErrorWithStatus {
|
|
|
|
|
return NewErrorStatus(http.StatusBadRequest, mesg_format, args...)
|
|
|
|
|
}
|
2026-03-03 17:08:58 +00:00
|
|
|
func NewError(mesg_format string, args ...any) *ErrorWithStatus {
|
|
|
|
|
return NewErrorStatus(http.StatusInternalServerError, mesg_format, args...)
|
|
|
|
|
}
|
|
|
|
|
func NewErrorMaybe(mesg_format string, err error, args ...any) *ErrorWithStatus {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
allArgs := append([]any{err}, args...)
|
|
|
|
|
return NewErrorStatus(http.StatusInternalServerError, mesg_format, allArgs...)
|
|
|
|
|
}
|
|
|
|
|
func NewErrorStatus(status int, mesg_format string, args ...any) *ErrorWithStatus {
|
|
|
|
|
w := fmt.Errorf(mesg_format, args...)
|
|
|
|
|
return &ErrorWithStatus{
|
|
|
|
|
Message: w.Error(),
|
|
|
|
|
Status: status,
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-02 14:22:45 +00:00
|
|
|
func NewForbidden(mesg_format string, args ...any) *ErrorWithStatus {
|
|
|
|
|
return NewErrorStatus(http.StatusForbidden, mesg_format, args...)
|
|
|
|
|
}
|
2026-04-23 15:24:06 +00:00
|
|
|
func NewUnauthorized(mesg_format string, args ...any) *ErrorWithStatus {
|
|
|
|
|
return NewErrorStatus(http.StatusUnauthorized, mesg_format, args...)
|
|
|
|
|
}
|