You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
741 B
Go
43 lines
741 B
Go
package httpdata
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
type UniverseError struct {
|
|
Code Code
|
|
Message string
|
|
}
|
|
|
|
func NewUniverseError(code Code, message string) error {
|
|
return UniverseError{
|
|
Code: code,
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
func NewBadRequestError(message string) error {
|
|
return UniverseError{
|
|
Code: CodeBadRequest,
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
func (err UniverseError) Error() string {
|
|
if err.Message == "" {
|
|
return string(err.Code)
|
|
}
|
|
return string(err.Code) + ": " + err.Message
|
|
}
|
|
|
|
func IsUniverseError(err error) bool {
|
|
return errors.As(err, &UniverseError{})
|
|
}
|
|
|
|
func ToUniverseError(err error, code Code, message string) error {
|
|
if err == nil || IsUniverseError(err) {
|
|
return err
|
|
}
|
|
return NewUniverseError(code, message)
|
|
}
|