Files
vppn/hub/errs/types.go
2026-06-14 09:04:41 +02:00

62 lines
1.7 KiB
Go

package errs
import (
"fmt"
"net/http"
)
type Error struct {
Code int
Msg string
}
func (e *Error) Error() string {
return fmt.Sprintf("[%d] %s", e.Code, e.Msg)
}
var (
ErrNotAuthorized = NotAuthorized.WithMsg("Not authorized.")
ErrInvalidPassword = BadRequest.WithMsg("Invalid password.")
ErrPasswordMismatch = BadRequest.WithMsg("Passwords don't match.")
ErrUnexpected = Internal.WithMsg("Unexpected internal error.")
ErrNotFound = NotFound.WithMsg("Not found.")
ErrAlreadyExists = Conflict.WithMsg("Already exists.")
// Validation errors.
ErrInvalidIP = BadRequest.WithMsg("Invalid IP.")
ErrInvalidPeerIP = BadRequest.WithMsg("Invalid peer IP.")
ErrNonPrivateIP = BadRequest.WithMsg("Non-private IP.")
ErrInvalidPort = BadRequest.WithMsg("Invalid port.")
ErrInvalidNetName = BadRequest.WithMsg("Invalid network name.")
ErrNetNameNotLocal = BadRequest.WithMsg("Network name must end with .local.")
ErrInvalidPeerName = BadRequest.WithMsg("Invalid peer name.")
ErrConstraint = BadRequest.WithMsg("Constraint error.")
)
// ----------------------------------------------------------------------------
type Type struct {
Code int
Msg string
}
func (t Type) WithErr(err error) *Error {
return &Error{Code: t.Code, Msg: err.Error()}
}
func (t Type) WithMsg(msg string) *Error {
return &Error{Code: t.Code, Msg: msg}
}
func (t Type) WithMsgf(msg string, args ...any) *Error {
return &Error{Code: t.Code, Msg: fmt.Sprintf(msg, args...)}
}
var (
Internal = Type{Code: http.StatusInternalServerError}
NotAuthorized = Type{Code: http.StatusUnauthorized}
NotFound = Type{Code: http.StatusNotFound}
BadRequest = Type{Code: http.StatusBadRequest}
Conflict = Type{Code: http.StatusConflict}
)