Code Monkey home page Code Monkey logo

Comments (6)

jjeffcaii avatar jjeffcaii commented on June 9, 2024

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.

jjeffcaii avatar jjeffcaii commented on June 9, 2024

@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.

Codebreaker101 avatar Codebreaker101 commented on June 9, 2024

Excellent work!

If you accept donations I would be happy to contribute.

from rsocket-go.

Codebreaker101 avatar Codebreaker101 commented on June 9, 2024

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.

jjeffcaii avatar jjeffcaii commented on June 9, 2024

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.

Codebreaker101 avatar Codebreaker101 commented on June 9, 2024

That could work. Thank you!

from rsocket-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.