MCPcopy Create free account
hub / github.com/caarlos0/env / ExampleParse_errorHandling

Function ExampleParse_errorHandling

example_test.go:420–457  ·  view source on GitHub ↗

TODO: envSeperator

()

Source from the content-addressed store, hash-verified

418//
419
420func ExampleParse_errorHandling() {
421 type Config struct {
422 Username string `env:"EX_ERR_USERNAME" envDefault:"admin"`
423 Password string `env:"EX_ERR_PASSWORD,notEmpty"`
424 }
425
426 var cfg Config
427 if err := Parse(&cfg); err != nil {
428 if errors.Is(err, EmptyVarError{}) {
429 fmt.Println("oopsie")
430 }
431 aggErr := AggregateError{}
432 if ok := errors.As(err, &aggErr); ok {
433 for _, er := range aggErr.Errors {
434 switch v := er.(type) {
435 // Handle the error types you need:
436 // ParseError
437 // NotStructPtrError
438 // NoParserError
439 // NoSupportedTagOptionError
440 // EnvVarIsNotSetError
441 // EmptyEnvVarError
442 // LoadFileContentError
443 // ParseValueError
444 case EmptyVarError:
445 fmt.Println("daisy")
446 default:
447 fmt.Printf("Unknown error type %v", v)
448 }
449 }
450 }
451 }
452
453 fmt.Printf("%+v", cfg)
454 // Output: oopsie
455 // daisy
456 // {Username:admin Password:}
457}
458
459// You can avoid setting defaults for non zero values
460// This could be useful for loading data from config file first

Callers

nothing calls this directly

Calls 2

ParseFunction · 0.85
IsMethod · 0.80

Tested by

no test coverage detected