Debian Codegraph @

vcf.go

raw
1 package vcfgo
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 // VCFError satisfies the error interface and allows multiple errors.
9 // This is useful because, for example, on a single line, every sample may have
10 // a field that doesn't match the description in the header. We want to keep parsing
11 // but also let the caller know about the error.
12 type VCFError struct {
13 Msgs []string
14 Lines []int64
15 }
16
17 // Error returns a string with all errors delimited by newlines.
18 func (e *VCFError) Error() string {
19 var msgs []string
20 seen := make(map[string]struct{})
21 for i, m := range e.Msgs {
22 // remove duplicates
23 if _, ok := seen[m]; !ok {
24 seen[m] = struct{}{}
25 msgs = append(msgs, fmt.Sprintf("%s. [line: %d]", m, e.Lines[i]))
26 }
27 }
28 return strings.Join(msgs, "\n")
29 }
30
31 // NewVCFError allocates the needed ingredients.
32 func NewVCFError() *VCFError {
33 e := VCFError{Msgs: make([]string, 0), Lines: make([]int64, 0)}
34 return &e
35 }
36
37 // Add adds an error and the line number within the vcf where the error took place.
38 func (e *VCFError) Add(err error, line int64) {
39 if err != nil {
40 if ierr := err.Error(); ierr != "" {
41 if len(e.Msgs) == 5000 {
42 // only keep at most 5K errors.
43 m := make([]string, 0, 5000)
44 l := make([]int64, 0, 5000)
45 m = append(m, e.Msgs[3000:]...)
46 l = append(l, e.Lines[3000:]...)
47 e.Msgs = m
48 e.Lines = l
49 }
50 e.Msgs = append(e.Msgs, ierr)
51 e.Lines = append(e.Lines, line)
52 }
53 }
54 }
55
56 // IsEmpty returns true if there no errors stored.
57 func (e *VCFError) IsEmpty() bool {
58 return len(e.Msgs) == 0
59 }
60
61 // Clear empties the Messages
62 func (e *VCFError) Clear() {
63 e.Msgs = e.Msgs[:0]
64 e.Lines = e.Lines[:0]
65 }