tricrypt/errors/env.go

37 lines
796 B
Go
Raw Normal View History

2024-02-23 18:02:43 +01:00
package errors
2024-03-13 23:04:58 +01:00
import (
"errors"
"fmt"
"reflect"
)
2024-02-23 18:02:43 +01:00
var (
2024-03-13 23:04:58 +01:00
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")
2024-03-21 16:23:16 +01:00
ALREADY_SET = errors.New("Already set")
2024-04-28 22:36:26 +02:00
NOT_FOUND = errors.New("Was not found")
2024-02-23 18:02:43 +01:00
)
2024-03-13 23:04:58 +01:00
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
}