File: errors.go

package info (click to toggle)
golang-github-go-git-gcfg 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 256 kB
  • sloc: makefile: 12
file content (50 lines) | stat: -rw-r--r-- 1,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package gcfg

import (
	"errors"
	"fmt"
)

var (
	ErrSyntaxWarning = errors.New("syntax warning")

	ErrMissingEscapeSequence = errors.New("missing escape sequence")
	ErrMissingEndQuote       = errors.New("missing end quote")
)

// FatalOnly filters the results of a Read*Into invocation and returns only
// fatal errors. That is, errors (warnings) indicating data for unknown
// sections / variables is ignored. Example invocation:
//
//	err := gcfg.FatalOnly(gcfg.ReadFileInto(&cfg, configFile))
//	if err != nil {
//	    ...
func FatalOnly(err error) error {
	for {
		if err == nil {
			return nil
		}
		err = errors.Unwrap(err)
		if !errors.Is(err, ErrSyntaxWarning) {
			return err
		}
	}
}

func newSyntaxWarning(sec, sub, variable string) error {
	msg := fmt.Sprintf("can't store data in section %q", sec)
	if sub != "" {
		msg += fmt.Sprintf(", subsection %q", sub)
	}
	if variable != "" {
		msg += fmt.Sprintf(", variable %q", variable)
	}
	return fmt.Errorf("%w: %s", ErrSyntaxWarning, msg)
}

func joinNonFatal(prev, cur error) (error, bool) {
	if !errors.Is(cur, ErrSyntaxWarning) {
		return cur, true
	}
	return errors.Join(prev, cur), false
}