-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: catch different exception types
- Loading branch information
1 parent
b3c8ed2
commit f145a7b
Showing
6 changed files
with
90 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters