2025-02-12 22:53:18 +05:30
|
|
|
package errors
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"regexp"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-18 13:06:31 +05:30
|
|
|
var (
|
2025-04-27 16:38:34 +05:30
|
|
|
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"}
|
|
|
|
|
CodeForbidden = Code{"forbidden"}
|
2025-06-06 00:48:44 +05:30
|
|
|
CodeCanceled = Code{"canceled"}
|
|
|
|
|
CodeTimeout = Code{"timeout"}
|
2025-02-18 13:06:31 +05:30
|
|
|
)
|
|
|
|
|
|
2025-02-12 22:53:18 +05:30
|
|
|
var (
|
|
|
|
|
codeRegex = regexp.MustCompile(`^[a-z_]+$`)
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-27 16:38:34 +05:30
|
|
|
type Code struct{ s string }
|
2025-02-12 22:53:18 +05:30
|
|
|
|
2025-04-27 16:38:34 +05:30
|
|
|
func NewCode(s string) (Code, error) {
|
2025-02-12 22:53:18 +05:30
|
|
|
if !codeRegex.MatchString(s) {
|
2025-04-27 16:38:34 +05:30
|
|
|
return Code{}, fmt.Errorf("invalid code: %v", s)
|
2025-02-12 22:53:18 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-27 16:38:34 +05:30
|
|
|
return Code{s: s}, nil
|
2025-02-12 22:53:18 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-27 16:38:34 +05:30
|
|
|
func MustNewCode(s string) Code {
|
2025-02-12 22:53:18 +05:30
|
|
|
code, err := NewCode(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 16:38:34 +05:30
|
|
|
func (c Code) String() string {
|
2025-02-12 22:53:18 +05:30
|
|
|
return c.s
|
|
|
|
|
}
|