-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40c94da
commit b1eeafc
Showing
72 changed files
with
3,253 additions
and
185 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,56 @@ | ||
# Minimum golangci-lint version required: v1.42.0 | ||
run: | ||
timeout: 3m | ||
skip-dirs: | ||
- tmp | ||
|
||
linters-settings: | ||
gci: | ||
local-prefixes: github.com/foomo/keel | ||
revive: | ||
rules: | ||
- name: indent-error-flow | ||
disabled: true | ||
gocritic: | ||
enabled-tags: | ||
- diagnostic | ||
- performance | ||
- style | ||
disabled-tags: | ||
- experimental | ||
- opinionated | ||
disabled-checks: | ||
- ifElseChain | ||
settings: | ||
hugeParam: | ||
sizeThreshold: 512 | ||
|
||
linters: | ||
enable: | ||
- bodyclose | ||
- exhaustive | ||
- dogsled | ||
- dupl | ||
- exportloopref | ||
- goconst | ||
- gocritic | ||
- gocyclo | ||
- gofmt | ||
- goprintffuncname | ||
- gosec | ||
- ifshort | ||
- misspell | ||
- nakedret | ||
- noctx | ||
- nolintlint | ||
- prealloc | ||
- revive | ||
- promlinter | ||
- rowserrcheck | ||
- sqlclosecheck | ||
- stylecheck | ||
- thelper | ||
- tparallel | ||
- unconvert | ||
- unparam | ||
- whitespace |
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package gotsrpc | ||
|
||
type ClientError struct { | ||
error | ||
} | ||
|
||
func NewClientError(err error) *ClientError { | ||
return &ClientError{ | ||
error: err, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package gotsrpc | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Error struct { | ||
Msg string `json:"m"` | ||
Pkg string `json:"p"` | ||
Type string `json:"t"` | ||
Data interface{} `json:"d,omitempty"` | ||
ErrCause *Error `json:"c,omitempty"` | ||
} | ||
|
||
// NewError returns a new instance | ||
func NewError(err error) *Error { | ||
// check if already transformed | ||
if v, ok := err.(*Error); ok { | ||
return v | ||
} | ||
|
||
// skip *withStack error type | ||
if _, ok := err.(interface { | ||
StackTrace() errors.StackTrace | ||
}); ok && errors.Unwrap(err) != nil { | ||
err = errors.Unwrap(err) | ||
} | ||
|
||
// retrieve error details | ||
errType := reflect.TypeOf(err) | ||
|
||
inst := &Error{ | ||
Msg: err.Error(), | ||
Type: errType.String(), | ||
Pkg: errType.Elem().PkgPath(), | ||
Data: err, | ||
} | ||
|
||
// unwrap error | ||
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil { | ||
inst.ErrCause = NewError(unwrappedErr) | ||
inst.Msg = strings.TrimSuffix(inst.Msg, ": "+unwrappedErr.Error()) | ||
} | ||
|
||
return inst | ||
} | ||
|
||
// As interface | ||
func (e *Error) As(err interface{}) bool { | ||
if e == nil || err == nil { | ||
return false | ||
} | ||
if reflect.TypeOf(err).Elem().String() == e.Type { | ||
if decodeErr := mapstructure.Decode(e.Data, &err); decodeErr != nil { | ||
fmt.Printf("ERROR: failed to decode error data\n%+v", decodeErr) | ||
return false | ||
} else { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Cause interface | ||
func (e *Error) Cause() error { | ||
if e.ErrCause != nil { | ||
return e.ErrCause | ||
} | ||
return e | ||
} | ||
|
||
// Format interface | ||
func (e *Error) Format(s fmt.State, verb rune) { | ||
switch verb { | ||
case 'v': | ||
if s.Flag('+') { | ||
fmt.Fprintf(s, "%s.(%s)\n", e.Pkg, e.Type) | ||
if e.Data != nil { | ||
fmt.Fprintf(s, "Data: %v\n", e.Data) | ||
} | ||
} | ||
fallthrough | ||
case 's', 'q': | ||
io.WriteString(s, e.Error()) | ||
} | ||
} | ||
|
||
// Unwrap interface | ||
func (e *Error) Unwrap() error { | ||
if e != nil && e.ErrCause != nil { | ||
return e.ErrCause | ||
} | ||
return nil | ||
} | ||
|
||
// Is interface | ||
func (e *Error) Is(err error) bool { | ||
if e == nil || err == nil { | ||
return false | ||
} | ||
|
||
errType := reflect.TypeOf(err) | ||
|
||
if e.Msg == err.Error() && | ||
errType.String() == e.Type && | ||
errType.Elem().PkgPath() == e.Pkg { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// Error interface | ||
func (e *Error) Error() string { | ||
msg := e.Msg | ||
if e.ErrCause != nil { | ||
msg += ": " + e.ErrCause.Error() | ||
} | ||
return msg | ||
} |
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
Oops, something went wrong.