You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
5.4 KiB
123 lines
5.4 KiB
/* ——————————————————————————————————————————————————————————————————————————
|
|
Proyecto goat
|
|
—————————————————————————————————————————————————————————————————————————————
|
|
Fichero module.go
|
|
Package errs
|
|
Autor Juan V. Navarro juanvnl@activething.com
|
|
Creado 03/02/2026
|
|
—————————————————————————————————————————————————————————————————————————————
|
|
|
|
LICENSES AND TERMS OF USE
|
|
-------------------------
|
|
|
|
This software is licensed under the Elastic License v2.0 (the "License").
|
|
For full terms and additional information regarding permitted and prohibited
|
|
uses, please visit:
|
|
https://activething.com/ATGO/licenses
|
|
|
|
You may use, copy, modify, and redistribute this software internally within
|
|
your organization for any purpose, including research, development, and
|
|
testing, subject to the terms of this License.
|
|
|
|
You may NOT, however, use, provide, distribute, or make this software
|
|
available to any third party as part of a hosted service, SaaS offering, or
|
|
commercial product without first obtaining a commercial license from
|
|
Active Thing.
|
|
|
|
You may combine this software with other code, provided that such
|
|
combination does not circumvent the restrictions of this License.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. In no event shall
|
|
the authors or copyright holders be liable for any claim, damages, or other
|
|
liability arising from the use of this software.
|
|
|
|
—————————————————————————————————————————————————————————————————————————————
|
|
|
|
Web : activething.com | activething.com/goat
|
|
git : g.activething.com | github.com/activething/goat
|
|
Correo : dev@activething.com
|
|
|
|
—————————————————————————————————————————————————————————————————————————————
|
|
No deseo caminar sobre el agua", dijo Siddhartha.
|
|
Que los antiguos chamanes se contenten con tales habilidades.
|
|
—— Hermann Hesse, Siddhartha
|
|
—————————————————————————————————————————————————————————————————————————————
|
|
Copyright (c) 2026 Active Thing
|
|
————————————————————————————————————————————————————————————————————————————— */
|
|
|
|
package errs
|
|
|
|
const (
|
|
moduleSepChar = ':'
|
|
ModuleSep = "::"
|
|
lenModuleSep = 2
|
|
minModuleSeg = 1
|
|
)
|
|
|
|
// Module representa un espacio de nombres (namespace) raíz para una familia de errores.
|
|
//
|
|
// El módulo es la unidad fundamental de agrupación. Todos los errores generados
|
|
// desde un módulo compartirán el mismo prefijo, garantizando unicidad en sistemas grandes.
|
|
//
|
|
// Formato:
|
|
// - Debe ser un identificador simple ("auth", "payment_gateway").
|
|
// - NO debe contener el separador "::".
|
|
// - Solo caracteres: [a-z], [0-9], ['_'].
|
|
type Module string
|
|
|
|
// MustModule crea y valida un nuevo módulo raíz.
|
|
//
|
|
// Utilice esta función durante la inicialización de paquetes para definir
|
|
// el origen de sus errores.
|
|
//
|
|
// Panics:
|
|
//
|
|
// Lanza un panic si el nombre contiene caracteres inválidos (ej. mayúsculas, puntos)
|
|
// o si incluye el separador "::".
|
|
func MustModule(name string) Module {
|
|
if !isValidSegment(name, minModuleSeg) {
|
|
panic(ErrInvalidModule)
|
|
}
|
|
return Module(name)
|
|
}
|
|
|
|
func (m Module) String() string {
|
|
return string(m)
|
|
}
|
|
|
|
// MustCode genera un código de error completo bajo este módulo.
|
|
// Combina el módulo con un código específico para crear un identificador único global.
|
|
//
|
|
// Es la forma recomendada de crear errores base. Para añadir niveles de
|
|
// jerarquía adicionales, utilice el método .Sub() sobre el Code resultante.
|
|
//
|
|
// Ejemplo:
|
|
//
|
|
// m := MustModule("auth")
|
|
// code := m.MustCode("user_blocked") // -> "auth::user_blocked"
|
|
//
|
|
// Panics:
|
|
//
|
|
// Lanza panic si el código proporcionado ("user_blocked") no es un segmento válido.
|
|
func (m Module) MustCode(code string) Code {
|
|
// 1. Verificación de seguridad: ¿El módulo sobre el que se llama es válido?
|
|
if er := ValidateModule(string(m)); er != nil {
|
|
panic(er)
|
|
}
|
|
if !isValidSegment(code, minCodeSeg) {
|
|
panic(ErrInvalidCode)
|
|
}
|
|
return Code(string(m) + ModuleSep + code)
|
|
}
|
|
|
|
// ValidateModule verifica si un string cumple los requisitos para ser un nombre de módulo.
|
|
// A diferencia de MustModule, devuelve un error en lugar de entrar en pánico.
|
|
func ValidateModule(module string) error {
|
|
if !isValidSegment(module, minModuleSeg) {
|
|
return ErrInvalidModule
|
|
}
|
|
return nil
|
|
}
|