Skip to content

Commit

Permalink
Use std library errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 20, 2024
1 parent 5af5142 commit 9e0338c
Show file tree
Hide file tree
Showing 74 changed files with 267 additions and 311 deletions.
5 changes: 2 additions & 3 deletions assets/static/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package static

import (
"encoding/json"
"errors"

"github.com/buger/jsonparser"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/goflow/assets"

"github.com/buger/jsonparser"
"github.com/pkg/errors"
)

// Flow is a JSON serializable implementation of a flow asset
Expand Down
10 changes: 5 additions & 5 deletions assets/static/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package static

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)

// StaticSource is an asset source which loads assets from a static JSON file
Expand Down Expand Up @@ -40,7 +40,7 @@ func NewEmptySource() *StaticSource {
func NewSource(data json.RawMessage) (*StaticSource, error) {
s := &StaticSource{}
if err := utils.UnmarshalAndValidate(data, &s.s); err != nil {
return nil, errors.Wrap(err, "unable to read assets")
return nil, fmt.Errorf("unable to read assets: %w", err)
}
return s, nil
}
Expand All @@ -49,7 +49,7 @@ func NewSource(data json.RawMessage) (*StaticSource, error) {
func LoadSource(path string) (*StaticSource, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "error reading file '%s'", path)
return nil, fmt.Errorf("error reading file '%s': %w", path, err)

Check warning on line 52 in assets/static/source.go

View check run for this annotation

Codecov / codecov/patch

assets/static/source.go#L52

Added line #L52 was not covered by tests
}
return NewSource(data)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func (s *StaticSource) FlowByUUID(uuid assets.FlowUUID) (assets.Flow, error) {
return flow, nil
}
}
return nil, errors.Errorf("no such flow with UUID '%s'", uuid)
return nil, fmt.Errorf("no such flow with UUID '%s'", uuid)

Check warning on line 91 in assets/static/source.go

View check run for this annotation

Codecov / codecov/patch

assets/static/source.go#L91

Added line #L91 was not covered by tests
}

// Flow returns the flow asset with the given UUID
Expand All @@ -98,7 +98,7 @@ func (s *StaticSource) FlowByName(name string) (assets.Flow, error) {
return flow, nil
}
}
return nil, errors.Errorf("no such flow with name '%s'", name)
return nil, fmt.Errorf("no such flow with name '%s'", name)

Check warning on line 101 in assets/static/source.go

View check run for this annotation

Codecov / codecov/patch

assets/static/source.go#L101

Added line #L101 was not covered by tests
}

// Globals returns all global assets
Expand Down
3 changes: 1 addition & 2 deletions cmd/classify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/services/classification/luis"
"github.com/nyaruka/goflow/services/classification/wit"
"github.com/pkg/errors"
)

const usage = `usage: classify [flags] <input>`
Expand Down Expand Up @@ -72,7 +71,7 @@ func classify(svcs map[string]flows.ClassificationService, input string) (map[st
for t, s := range svcs {
c, err := s.Classify(nil, input, log.Log)
if err != nil {
return nil, log.Logs, errors.Wrapf(err, "error classifying with %s", t)
return nil, log.Logs, fmt.Errorf("error classifying with %s: %w", t, err)

Check warning on line 74 in cmd/classify/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/classify/main.go#L74

Added line #L74 was not covered by tests
}
res[t] = c
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/docgen/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package completion

import (
"fmt"

"github.com/pkg/errors"
)

// types available in root of context even without a session
Expand Down Expand Up @@ -47,13 +45,13 @@ func (c *Completion) Validate() error {
for _, t := range c.Types {
for _, ref := range t.TypeRefs() {
if !knownTypes[ref] {
return errors.Errorf("context type %s references unknown type %s", t.Name(), ref)
return fmt.Errorf("context type %s references unknown type %s", t.Name(), ref)

Check warning on line 48 in cmd/docgen/completion/completion.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/completion/completion.go#L48

Added line #L48 was not covered by tests
}
}
}
for _, p := range c.Root {
if !knownTypes[p.Type] {
return errors.Errorf("context root references unknown type %s", p.Type)
return fmt.Errorf("context root references unknown type %s", p.Type)

Check warning on line 54 in cmd/docgen/completion/completion.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/completion/completion.go#L54

Added line #L54 was not covered by tests
}
}
return nil
Expand Down
10 changes: 4 additions & 6 deletions cmd/docgen/docs/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/goflow/utils/po"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -40,7 +38,7 @@ func Generate(baseDir, outputDir, localeDir string) error {
// extract all documented items from the source code
taggedItems, err := FindAllTaggedItems(baseDir)
if err != nil {
return errors.Wrap(err, "error extracting tagged items")
return fmt.Errorf("error extracting tagged items: %w", err)

Check warning on line 41 in cmd/docgen/docs/base.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/base.go#L41

Added line #L41 was not covered by tests
}

for k, v := range taggedItems {
Expand Down Expand Up @@ -72,7 +70,7 @@ func Generate(baseDir, outputDir, localeDir string) error {

// and merge with existing locale files
if err := locales.Update(poDomain, pot); err != nil {
return errors.Wrap(err, "error updating locale files")
return fmt.Errorf("error updating locale files: %w", err)

Check warning on line 73 in cmd/docgen/docs/base.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/base.go#L73

Added line #L73 was not covered by tests
}

return nil
Expand All @@ -92,7 +90,7 @@ func generateForLang(baseDir, outputDir string, items map[string][]*TaggedItem,
// load PO file for translations
po, err := locales.Load(locale, poDomain)
if err != nil {
return errors.Wrapf(err, "error loading PO file for '%s'", locale)
return fmt.Errorf("error loading PO file for '%s': %w", locale, err)

Check warning on line 93 in cmd/docgen/docs/base.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/base.go#L93

Added line #L93 was not covered by tests
}

// create gettext function which will keep track of all unique message IDs
Expand All @@ -108,7 +106,7 @@ func generateForLang(baseDir, outputDir string, items map[string][]*TaggedItem,
fmt.Printf("Invoking generator: %s...\n", g.Name())

if err := g.Generate(baseDir, genDir, items, gettext); err != nil {
return errors.Wrapf(err, "error invoking generator %s", g.Name())
return fmt.Errorf("error invoking generator %s: %w", g.Name(), err)

Check warning on line 109 in cmd/docgen/docs/base.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/base.go#L109

Added line #L109 was not covered by tests
}
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/docgen/docs/editor_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/cmd/docgen/completion"

"github.com/pkg/errors"
)

func init() {
Expand Down Expand Up @@ -90,7 +88,7 @@ func (g *editorSupportGenerator) buildContextCompletion(items map[string][]*Tagg
for i, propDesc := range item.examples {
prop := completion.ParseProperty(propDesc)
if prop == nil {
return nil, errors.Errorf("invalid format for property description \"%s\"", propDesc)
return nil, fmt.Errorf("invalid format for property description \"%s\"", propDesc)

Check warning on line 91 in cmd/docgen/docs/editor_support.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/editor_support.go#L91

Added line #L91 was not covered by tests
}
prop.Help = gettext(prop.Help)
properties[i] = prop
Expand Down
22 changes: 10 additions & 12 deletions cmd/docgen/docs/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/test"

"github.com/pkg/errors"
)

func init() {
Expand Down Expand Up @@ -63,15 +61,15 @@ func (g *htmlDocsGenerator) Name() string {

func (g *htmlDocsGenerator) Generate(baseDir, outputDir string, items map[string][]*TaggedItem, gettext func(string) string) error {
if err := renderTemplateDocs(baseDir, outputDir, items); err != nil {
return errors.Wrap(err, "error rendering templates")
return fmt.Errorf("error rendering templates: %w", err)

Check warning on line 64 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L64

Added line #L64 was not covered by tests
}

// copy static resources to docs dir
for _, res := range Resources {
src := path.Join(baseDir, templateDir, res)
dst := path.Join(outputDir, res)
if err := copyFile(src, dst); err != nil {
return errors.Wrap(err, "error copying resource")
return fmt.Errorf("error copying resource: %w", err)

Check warning on line 72 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L72

Added line #L72 was not covered by tests
}
fmt.Printf(" > Copied %s > %s\n", src, dst)
}
Expand All @@ -82,7 +80,7 @@ func renderTemplateDocs(baseDir string, outputDir string, items map[string][]*Ta
// render items as context for the main doc templates
context, err := buildTemplateContext(items)
if err != nil {
return errors.Wrap(err, "error building docs context")
return fmt.Errorf("error building docs context: %w", err)

Check warning on line 83 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L83

Added line #L83 was not covered by tests
}

// to post-process templates to resolve links between templates
Expand All @@ -102,14 +100,14 @@ func renderTemplateDocs(baseDir string, outputDir string, items map[string][]*Ta
htmlPath := path.Join(outputDir, template.Path[0:len(template.Path)-3]+".html")

if err := renderTemplate(templatePath, renderedPath, context, linkResolver, linkTargets); err != nil {
return errors.Wrapf(err, "error rendering template %s", templatePath)
return fmt.Errorf("error rendering template %s: %w", templatePath, err)

Check warning on line 103 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L103

Added line #L103 was not covered by tests
}

htmlTemplate := path.Join(baseDir, "cmd/docgen/templates/template.html")
htmlContext := map[string]string{"title": template.Title}

if err := renderHTML(renderedPath, htmlPath, htmlTemplate, template.TOC, htmlContext); err != nil {
return errors.Wrapf(err, "error rendering HTML from %s to %s", renderedPath, htmlPath)
return fmt.Errorf("error rendering HTML from %s to %s: %w", renderedPath, htmlPath, err)

Check warning on line 110 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L110

Added line #L110 was not covered by tests
}

fmt.Printf(" > Rendered %s > %s > %s\n", templatePath, renderedPath, htmlPath)
Expand All @@ -123,12 +121,12 @@ func renderTemplate(src, dst string, context map[string]string, resolver urlReso
// generate our complete docs
docTpl, err := template.ParseFiles(src)
if err != nil {
return errors.Wrap(err, "error reading template file")
return fmt.Errorf("error reading template file: %w", err)

Check warning on line 124 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L124

Added line #L124 was not covered by tests
}

output := &strings.Builder{}
if err := docTpl.Execute(output, context); err != nil {
return errors.Wrap(err, "error executing template")
return fmt.Errorf("error executing template: %w", err)

Check warning on line 129 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L129

Added line #L129 was not covered by tests
}

processed := resolveLinks(output.String(), resolver, linkTargets)
Expand Down Expand Up @@ -177,7 +175,7 @@ func createLinkResolver(items map[string][]*TaggedItem) (urlResolver, map[string
return func(tag string, val string) (string, error) {
linkTpl := typeTemplates[tag]
if linkTpl == "" {
return "", errors.Errorf("no link template for type %s", tag)
return "", fmt.Errorf("no link template for type %s", tag)

Check warning on line 178 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L178

Added line #L178 was not covered by tests
}
return fmt.Sprintf(linkTpl, val), nil
}, linkTargets
Expand Down Expand Up @@ -224,12 +222,12 @@ func buildTemplateContext(items map[string][]*TaggedItem) (map[string]string, er

session, _, err := test.CreateTestSession(server.URL, envs.RedactionPolicyNone)
if err != nil {
return nil, errors.Wrap(err, "error creating example session")
return nil, fmt.Errorf("error creating example session: %w", err)

Check warning on line 225 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L225

Added line #L225 was not covered by tests
}

voiceSession, _, err := test.CreateTestVoiceSession(server.URL)
if err != nil {
return nil, errors.Wrap(err, "error creating example session")
return nil, fmt.Errorf("error creating example session: %w", err)

Check warning on line 230 in cmd/docgen/docs/html.go

View check run for this annotation

Codecov / codecov/patch

cmd/docgen/docs/html.go#L230

Added line #L230 was not covered by tests
}

context := make(map[string]string)
Expand Down

0 comments on commit 9e0338c

Please sign in to comment.