Skip to content

Commit

Permalink
Fix errors.Is(status.X) (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Feb 2, 2023
1 parent 7dca61e commit 5f19f7d
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 21 deletions.
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
If feasible/relevant, please provide a code snippet (inline or with Go playground) to reproduce the issue.


**Expected behavior**
A clear and concise description of what you expected to happen.

**Additional context**
Add any other context about the problem here.
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

Please use discussions https://github.com/swaggest/usecase/discussions/categories/ideas to share feature ideas.
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Question
about: Any question about features or usage
title: ''
labels: ''
assignees: ''

---

Please use discussions https://github.com/swaggest/usecase/discussions/categories/q-a to make your question more discoverable by other folks.
7 changes: 3 additions & 4 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ jobs:
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
- name: Count lines of code
id: loc
run: |
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
sccdiff_hash=$(git hash-object ./sccdiff)
[ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1)
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
echo "${OUTPUT}"
OUTPUT="${OUTPUT//$'\n'/%0A}"
echo "::set-output name=diff::$OUTPUT"
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
- name: Comment Code Lines
- name: Comment lines of code
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
go-version: 1.19.x
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.1.0
uses: golangci/golangci-lint-action@v3.3.1
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.49.0
version: v1.50.1

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/gorelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ jobs:
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
OUTPUT=$(gorelease 2>&1 || exit 0)
echo "${OUTPUT}"
OUTPUT="${OUTPUT//$'\n'/%0A}"
echo "::set-output name=report::$OUTPUT"
- name: Comment Report
echo "report<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
- name: Comment report
continue-on-error: true
uses: marocchino/sticky-pull-request-comment@v2
with:
Expand Down
12 changes: 5 additions & 7 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
go tool cover -func=./unit.coverprofile > unit.txt
TOTAL=$(grep 'total:' unit.txt)
echo "${TOTAL}"
echo "::set-output name=total::$TOTAL"
echo "total=$TOTAL" >> $GITHUB_OUTPUT
- name: Annotate missing test coverage
id: annotate
Expand All @@ -94,16 +94,14 @@ jobs:
git fetch origin master ${{ github.event.pull_request.base.sha }}
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
echo "${REP}"
REP="${REP//$'\n'/%0A}"
cat gha-unit.txt
DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
DIFF="${DIFF//$'\n'/%0A}"
TOTAL=$(cat delta-cov-unit.txt)
echo "::set-output name=rep::$REP"
echo "::set-output name=diff::$DIFF"
echo "::set-output name=total::$TOTAL"
echo "rep<<EOF" >> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
echo "total<<EOF" >> $GITHUB_OUTPUT && echo "$TOTAL" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
- name: Comment Test Coverage
- name: Comment test coverage
continue-on-error: true
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
uses: marocchino/sticky-pull-request-comment@v2
Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ linters:
- structcheck
- varcheck
- deadcode
- testableexamples
- dupword

issues:
exclude-use-default: false
Expand All @@ -65,5 +67,6 @@ issues:
path: "_test.go"
- linters:
- errcheck # Error checking omitted for brevity.
- gosec
path: "example_"

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#GOLANGCI_LINT_VERSION := "v1.49.0" # Optional configuration to pinpoint golangci-lint version.
#GOLANGCI_LINT_VERSION := "v1.50.1" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/swaggest/usecase
go 1.18

require (
github.com/bool64/dev v0.2.20
github.com/bool64/dev v0.2.24
github.com/stretchr/testify v1.8.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/bool64/dev v0.2.20 h1:9eIRGdcg2kQW2NGza++QbOKidNNaK+KfWuUXcZFDejE=
github.com/bool64/dev v0.2.20/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.24 h1:xptlKivPh870W3Xc9szPcM7wkFmTMuHT8rc0nu7dITk=
github.com/bool64/dev v0.2.24/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
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=
Expand Down
13 changes: 12 additions & 1 deletion status/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package status

import "strings"
import (
"errors"
"strings"
)

var codeToMsg = func() map[Code]string {
res := make(map[Code]string, len(strToCode))
Expand Down Expand Up @@ -29,6 +32,14 @@ func (e errorWithStatus) Unwrap() error {
return e.err
}

func (e errorWithStatus) Is(target error) bool {
if target == e.code { //nolint:goerr113 // Target is expected to be plain status error.
return true
}

return errors.Is(e.err, target)
}

func (e errorWithStatus) Status() Code {
return e.code
}
Expand Down
2 changes: 2 additions & 0 deletions status/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
func TestWrap(t *testing.T) {
err := status.Wrap(errors.New("failed"), status.AlreadyExists)
assert.EqualError(t, err, "already exists: failed")
assert.True(t, errors.Is(err, status.AlreadyExists))
assert.False(t, errors.Is(err, status.NotFound))
assert.EqualError(t, err.(interface{ Unwrap() error }).Unwrap(), "failed")
assert.Equal(t, status.AlreadyExists, err.(interface{ Status() status.Code }).Status())
}
Expand Down

0 comments on commit 5f19f7d

Please sign in to comment.