Comments (6)
Some Error Codes in RSocket protocol are meaningful. We usually use APPLICATION_ERROR
for most of cases.
But I think that we can support those Error structs which implement rsocket.Error
.
from rsocket-go.
@Codebreaker101 I've change some codes to support custom error. Now you just need implement rsocket.Error
to create your own error struct.
If you have any problems, feel free to ask me. 😃
from rsocket-go.
Excellent work!
If you accept donations I would be happy to contribute.
from rsocket-go.
Now that we have custom error codes, I want to pretty print the error code, just like it is done in the
internal/common
package as well as compare error codes. Since the interface demands the use of rsocket.ErrorCode
as a return type of ErrorCode
, I am forced to use internal ErrorCode implementation.
Do you perhaps have any input on how to improve the struggles of the example code below (check comments)?
package errors_test
import (
"fmt"
"log"
"testing"
"github.com/rsocket/rsocket-go"
)
type (
ErrorCode uint32
Error interface {
rsocket.Error
}
)
func (c ErrorCode) String() string {
switch c {
case ErrCodeMissingMetadata:
return "MISSING_METADATA"
default:
return rsocket.ErrorCode(c).String()
}
}
const (
ErrCodeMissingMetadata ErrorCode = 0xA0000001
)
func New(code ErrorCode, data []byte) Error {
return &customError{
code: code,
data: data,
}
}
type customError struct {
code ErrorCode
data []byte
}
func (err *customError) Error() string {
return fmt.Sprintf("%#x: %s", uint32(err.code), err.code)
}
func (err *customError) ErrorCode() rsocket.ErrorCode {
// need to convert type, therefore losing `String()` implementation
return rsocket.ErrorCode(err.code)
}
func (err *customError) ErrorData() []byte {
return err.data
}
func TestCustomError(t *testing.T) {
err := New(ErrCodeMissingMetadata, []byte("MyErrorData"))
log.Println(err.Error())
// logs "UNKNOWN"/ expected "MISSING_METADATA"
log.Println(err.ErrorCode())
log.Println(string(err.ErrorData()))
// comparison is harder
log.Println(ErrorCode(err.ErrorCode()) == ErrCodeMissingMetadata)
}
from rsocket-go.
I think that because the function ErrorCode
returns the rsocket.ErrorCode
instead of your custom ErrorCode
and it call method of rsocket.ErrorCode#String
.
Maybe you can and a function like this:
type customError struct {
code ErrorCode
data []byte
}
func (err customErr) Is(code ErrorCode) bool {
return err.code == code
}
from rsocket-go.
That could work. Thank you!
from rsocket-go.
Related Issues (20)
- 关于NewRoundRobinBalancer的疑问 HOT 1
- 开启租约和fragmet=4096的限制后 HOT 6
- client connect: rejected setup HOT 1
- Let user specify a Proxy func for NewWebsocketClientTransport HOT 2
- Word Counter example hangs indefinitely HOT 2
- RSocket mono failure. HOT 8
- RequestStream with timeout context doesn't work HOT 2
- Mistake
- Compile failed on ARM platform. HOT 2
- When calling request-channel from javascript SDK, program got a panic with "cannot cancel". HOT 3
- requestChannel cancel panic HOT 1
- Build failure on 1.18 HOT 1
- Calling payload.Data() after BlockSlice() panics with `frame has been released!` HOT 4
- unexport type how to implement HOT 2
- Performance tuning HOT 1
- /go/pkg/mod/github.com/rsocket/[email protected]/rx/subscriber.go:12:60: cannot use func literal (type func(interface {})) as type func(interface {}) error in argument to reactor.OnNext HOT 2
- how to use it in go mod? HOT 4
- Add merge function for Flux
- Access rsocket Serve context from within Acceptor HOT 2
- rpc pool HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rsocket-go.