Skip to content

Commit

Permalink
Merge pull request suggest-go#44 from suggest-go/issue_37
Browse files Browse the repository at this point in the history
Issue37 Wrap errors by calling fmt.Errorf `%w`
  • Loading branch information
alldroll authored Feb 25, 2020
2 parents 5a48ce1 + 0ae14d5 commit 0f4361f
Show file tree
Hide file tree
Showing 34 changed files with 116 additions and 130 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
vendor/*
build/*
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: go

go:
- "1.12.x"
- "1.13.x"

script:
- env GO111MODULE=on make download
- env GO111MODULE=on make test
- make vet
- make test

notifications:
email:
Expand Down
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.PHONY: build test vet clean

BUILD_FLAGS = -mod=vendor $(GO_BUILD_FLAGS)
BUILD_FLAGS = $(GO_BUILD_FLAGS)

default: build

Expand All @@ -15,19 +15,15 @@ build-lm:
build-spellchecker:
go build $(BUILD_FLAGS) -o build/spellchecker ./cmd/spellchecker/

build: download test vet build-suggest build-lm build-spellchecker
build-bin: download build-suggest build-lm
build: test vet build-suggest build-lm build-spellchecker
build-bin: build-suggest build-lm

build-docker:
docker build --no-cache -t suggest:latest .

test:
go test -race -v ./...

download:
go mod download
go mod vendor

vet:
go vet ./...

Expand Down
4 changes: 2 additions & 2 deletions cmd/language-model/cmd/build-lm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ var buildLMCmd = &cobra.Command{
config, err := lm.ReadConfig(configPath)

if err != nil {
return fmt.Errorf("couldn't read a config %v", err)
return fmt.Errorf("couldn't read a config %w", err)
}

directory, err := store.NewFSDirectory(config.GetOutputPath())

if err != nil {
return fmt.Errorf("failed to create a fs directory: %v", err)
return fmt.Errorf("failed to create a fs directory: %w", err)
}

return lm.StoreBinaryLMFromGoogleFormat(directory, config)
Expand Down
4 changes: 2 additions & 2 deletions cmd/language-model/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ var evalCmd = &cobra.Command{
config, err := lm.ReadConfig(configPath)

if err != nil {
return fmt.Errorf("failed to read config file: %v", err)
return fmt.Errorf("failed to read config file: %w", err)
}

directory, err := store.NewFSDirectory(config.GetOutputPath())

if err != nil {
return fmt.Errorf("failed to create a fs directory: %v", err)
return fmt.Errorf("failed to create a fs directory: %w", err)
}

languageModel, err := lm.RetrieveLMFromBinary(directory, config)
Expand Down
8 changes: 4 additions & 4 deletions cmd/language-model/cmd/ngram-count.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var countNGramsCmd = &cobra.Command{
config, err := lm.ReadConfig(configPath)

if err != nil {
return fmt.Errorf("could read config %s", err)
return fmt.Errorf("could read config %w", err)
}

trie, err := buildNGramsCount(config)
Expand All @@ -40,7 +40,7 @@ func buildNGramsCount(config *lm.Config) (lm.CountTrie, error) {
sourceFile, err := os.Open(config.GetSourcePath())

if err != nil {
return nil, fmt.Errorf("could read source file %s", err)
return nil, fmt.Errorf("could read source file %w", err)
}

defer sourceFile.Close()
Expand All @@ -64,13 +64,13 @@ func storeNGramsCount(config *lm.Config, trie lm.CountTrie) error {
directory, err := store.NewFSDirectory(config.GetOutputPath())

if err != nil {
return fmt.Errorf("failed to create a fs directory: %v", err)
return fmt.Errorf("failed to create a fs directory: %w", err)
}

writer := lm.NewGoogleNGramWriter(config.NGramOrder, directory)

if err := writer.Write(trie); err != nil {
return fmt.Errorf("could save ngrams %s", err)
return fmt.Errorf("could save ngrams %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/spellchecker/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var evalCmd = &cobra.Command{
config, err := lm.ReadConfig(configPath)

if err != nil {
return fmt.Errorf("failed to read config file: %v", err)
return fmt.Errorf("failed to read config file: %w", err)
}

service, err := dep.BuildSpellChecker(config, indexDescription)
Expand Down
2 changes: 1 addition & 1 deletion cmd/suggest/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func configureService() (*suggest.Service, error) {
description, err := suggest.ReadConfigs(configPath)

if err != nil {
return nil, fmt.Errorf("Failed to read configs: %v", err)
return nil, fmt.Errorf("Failed to read configs: %w", err)
}

suggestService := suggest.NewService()
Expand Down
18 changes: 9 additions & 9 deletions cmd/suggest/cmd/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func readConfigs() ([]suggest.IndexDescription, error) {
configs, err := suggest.ReadConfigs(configPath)

if err != nil {
return nil, fmt.Errorf("invalid config file format %s", err)
return nil, fmt.Errorf("invalid config file format %w", err)
}

return configs, nil
Expand All @@ -111,7 +111,7 @@ func indexJob(description suggest.IndexDescription) error {
dict, err := buildDictionaryJob(description)

if err != nil {
return fmt.Errorf("failed to build a dictionary: %v", err)
return fmt.Errorf("failed to build a dictionary: %w", err)
}

log.Printf("Time spent %s", time.Since(start))
Expand All @@ -123,7 +123,7 @@ func indexJob(description suggest.IndexDescription) error {
directory, err := store.NewFSDirectory(description.GetIndexPath())

if err != nil {
return fmt.Errorf("failed to create a directory: %v", err)
return fmt.Errorf("failed to create a directory: %w", err)
}

if err = suggest.Index(directory, dict, description.GetWriterConfig(), description.GetIndexTokenizer()); err != nil {
Expand Down Expand Up @@ -179,7 +179,7 @@ func newDictionaryReader(config suggest.IndexDescription) (dictionary.Iterable,
f, err := os.Open(config.GetSourcePath())

if err != nil {
return nil, fmt.Errorf("could not open a source file %s", err)
return nil, fmt.Errorf("could not open a source file %w", err)
}

scanner := bufio.NewScanner(f)
Expand All @@ -194,17 +194,17 @@ func tryToSendReindexSignal() error {
d, err := ioutil.ReadFile(pidPath)

if err != nil {
return fmt.Errorf("error parsing pid from %s: %s", pidPath, err)
return fmt.Errorf("error parsing pid from %s: %w", pidPath, err)
}

pid, err := strconv.Atoi(string(bytes.TrimSpace(d)))

if err != nil {
return fmt.Errorf("error parsing pid from %s: %s", pidPath, err)
return fmt.Errorf("error parsing pid from %s: %w", pidPath, err)
}

if err := syscall.Kill(pid, syscall.SIGHUP); err != nil {
return fmt.Errorf("fail to send reindex signal to %d, %s", pid, err)
return fmt.Errorf("fail to send reindex signal to %d, %w", pid, err)
}

return nil
Expand All @@ -215,14 +215,14 @@ func tryToSendReindexRequest() error {
resp, err := http.Post(host, "text/plain", nil)

if err != nil {
return fmt.Errorf("fail to send reindex request to %s, %s", host, err)
return fmt.Errorf("fail to send reindex request to %s, %w", host, err)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return fmt.Errorf("fail to read response body %s", err)
return fmt.Errorf("fail to read response body %w", err)
}

if string(body) != "OK" {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ require (
github.com/RoaringBitmap/roaring v0.4.20
github.com/alldroll/cdb v1.0.2
github.com/alldroll/go-datastructures v0.0.0-20190322060030-1d3a19ff3b29
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da
github.com/edsrzf/mmap-go v0.0.0-20190108065903-904c4ced31cd
github.com/gorilla/handlers v1.4.0
github.com/gorilla/mux v1.7.1
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20190426135247-a129542de9ae // indirect
)
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/alldroll/go-datastructures v0.0.0-20190322060030-1d3a19ff3b29 h1:gKZg
github.com/alldroll/go-datastructures v0.0.0-20190322060030-1d3a19ff3b29/go.mod h1:3IP5cUVnXIyZIsUbKXztqoz1brdOXngjFyYtmwqz9MY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da h1:0qwwqQCLOOXPl58ljnq3sTJR7yRuMolM02vjxDh4ZVE=
github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da/go.mod h1:ns+zIWBBchgfRdxNgIJWn2x6U95LQchxeqiN5Cgdgts=
github.com/edsrzf/mmap-go v0.0.0-20190108065903-904c4ced31cd h1:v8VTjPes659sdlQ3O2AbICsk2XjORhYc76QLCFSTEgA=
github.com/edsrzf/mmap-go v0.0.0-20190108065903-904c4ced31cd/go.mod h1:W3m91qexYIu40kcj8TLXNUSTCKprH8UQ3GgH5/Xyfc0=
github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2 h1:Ujru1hufTHVb++eG6OuNDKMxZnGIvF6o/u8q/8h2+I4=
Expand Down Expand Up @@ -47,13 +45,8 @@ github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc=
github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 h1:MlY3mEfbnWGmUi4rtHOtNnnnN4UJRGSyLPx+DXA5Sq4=
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190426135247-a129542de9ae h1:mQLHiymj/JXKnnjc62tb7nD5pZLs940/sXJu+Xp3DBA=
golang.org/x/sys v0.0.0-20190426135247-a129542de9ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
17 changes: 9 additions & 8 deletions internal/spellchecker/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package api
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/suggest-go/suggest/internal/http"
"github.com/suggest-go/suggest/internal/spellchecker/dep"
"github.com/suggest-go/suggest/pkg/lm"
"github.com/suggest-go/suggest/pkg/suggest"
"log"
"os"
"os/signal"
"syscall"
)

// App is our application
Expand All @@ -22,9 +23,9 @@ type App struct {

// AppConfig is an application config
type AppConfig struct {
Port string
ConfigPath string
PidPath string
Port string
ConfigPath string
PidPath string
IndexDescription suggest.IndexDescription
}

Expand All @@ -41,7 +42,7 @@ func (a App) Run() error {
config, err := lm.ReadConfig(a.config.ConfigPath)

if err != nil {
return fmt.Errorf("failed to read config file: %v", err)
return fmt.Errorf("failed to read config file: %w", err)
}

spellchecker, err := dep.BuildSpellChecker(config, a.config.IndexDescription)
Expand Down
10 changes: 5 additions & 5 deletions internal/spellchecker/dep/spellchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ func BuildSpellChecker(config *lm.Config, indexDescription suggest.IndexDescript
directory, err := store.NewFSDirectory(config.GetOutputPath())

if err != nil {
return nil, fmt.Errorf("failed to create a fs directory: %v", err)
return nil, fmt.Errorf("failed to create a fs directory: %w", err)
}

languageModel, err := lm.RetrieveLMFromBinary(directory, config)

if err != nil {
return nil, fmt.Errorf("failed to retrieve a lm model from binary format: %v", err)
return nil, fmt.Errorf("failed to retrieve a lm model from binary format: %w", err)
}

dict, err := dictionary.OpenCDBDictionary(config.GetDictionaryPath())

if err != nil {
return nil, fmt.Errorf("failed to open a cdb dictionary: %v", err)
return nil, fmt.Errorf("failed to open a cdb dictionary: %w", err)
}

// create runtime search index builder
builder, err := suggest.NewRAMBuilder(dict, indexDescription)

if err != nil {
return nil, fmt.Errorf("failed to create a ngram index: %v", err)
return nil, fmt.Errorf("failed to create a ngram index: %w", err)
}

index, err := builder.Build()

if err != nil {
return nil, fmt.Errorf("failed to build a ngram index: %v", err)
return nil, fmt.Errorf("failed to build a ngram index: %w", err)
}

return spellchecker.New(
Expand Down
6 changes: 3 additions & 3 deletions internal/suggest/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a App) Run() error {
}

if err := reindexJob(); err != nil {
return fmt.Errorf("Fail to configure service: %s", err)
return fmt.Errorf("Fail to configure service: %w", err)
}

ctx, cancelFn := context.WithCancel(context.Background())
Expand Down Expand Up @@ -93,13 +93,13 @@ func (a App) writePIDFile() error {

err := os.MkdirAll(filepath.Dir(a.config.PidPath), 0700)
if err != nil {
return fmt.Errorf("There is no such file for %s, %s", a.config.PidPath, err)
return fmt.Errorf("There is no such file for %s, %w", a.config.PidPath, err)
}

// Retrieve the PID and write it.
pid := strconv.Itoa(os.Getpid())
if err := ioutil.WriteFile(a.config.PidPath, []byte(pid), 0644); err != nil {
return fmt.Errorf("Fail to write pid file to %s, %s", a.config.PidPath, err)
return fmt.Errorf("Fail to write pid file to %s, %w", a.config.PidPath, err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/dictionary/cdb_dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewCDBDictionary(r io.ReaderAt) (Dictionary, error) {
reader, err := handle.GetReader(r)

if err != nil {
return nil, fmt.Errorf("fail to create cdb dictionary: %v", err)
return nil, fmt.Errorf("fail to create cdb dictionary: %w", err)
}

return &cdbDictionary{
Expand Down
Loading

0 comments on commit 0f4361f

Please sign in to comment.