-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructing an error #55
Comments
Some Error Codes in RSocket protocol are meaningful. We usually use |
ISSUE #55: support user defined error.
@Codebreaker101 I've change some codes to support custom error. Now you just need implement If you have any problems, feel free to ask me. 😃 |
Excellent work! If you accept donations I would be happy to contribute. |
Now that we have custom error codes, I want to pretty print the error code, just like it is done in the 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)
} |
I think that because the function type customError struct {
code ErrorCode
data []byte
}
func (err customErr) Is(code ErrorCode) bool {
return err.code == code
} |
That could work. Thank you! |
First of all, thank you for implementing the ability to deconstruct the error.
Now, I wish to add custom error codes into a service. For example: request validation errors, invalid authentication token error, missing tracing ID error.
The
rsocket
protocol states the following:[Error Code] Values in the range of 0x00301 to 0xFFFFFFFE are reserved for application layer errors
.With the current version (
v0.5.10
), the only way to pass custom error codes is with a custom error object serialized into an error string passed to amono
,flux
andsink
Error(err error)
function or returned from theAcceptor
function. This to me seems like a bad way of doing it, and I was wondering is there a possibility of implementing a way of specifying error code alongside the message?From what I can see, with
writeError
function almost everything is in place for this to be supported and all we need is an interface to construct the new frame error. Something along the lines of:The text was updated successfully, but these errors were encountered: