Skip to content

Commit

Permalink
feat: catch different exception types
Browse files Browse the repository at this point in the history
  • Loading branch information
wst24365888 committed Jan 12, 2022
1 parent b3c8ed2 commit f145a7b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 32 deletions.
25 changes: 25 additions & 0 deletions .examples/catch_on_different_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

. "github.com/ez4o/go-try"
)

func main() {
Try(func() {
ThrowOnError(1)
}).Catch(func(s string) {
fmt.Println("This is a string exception:", s)
}).Catch(func(i int) {
fmt.Println("This is an int exception:", i)
})

Try(func() {
ThrowOnError("1")
}).Catch(func(s string) {
fmt.Println("This is a string exception:", s)
}).Catch(func(i int) {
fmt.Println("This is an int exception:", i)
})
}
6 changes: 3 additions & 3 deletions .examples/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func main() {
ThrowOnError(err)

fmt.Println(data)
}).Catch(func(e *Exception) {
fmt.Println(e.Error)
e.PrintStackTrace()
}).Catch(func(e error, st *StackTrace) {
fmt.Println(e)
st.Print()
})
}
42 changes: 35 additions & 7 deletions catch.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
package try

type Handler struct {
e *Exception
}
import (
"reflect"
)

func (e *Exception) Catch(f any) *Exception {
fVal := reflect.ValueOf(f)
fType := fVal.Type()

if fType.NumIn() == 1 {
firstParamType := fType.In(0)
errorType := reflect.TypeOf(e.err)

if errorType.String() == "*errors.errorString" {
var err error
errorType = reflect.TypeOf(&err).Elem()
}

if firstParamType.AssignableTo(errorType) {
fVal.Call([]reflect.Value{reflect.ValueOf(e.err)})
}
} else if fType.NumIn() == 2 {
firstParamType := fType.In(0)
errorType := reflect.TypeOf(e.err)

secondParamType := fType.In(1)
stackTraceType := reflect.TypeOf(e.stackTrace)

if errorType.String() == "*errors.errorString" {
var err error
errorType = reflect.TypeOf(&err).Elem()
}

func (h *Handler) Catch(f func(*Exception)) {
if h == nil {
return
if firstParamType.AssignableTo(errorType) && secondParamType.ConvertibleTo(stackTraceType) {
fVal.Call([]reflect.Value{reflect.ValueOf(e.err), reflect.ValueOf(e.stackTrace)})
}
}

f(h.e)
return e
}
16 changes: 2 additions & 14 deletions exception.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
package try

import "fmt"

type Exception struct {
Error any
stackTrace []StackInfo
}

func (e *Exception) PrintStackTrace() {
stackTrace := ""

for _, s := range e.stackTrace {
stackTrace += s.String()
}

fmt.Println(stackTrace[:len(stackTrace)-1])
err any
stackTrace *StackTrace
}
17 changes: 17 additions & 0 deletions stack_trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package try

import "fmt"

type StackTrace struct {
stackInfos []StackInfo
}

func (st *StackTrace) Print() {
stackTrace := ""

for _, si := range st.stackInfos {
stackTrace += si.String()
}

fmt.Println(stackTrace[:len(stackTrace)-1])
}
16 changes: 8 additions & 8 deletions try.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"runtime"
)

func getStackTrace() []StackInfo {
stackTrace := make([]StackInfo, 0)
func getStackTrace() *StackTrace {
stackInfos := make([]StackInfo, 0)

// Skip caller [0, 1, 2, 3, 5, 6].
for i := 4; ; i++ {
Expand All @@ -18,24 +18,24 @@ func getStackTrace() []StackInfo {
break
}

stackTrace = append(stackTrace, StackInfo{pc, file, line})
stackInfos = append(stackInfos, StackInfo{pc, file, line})
}

return stackTrace[:len(stackTrace)-1]
return &StackTrace{stackInfos}
}

func Try(f func()) *Handler {
var h *Handler
func Try(f func()) *Exception {
var e *Exception

func() {
defer func() {
if r := recover(); r != nil {
h = &Handler{&Exception{r, getStackTrace()}}
e = &Exception{r, getStackTrace()}
}
}()

f()
}()

return h
return e
}

0 comments on commit f145a7b

Please sign in to comment.