error handling

This commit is contained in:
Timon Ringwald 2022-09-18 19:39:45 +02:00
parent b730aca6cf
commit 858421789f
6 changed files with 24 additions and 7 deletions

9
errors.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "git.milar.in/milarin/adverr"
var (
ErrInvalidTemplateSyntax = adverr.NewErrTmpl("ErrInvalidTemplateSyntax", "parsing of template failed: %s")
ErrTemplateExecutionFailed = adverr.NewErrTmpl("ErrTemplateExecutionFailed", "execution of template failed: %s")
ErrDirectoryCreationFailed = adverr.NewErrTmpl("ErrDirectoryCreationFailed", "creation of target directory failed: %s")
)

5
go.mod
View File

@ -2,4 +2,7 @@ module git.milar.in/milarin/cfgen
go 1.19 go 1.19
require gopkg.in/yaml.v2 v2.4.0 require (
git.milar.in/milarin/adverr v1.1.0
gopkg.in/yaml.v2 v2.4.0
)

2
go.sum
View File

@ -1,3 +1,5 @@
git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0=
git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

View File

@ -13,7 +13,7 @@ import (
func HandleTemplate(tmpl *template.Template) error { func HandleTemplate(tmpl *template.Template) error {
b := &bytes.Buffer{} b := &bytes.Buffer{}
if err := tmpl.Execute(b, ConfigFile.Globals); err != nil { if err := tmpl.Execute(b, ConfigFile.Globals); err != nil {
return err return ErrTemplateExecutionFailed.Wrap(err, tmpl.Name())
} }
if len(strings.TrimSpace(b.String())) == 0 { if len(strings.TrimSpace(b.String())) == 0 {

11
main.go
View File

@ -3,9 +3,11 @@ package main
import ( import (
"errors" "errors"
"flag" "flag"
"fmt"
"os" "os"
"text/template" "text/template"
"git.milar.in/milarin/adverr"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -17,21 +19,22 @@ func main() {
flag.Parse() flag.Parse()
if err := ReadConfigFile(); err != nil { if err := ReadConfigFile(); err != nil {
panic(err) fmt.Fprintln(os.Stderr, "no config file provided")
os.Exit(1)
} }
tmplMap, err := ParseTemplates(template.New("template"), ConfigFile.TemplateDir) tmplMap, err := ParseTemplates(template.New("template"), ConfigFile.TemplateDir)
if err != nil { if err != nil {
panic(err) adverr.Fatalln(err, 2)
} }
if err := os.MkdirAll(ConfigFile.TargetDir, 0755); err != nil { if err := os.MkdirAll(ConfigFile.TargetDir, 0755); err != nil {
panic(err) adverr.Fatalln(ErrDirectoryCreationFailed.Wrap(err, ConfigFile.TargetDir), 3)
} }
for _, tmpl := range tmplMap { for _, tmpl := range tmplMap {
if err := HandleTemplate(tmpl); err != nil { if err := HandleTemplate(tmpl); err != nil {
panic(err) adverr.Fatalln(err, 4)
} }
} }
} }

View File

@ -48,7 +48,7 @@ func parseTemplates(tmpl *template.Template, tmplMap map[string]*template.Templa
tmpl, err = tmpl.New(tmplName).Parse(string(data)) tmpl, err = tmpl.New(tmplName).Parse(string(data))
if err != nil { if err != nil {
return err return ErrInvalidTemplateSyntax.Wrap(err, tmplName)
} }
tmplMap[tmplName] = tmpl tmplMap[tmplName] = tmpl