Skip to content

Commit

Permalink
Provide Is functionality (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
liranbg authored Sep 7, 2022
1 parent d0b9940 commit 04ffcea
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 3 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
13 changes: 11 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -23,6 +27,7 @@ package errors

import (
"bytes"
buildinterrors "errors"
"fmt"
"io"
"os"
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 04ffcea

Please sign in to comment.