Skip to content

Commit

Permalink
- Add case to handle error interface ✨🐛
Browse files Browse the repository at this point in the history
- Ignore specific folders for editor configurations 🙈
  • Loading branch information
fairyhunter13 committed Jul 22, 2022
1 parent 269aa6d commit bb15260
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
# Dependency directories (remove the comment below to include it)
# vendor/
test/

# Editor Configs
.vscode
.idea
6 changes: 6 additions & 0 deletions caster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ func castString(val interface{}) (result string, err error) {
if err != nil {
return
}

if errType, ok := val.(error); ok {
result = errType.Error()
return
}

result, err = cast.String(val)
return
}
Expand Down
9 changes: 9 additions & 0 deletions extractor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reflecthelper

import (
"errors"
"net"
"net/url"
"reflect"
Expand Down Expand Up @@ -895,6 +896,14 @@ func TestExtractString(t *testing.T) {
wantResult: "hello",
wantErr: false,
},
{
name: "error interface",
args: args{
val: reflect.ValueOf(errors.New("test this error")),
},
wantResult: "test this error",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit bb15260

Please sign in to comment.