diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..ae584b4 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,63 @@ +# Copyright 2019 Iguazio +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +name: CI + +on: [ push, pull_request ] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-go@v3 + with: + go-version: "1.17" + + - uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Lint + run: make lint + + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-go@v3 + with: + go-version: "1.17" + + - uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Test + run: make test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9db7fe2 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +# Copyright 2019 Iguazio +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +GOPATH ?= $(shell go env GOPATH) +OS_NAME = $(shell uname) + +ensure-gopath: +ifndef GOPATH + $(error GOPATH must be set) +endif + +.PHONY: modules +modules: ensure-gopath + @echo Getting go modules + @go mod download + +.PHONY: fmt +fmt: + gofmt -s -w . + +.PHONY: lint +lint: modules + @echo Installing linters... + @test -e $(GOPATH)/bin/impi || \ + (mkdir -p $(GOPATH)/bin && \ + curl -s https://api.github.com/repos/pavius/impi/releases/latest \ + | grep -i "browser_download_url.*impi.*$(OS_NAME)" \ + | cut -d : -f 2,3 \ + | tr -d \" \ + | wget -O $(GOPATH)/bin/impi -qi - \ + && chmod +x $(GOPATH)/bin/impi) + + @test -e $(GOPATH)/bin/golangci-lint || \ + (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.41.1) + + @echo Verifying imports... + $(GOPATH)/bin/impi \ + --local github.com/nuclio/errors/ \ + --scheme stdLocalThirdParty \ + ./... + + @echo Linting... + $(GOPATH)/bin/golangci-lint run -v + @echo Done. + +.PHONY: test +test: modules + go test -v ./... -p 1 diff --git a/errors.go b/errors.go index 451dec8..d265d79 100644 --- a/errors.go +++ b/errors.go @@ -1,15 +1,19 @@ /* Copyright 2019 The Nuclio Authors. + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + // Package errors provides an api similar to github.com/nuclio/nuclio/pkg/errors // However we don't carry stack trace around for performance // (see https://github.com/pkg/errors/issues/124) @@ -23,6 +27,7 @@ package errors import ( "bytes" + buildinterrors "errors" "fmt" "io" "os" @@ -49,7 +54,7 @@ func init() { ShowLineInfo = len(os.Getenv("NUCLIO_NO_ERROR_LINE_INFO")) == 0 } -// caller return the caller informatin (file, line) +// caller return the caller information (file, line) // Note this is sensitive to where it's called func caller() (string, int) { pcs := make([]uintptr, 1) @@ -120,6 +125,10 @@ func Wrapf(err error, format string, args ...interface{}) error { return errObj } +func Is(base, target error) bool { + return buildinterrors.Is(base, target) +} + // Error is the string representation of the error func (err *Error) Error() string { return err.message @@ -297,7 +306,7 @@ func (err *Error) Format(s fmt.State, verb rune) { } fallthrough case 's': - fmt.Fprintf(s, err.Error()) // nolint: errcheck + fmt.Fprint(s, err.Error()) // nolint: errcheck case 'q': fmt.Fprintf(s, "%q", err.Error()) // nolint: errcheck } diff --git a/errors_test.go b/errors_test.go index ee16239..b209f9d 100644 --- a/errors_test.go +++ b/errors_test.go @@ -1,19 +1,24 @@ /* Copyright 2019 The Nuclio Authors. + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + package errors import ( "bytes" + "context" "errors" "fmt" "path" @@ -27,6 +32,26 @@ type ErrorsTestSuite struct { suite.Suite } +func (suite *ErrorsTestSuite) TestIs() { + message := "hello" + + // the fact they have same message, does not mean they are the same error + suite.Require().Equal(false, Is(New(message), New(message))) + + // the fact they have same message, does not mean they are the same error, even when wrapped + suite.Require().Equal(false, Is(Wrap(New(message), "wrapped"), New(message))) + + // error is indeed a context cancelled + suite.Require().Equal(true, Is(context.Canceled, context.Canceled)) + suite.Require().Equal(true, Is(Wrap(context.Canceled, "wrapped"), context.Canceled)) + + // error is indeed someErr + someErr := errors.New("some error") + suite.Require().Equal(true, Is(someErr, someErr)) + suite.Require().Equal(true, Is(Wrap(someErr, "wrapped"), someErr)) + suite.Require().Equal(true, Is(Wrap(Wrap(someErr, "wrapped"), "TopWrapped"), someErr)) +} + func (suite *ErrorsTestSuite) TestNew() { var err error // Make sure we conform to Error interface message := "hello" diff --git a/go.mod b/go.mod index c242fb0..6fa2bc3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/nuclio/errors -go 1.12 +go 1.17 + +require github.com/stretchr/testify v1.8.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index e69de29..5164829 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=