Changes in errors

This commit is contained in:
qowevisa 2024-03-14 00:04:58 +02:00
parent a15defc397
commit 4be3db88e6

View File

@ -1,7 +1,34 @@
package errors
import "errors"
import (
"errors"
"fmt"
"reflect"
)
var (
ENV_EMPTY = errors.New("Environment variable was empty")
ENV_EMPTY = errors.New("Environment variable was empty")
NOT_INIT = errors.New("Is not initialised")
NOT_HANDLED = errors.New("Is not handled")
OUT_OF_BOUND = errors.New("Out of bound")
NOT_SET = errors.New("Is not set")
)
func WrapErr(context string, err error) error {
return fmt.Errorf("%s: %w", context, err)
}
func CheckFieldInitialized(t interface{}, fieldName string) error {
v := reflect.ValueOf(t)
fieldValue := v.Elem().FieldByName(fieldName)
if !fieldValue.IsValid() {
return fmt.Errorf("field %s does not exist", fieldName)
}
if fieldValue.IsNil() {
return WrapErr(fieldName, NOT_INIT)
}
return nil
}