Commit 6e4c079d authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'jv-workhorse-simple-errors' into 'master'

Workhorse: simplify error handling in LSIF parser

See merge request gitlab-org/gitlab!85092
parents 664c0e27 5acca3fc
package parser
import (
"errors"
"strings"
)
func combineErrors(errsOrNil ...error) error {
var errs []error
for _, err := range errsOrNil {
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
if len(errs) == 1 {
return errs[0]
}
var msgs []string
for _, err := range errs {
msgs = append(msgs, err.Error())
}
return errors.New(strings.Join(msgs, "\n"))
}
package parser
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
type customErr struct {
err string
}
func (e customErr) Error() string {
return e.err
}
func TestCombineErrors(t *testing.T) {
err := combineErrors(nil, errors.New("first"), nil, customErr{"second"})
require.EqualError(t, err, "first\nsecond")
err = customErr{"custom error"}
require.Equal(t, err, combineErrors(nil, err, nil))
require.Nil(t, combineErrors(nil, nil, nil))
}
...@@ -95,10 +95,15 @@ func (h *Hovers) For(refId Id) json.RawMessage { ...@@ -95,10 +95,15 @@ func (h *Hovers) For(refId Id) json.RawMessage {
} }
func (h *Hovers) Close() error { func (h *Hovers) Close() error {
return combineErrors( for _, err := range []error{
h.File.Close(), h.File.Close(),
h.Offsets.Close(), h.Offsets.Close(),
) } {
if err != nil {
return err
}
}
return nil
} }
func (h *Hovers) addData(line []byte) error { func (h *Hovers) addData(line []byte) error {
......
...@@ -130,11 +130,16 @@ func (r *Ranges) Serialize(f io.Writer, rangeIds []Id, docs map[Id]string) error ...@@ -130,11 +130,16 @@ func (r *Ranges) Serialize(f io.Writer, rangeIds []Id, docs map[Id]string) error
} }
func (r *Ranges) Close() error { func (r *Ranges) Close() error {
return combineErrors( for _, err := range []error{
r.Cache.Close(), r.Cache.Close(),
r.References.Close(), r.References.Close(),
r.Hovers.Close(), r.Hovers.Close(),
) } {
if err != nil {
return err
}
}
return nil
} }
func (r *Ranges) definitionPathFor(docs map[Id]string, refId Id) string { func (r *Ranges) definitionPathFor(docs map[Id]string, refId Id) string {
......
...@@ -86,10 +86,15 @@ func (r *References) For(docs map[Id]string, refId Id) []SerializedReference { ...@@ -86,10 +86,15 @@ func (r *References) For(docs map[Id]string, refId Id) []SerializedReference {
} }
func (r *References) Close() error { func (r *References) Close() error {
return combineErrors( for _, err := range []error{
r.Items.Close(), r.Items.Close(),
r.Offsets.Close(), r.Offsets.Close(),
) } {
if err != nil {
return err
}
}
return nil
} }
func (r *References) getItems(refId Id) []Item { func (r *References) getItems(refId Id) []Item {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment