Skip to content

Commit

Permalink
new github actions
Browse files Browse the repository at this point in the history
gofmt

new github actions for testing

yaml syntax

remove windows form tests for a moment: fs error strings are different

type

wrong test.RequireEqual -> test.Require

removing windows examples from test build & bring it to CI pipe

flag description

NilThen -> err2.Handle(.. noerr)

sample/README more use cases and fix for sample starts

version code samples & more explanations

goreleaser to release.sh

goreleaser onboard

update release info and add auto-migration tip

Handler fn type, & updated documentation

logf function fixes stack frame bug on Result1 and 2

formatting documentation example

fixed lint-issue in ExampleOut_errorHappensNot

migration quides v0.9.40

scripts/README.md refs updated
  • Loading branch information
lainio committed Aug 16, 2023
1 parent ca7db67 commit bd00be9
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 122 deletions.
51 changes: 33 additions & 18 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: --timeout=5m
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: --timeout=5m
test:
strategy:
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x]
os: [windows-latest, ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: setup go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: checkout
uses: actions/checkout@v2
- name: test
run: make test
test-cov:
runs-on: ubuntu-latest
steps:
- name: setup
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- name: checkout
uses: actions/checkout@v2
- name: test
run: make test_cov_out
- name: upload
uses: codecov/codecov-action@v2
with:
files: ./coverage.txt
- name: setup
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- name: checkout
uses: actions/checkout@v2
- name: test
run: make test_cov_out
- name: upload
uses: codecov/codecov-action@v2
with:
files: ./coverage.txt
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ coverage.*

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

dist/
22 changes: 22 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- skip: true

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Cangelog
## Changelog

### Version history

Expand Down
29 changes: 13 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,46 +58,43 @@ tinline_handler:
$(GO) test -c -gcflags=-m=2 $(PKG_HANDLER) 2>&1 | ag 'inlin'

bench:
$(GO) test -bench=. $(PKGS)
$(GO) test $(TEST_ARGS) -bench=. $(PKGS)

bench_goid:
$(GO) test -bench='BenchmarkGoid' $(PKG_ASSERT)
$(GO) test $(TEST_ARGS) -bench='BenchmarkGoid' $(PKG_ASSERT)

bench_reca:
$(GO) test -bench='BenchmarkRecursion.*' $(PKG_ERR2)
$(GO) test $(TEST_ARGS) -bench='BenchmarkRecursion.*' $(PKG_ERR2)

bench_out:
$(GO) test -bench='BenchmarkTryOut_.*' $(PKG_ERR2)
$(GO) test $(TEST_ARGS) -bench='BenchmarkTryOut_.*' $(PKG_ERR2)

bench_go:
$(GO) test -bench='BenchmarkTry_StringGenerics' $(PKG_ERR2)

bench_arec:
$(GO) test -bench='BenchmarkRecursion.*' $(PKG_ERR2)
$(GO) test $(TEST_ARGS) -bench='BenchmarkTry_StringGenerics' $(PKG_ERR2)

bench_that:
$(GO) test -bench='BenchmarkThat.*' $(PKG_ASSERT)
$(GO) test $(TEST_ARGS) -bench='BenchmarkThat.*' $(PKG_ASSERT)

bench_copy:
$(GO) test -bench='Benchmark_CopyBuffer' $(PKG_TRY)
$(GO) test $(TEST_ARGS) -bench='Benchmark_CopyBuffer' $(PKG_TRY)

bench_rece:
$(GO) test -bench='BenchmarkRecursionWithTryAnd_Empty_Defer' $(PKG_ERR2)
$(GO) test $(TEST_ARGS) -bench='BenchmarkRecursionWithTryAnd_Empty_Defer' $(PKG_ERR2)

bench_rec:
$(GO) test -bench='BenchmarkRecursionWithOldErrorIfCheckAnd_Defer' $(PKG_ERR2)
$(GO) test $(TEST_ARGS) -bench='BenchmarkRecursionWithOldErrorIfCheckAnd_Defer' $(PKG_ERR2)

bench_err2:
$(GO) test -bench=. $(PKG_ERR2)
$(GO) test $(TEST_ARGS) -bench=. $(PKG_ERR2)

bench_assert:
$(GO) test -bench=. $(PKG_ASSERT)
$(GO) test $(TEST_ARGS) -bench=. $(PKG_ASSERT)

bench_str:
$(GO) test -bench=. $(PKG_STR)
$(GO) test $(TEST_ARGS) -bench=. $(PKG_STR)

bench_x:
$(GO) test -bench=. $(PKG_X)
$(GO) test $(TEST_ARGS) -bench=. $(PKG_X)

vet: | test
$(GO) vet $(PKGS)
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ We also mark functions deprecated before they become obsolete. Usually, one
released version before. We have tested this with a large code base in our
systems, and it works wonderfully.
More information can be found in the scripts' [readme file](./scripts/README.md).
More information can be found in the `scripts/` directory [readme
file](./scripts/README.md).
## Assertion
Expand Down Expand Up @@ -435,23 +436,29 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
simplest `defer` function in the `err`-returning function** . (Please see
the `defer` benchmarks in the `err2_test.go` and run `make bench_reca`)
- the solution caused a change to API, where the core reason is Go's
optimization "bug". (We don't have confirmation yet)
optimization "bug". (We don't have confirmation yet.)
- Changed API for deferred error handling: `defer err2.Handle/Catch()`
- Deprecated:
- *Obsolete*:
```go
defer err2.Handle(&err, func() { // <- relaying closure to access err val
defer err2.Handle(&err, func() {}) // <- relaying closure to access err val
```
- Current version:
```go
defer err2.Handle(&err, func(error) error { // <- err val goes thru
defer err2.Handle(&err, func(err error) error { return err }) // not a closure
```
- Added a new API:
Because handler function is not relaying closures any more, it opens a new
opportunity to use and build general helper functions: `err2.Noop`, etc.
- Use auto-migration scripts especially for large code-bases. More information
can be found in the `scripts/` directory's [readme file](./scripts/README.md).
- Added a new (*experimental*) API:
```go
defer err2.Handle(&err, func(noerr bool) {
assert.That(noerr) // noerr is always true!!
doSomething()
})
```
This is experimental because we aren't sure if this is something we want to
have in the `err2` package.
- Bug fixes: `ResultX.Logf()` now works as it should
- More documentation
Expand Down
31 changes: 22 additions & 9 deletions err2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"github.com/lainio/err2/internal/handler"
)

type (
// Handler is a function type used to process error values in Handle and
// Catch. We currently have a few build-ins of the Handler: err2.Noop,
// err2.Reset, etc.
Handler func(error) error
)

var (
// ErrNotFound is similar *no-error* like io.EOF for those who really want to
// use error return values to transport non errors. It's far better to have
Expand Down Expand Up @@ -101,28 +108,28 @@ func Handle(err *error, a ...any) {
//
// Catch support logging as well:
//
// defer err2.Catch("WARNING: catched errors: %s", name)
// defer err2.Catch("WARNING: caught errors: %s", name)
//
// The preceding line catches the errors and panics and prints an annotated
// error message about the error source (from where the error was thrown) to the
// currently set log.
//
// The next one stops errors and panics, but allows you handle errors, like
// cleanups, etc. The error handler function has same signature as Handle's
// error handling function: func(err error) error. By returning nil resets the
// error handling function, i.e., err2.Handler. By returning nil resets the
// error, which allows e.g. prevent automatic error logs to happening.
// Otherwise, the output results depends on the current Tracer and assert
// settings. Default setting print call stacks for panics but not for errors.
// settings. Default setting print call stacks for panics but not for errors:
//
// defer err2.Catch(func(err error) error { return err} )
//
// or if you you prefer to use dedicated helpers:
//
// defer err2.Catch(err2.Reset)
// defer err2.Catch(err2.Noop)
//
// The last one calls your error handler, and you have an explicit panic
// handler too, where you can e.g. continue panicking to propagate it for above
// callers:
// callers or stop it like below:
//
// defer err2.Catch(func(err error) error { return err }, func(p any) {})
func Catch(a ...any) {
Expand Down Expand Up @@ -165,20 +172,26 @@ func Throwf(format string, args ...any) {
panic(err)
}

// Noop is predeclared helper to use with Handle and Catch. It keeps the current
// Noop is a built-in helper to use with Handle and Catch. It keeps the current
// error value the same. You can use it like this:
//
// defer err2.Handle(&err, err2.Noop)
func Noop(err error) error { return err }

// Reset is predeclared helper to use with Handle and Catch. It sets the current
// Reset is a built-in helper to use with Handle and Catch. It sets the current
// error value to nil. You can use it like this to reset the error:
//
// defer err2.Handle(&err, err2.Reset)
func Reset(error) error { return nil }

// Err is predeclared helper to use with Handle and Catch. It offers simplifier
// for error handling function.
// Err is a built-in helper to use with Handle and Catch. It offers simplifier
// for error handling function for cases where you don't need to change the
// current error value. For instance, if you want to just write error to stdout,
// and don't want to use SetLogTracer and keep it to write to your logs.
//
// defer err2.Catch(err2.Err(func(err error) {
// fmt.Println("ERROR:", err)
// }))
func Err(f func(err error)) func(error) error {
return func(err error) error {
f(err)
Expand Down
2 changes: 1 addition & 1 deletion err2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestHandle_NoError(t *testing.T) {
var err error
var handlerCalled bool
defer func() {
test.RequireEqual(t, handlerCalled, true)
test.Require(t, handlerCalled)
}()
defer err2.Handle(&err, func(err error) error {
// this should not be called, so lets try to fuckup things...
Expand Down
2 changes: 2 additions & 0 deletions filecopy_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

package err2_test

import (
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func TestPreProcess_debug(t *testing.T) {
// and that's what error stack tracing is all about
Handle()

test.RequireEqual(t, panicHandlerCalled, false)
test.RequireEqual(t, errorHandlerCalled, false)
test.RequireEqual(t, nilHandlerCalled, false)
test.Require(t, !panicHandlerCalled)
test.Require(t, !errorHandlerCalled)
test.Require(t, !nilHandlerCalled)

// See the name of this test function. Decamel it + error
const want = "testing: t runner: error"
Expand Down
2 changes: 1 addition & 1 deletion internal/str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Decamel(s string) string {
isUpper bool
prevSkipped bool
)
b.Grow(2*len(s))
b.Grow(2 * len(s))

for i, v := range s {
skip := v == '(' || v == ')' || v == '*'
Expand Down
10 changes: 8 additions & 2 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ Please play with the samples by editing them and running the main files:
- `main.go` just a starter for different playgrounds
- `main-play.go` general playground based on CopyFile and recursion
- `main-db-sample.go` simulates DB transaction and money transfer
- `main-nil.go` samples and tests for logger and using `err2.Handle` for success

Run a default playground `play` mode:
```go
go run main.go
go run ./...
```

Or run the DB based version to maybe better understand how powerful the
automatic error string building is:
```go
go run main.go -mode db
go run ./... -mode db
```

You can print usage:
```go
go run ./... -h
```
Loading

0 comments on commit bd00be9

Please sign in to comment.