Skip to content

Commit

Permalink
feat: error types (#240)
Browse files Browse the repository at this point in the history
* support error types

* update erorr description

* add tb.Helper

* add documentation

* merge

* yaml new line

* readme new line

* update error

* linting

* goimport fix

* linting fix

* try to revert readme

---------

Co-authored-by: Alexander Kutuev <[email protected]>
  • Loading branch information
akutuev and Alexander Kutuev authored Feb 7, 2023
1 parent 850c2d8 commit b135bbd
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 140 deletions.
75 changes: 19 additions & 56 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package env

import (
"encoding"
"errors"
"fmt"
"net/url"
"os"
"reflect"
Expand All @@ -14,10 +12,6 @@ import (

// nolint: gochecknoglobals
var (
// ErrNotAStructPtr is returned if you pass something that is not a pointer to a
// Struct to Parse.
ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct")

defaultBuiltInParsers = map[reflect.Kind]ParserFunc{
reflect.Bool: func(v string) (interface{}, error) {
return strconv.ParseBool(v)
Expand Down Expand Up @@ -79,14 +73,14 @@ func defaultTypeParsers() map[reflect.Type]ParserFunc {
reflect.TypeOf(url.URL{}): func(v string) (interface{}, error) {
u, err := url.Parse(v)
if err != nil {
return nil, fmt.Errorf("unable to parse URL: %v", err)
return nil, newParseValueError("unable to parse URL", err)
}
return *u, nil
},
reflect.TypeOf(time.Nanosecond): func(v string) (interface{}, error) {
s, err := time.ParseDuration(v)
if err != nil {
return nil, fmt.Errorf("unable to parse duration: %v", err)
return nil, newParseValueError("unable to parse duration", err)
}
return s, err
},
Expand Down Expand Up @@ -183,11 +177,11 @@ func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...

ptrRef := reflect.ValueOf(v)
if ptrRef.Kind() != reflect.Ptr {
return ErrNotAStructPtr
return newAggregateError(NotStructPtrError{})
}
ref := ptrRef.Elem()
if ref.Kind() != reflect.Struct {
return ErrNotAStructPtr
return newAggregateError(NotStructPtrError{})
}
parsers := defaultTypeParsers()
for k, v := range funcMap {
Expand All @@ -200,22 +194,22 @@ func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...
func doParse(ref reflect.Value, funcMap map[reflect.Type]ParserFunc, opts []Options) error {
refType := ref.Type()

var agrErr aggregateError
var agrErr AggregateError

for i := 0; i < refType.NumField(); i++ {
refField := ref.Field(i)
refTypeField := refType.Field(i)

if err := doParseField(refField, refTypeField, funcMap, opts); err != nil {
if val, ok := err.(aggregateError); ok {
agrErr.errors = append(agrErr.errors, val.errors...)
if val, ok := err.(AggregateError); ok {
agrErr.Errors = append(agrErr.Errors, val.Errors...)
} else {
agrErr.errors = append(agrErr.errors, err)
agrErr.Errors = append(agrErr.Errors, err)
}
}
}

if len(agrErr.errors) == 0 {
if len(agrErr.Errors) == 0 {
return nil
}

Expand All @@ -226,8 +220,12 @@ func doParseField(refField reflect.Value, refTypeField reflect.StructField, func
if !refField.CanSet() {
return nil
}
if reflect.Ptr == refField.Kind() && refField.Elem().Kind() == reflect.Struct {
return ParseWithFuncs(refField.Interface(), funcMap, optsWithPrefix(refTypeField, opts)...)
if reflect.Ptr == refField.Kind() && !refField.IsNil() {
if refField.Elem().Kind() == reflect.Struct {

This comment has been minimized.

Copy link
@caarlos0

caarlos0 Feb 7, 2023

Owner

this change actually seems unrelated, will revert it

return ParseWithFuncs(refField.Interface(), funcMap, optsWithPrefix(refTypeField, opts)...)
}

return ParseWithFuncs(refField.Interface(), funcMap, opts...)
}
if reflect.Struct == refField.Kind() && refField.CanAddr() && refField.Type().Name() == "" {
return ParseWithFuncs(refField.Addr().Interface(), funcMap, optsWithPrefix(refTypeField, opts)...)
Expand Down Expand Up @@ -272,7 +270,7 @@ func get(field reflect.StructField, opts []Options) (val string, err error) {
case "notEmpty":
notEmpty = true
default:
return "", fmt.Errorf("tag option %q not supported", tag)
return "", newNoSupportedTagOptionError(tag)
}
}
expand := strings.EqualFold(field.Tag.Get("envExpand"), "true")
Expand All @@ -288,18 +286,18 @@ func get(field reflect.StructField, opts []Options) (val string, err error) {
}

if required && !exists && len(ownKey) > 0 {
return "", fmt.Errorf(`required environment variable %q is not set`, key)
return "", newEnvVarIsNotSet(key)
}

if notEmpty && val == "" {
return "", fmt.Errorf("environment variable %q should not be empty", key)
return "", newEmptyEnvVarError(key)
}

if loadFile && val != "" {
filename := val
val, err = getFromFile(filename)
if err != nil {
return "", fmt.Errorf(`could not load content of file "%s" from variable %s: %v`, filename, key, err)
return "", newLoadFileContentError(filename, key, err)
}
}

Expand Down Expand Up @@ -461,26 +459,6 @@ func parseTextUnmarshalers(field reflect.Value, data []string, sf reflect.Struct
return nil
}

func newParseError(sf reflect.StructField, err error) error {
return parseError{
sf: sf,
err: err,
}
}

type parseError struct {
sf reflect.StructField
err error
}

func (e parseError) Error() string {
return fmt.Sprintf(`parse error on field "%s" of type "%s": %v`, e.sf.Name, e.sf.Type, e.err)
}

func newNoParserError(sf reflect.StructField) error {
return fmt.Errorf(`no parser found for field "%s" of type "%s"`, sf.Name, sf.Type)
}

func optsWithPrefix(field reflect.StructField, opts []Options) []Options {
subOpts := make([]Options, len(opts))
copy(subOpts, opts)
Expand All @@ -489,18 +467,3 @@ func optsWithPrefix(field reflect.StructField, opts []Options) []Options {
}
return subOpts
}

type aggregateError struct {
errors []error
}

func (e aggregateError) Error() string {
var sb strings.Builder
sb.WriteString("env:")

for _, err := range e.errors {
sb.WriteString(fmt.Sprintf(" %v;", err.Error()))
}

return strings.TrimRight(sb.String(), ";")
}
Loading

0 comments on commit b135bbd

Please sign in to comment.