2025-02-12 22:53:18 +05:30
|
|
|
package errors
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"regexp"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-18 13:06:31 +05:30
|
|
|
var (
|
|
|
|
|
CodeInvalidInput code = code{"invalid_input"}
|
|
|
|
|
CodeInternal = code{"internal"}
|
|
|
|
|
CodeUnsupported = code{"unsupported"}
|
|
|
|
|
CodeNotFound = code{"not_found"}
|
|
|
|
|
CodeMethodNotAllowed = code{"method_not_allowed"}
|
|
|
|
|
CodeAlreadyExists = code{"already_exists"}
|
|
|
|
|
CodeUnauthenticated = code{"unauthenticated"}
|
2025-03-12 17:18:11 +05:30
|
|
|
CodeForbidden = code{"forbidden"}
|
2025-02-18 13:06:31 +05:30
|
|
|
)
|
|
|
|
|
|
2025-02-12 22:53:18 +05:30
|
|
|
var (
|
|
|
|
|
codeRegex = regexp.MustCompile(`^[a-z_]+$`)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type code struct{ s string }
|
|
|
|
|
|
|
|
|
|
func NewCode(s string) (code, error) {
|
|
|
|
|
if !codeRegex.MatchString(s) {
|
|
|
|
|
return code{}, fmt.Errorf("invalid code: %v", s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code{s: s}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MustNewCode(s string) code {
|
|
|
|
|
code, err := NewCode(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c code) String() string {
|
|
|
|
|
return c.s
|
|
|
|
|
}
|