Skip to content

Commit

Permalink
feat: add with context method on guard (#1)
Browse files Browse the repository at this point in the history
* feat: add guardWithContext and onGuardWithContext

* update README.md

* feat: add context support

---------

Co-authored-by: GUO YANKE <[email protected]>
  • Loading branch information
xyz327 and yankeguo authored Jul 18, 2024
1 parent 96135ea commit b71d7cf
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ name: Go

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ Any function with the latest return value of type `error` can be wrapped by `rg.
package demo

import (
"context"
"encoding/json"
"github.com/yankeguo/rg"
"gopkg.in/yaml.v3"
"github.com/yankeguo/rg"
"os"
)

Expand Down Expand Up @@ -51,6 +52,18 @@ func jsonFileToYAML(filename string) (err error) {
rg.Must0(os.WriteFile(filename+".yaml", buf, 0640))
return
}

func GuardCallbackOnErrWithContext(ctx context.Context)(err error) {
rg.OnGuardWithContext = func(ctx context.Context, r any) {
// do something like logging with ctx on guarded
}
// for recovery panic
defer rg.Guard(&err, rg.WithContext(ctx))
// if err is not nil, it will panic
file:=rg.Must(os.ReadFile("something.txt"))
rg.Must0(os.WriteFile("something.txt", file, 0640))
return
}
```

## Credits
Expand Down
35 changes: 34 additions & 1 deletion guard.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
package rg

import (
"context"
"fmt"
)

// OnGuard is a global hook for Guard
// Deprecated use OnGuardWithContext
var OnGuard func(r any)

// OnGuardWithContext is a global hook for Guard And GuardWithContext
var OnGuardWithContext func(ctx context.Context, r any)

type options struct {
ctx context.Context
}

// Option for Guard
type Option func(opts *options)

// WithContext set context for Guard
func WithContext(ctx context.Context) Option {
return func(opts *options) {
opts.ctx = ctx
}
}

// Guard recover from panic and set err
func Guard(err *error) {
// Deprecated use GuardWithContext
func Guard(err *error, opts ...Option) {
_opts := options{}
for _, opt := range opts {
opt(&_opts)
}

ctx := _opts.ctx
if ctx == nil {
ctx = context.TODO()
}

if r := recover(); r != nil {
if fn := OnGuard; fn != nil {
fn(r)
}
if fn := OnGuardWithContext; fn != nil {
fn(ctx, r)
}
if re, ok := r.(error); ok {
*err = re
} else {
Expand Down
20 changes: 18 additions & 2 deletions guard_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rg

import (
"context"
"errors"
"testing"

Expand Down Expand Up @@ -34,15 +35,30 @@ func TestOnGuard(t *testing.T) {
require.Equal(t, err, err2)
require.Equal(t, "hello", err.Error())
}
func TestOnGuardWithContext(t *testing.T) {
type contextKeyTest string

OnGuardWithContext = func(ctx context.Context, r any) {
require.Equal(t, "val", ctx.Value(contextKeyTest("key")))
}
defer func() {
OnGuardWithContext = nil
}()
fn := func(ctx context.Context) (err error) {
defer Guard(&err, WithContext(ctx))
panic(errors.New("GuardWithContext"))
}
_ = fn(context.WithValue(context.TODO(), contextKeyTest("key"), "val"))
}

func TestGuardNotError(t *testing.T) {
var err error
func() {
defer Guard(&err)
panic("hello")
panic(errors.New("hello"))
}()
require.Error(t, err)
require.Equal(t, "panic: hello", err.Error())
require.Equal(t, "hello", err.Error())
}

func TestMust0(t *testing.T) {
Expand Down

0 comments on commit b71d7cf

Please sign in to comment.