go-micro version: 1.15
go version: 1.12
os: devian
My grpc server returns a custom error, for example
type error struct{
code int
customField int
}
In my client, the error received is the go-micro error instead of my custom error. Only the shared fields are mapped (for example the code field).
I would like to have my custom error embebed into the go-micro error or receive my custom error instead
example:
client.go
resp, extErr := p.service.Say(ctx, request)
if extErr != nil {
return nil, extErr.(sharedRepo.(CustomError))
}
return resp, nil
server.go
func (f *Handler) Say(ctx context.Context, req *proto.SayRequest, rsp *proto.SayResposnse) sharedRepo.CustomError {
return sharedRepo.CustomError{}
}
This conversion fails when it should not
return nil, extErr.(sharedRepo.(CustomError))
You error must be compatible with proto.Message , if this will be possible i can transfer it to client .
Does this looks good to you?
what do you mean by compatible with proto.message? If this means that I should create a proto message for my error then yes, it's fine because I can write a function that encodes and decodes my error into a proto.message
if this gets fixed, the fix will be in the lastes release of go-micro? I'am using go-micro 1.15 because the newer versions require go 1.13 and I am stuck with go 1.12 for the moment . I can update my project to go 1.13 but that will require several changes to my project (I already tried to do the update and decided to rollback because of some issues)
this fix goes to go-micro master, so you need go 1.13
try pr #1167 in my testing if you pass proto based struct as error it correctly encoded to grpc Status Detail and returned to client
Wow! thanks for the quick update. I reviewed the pr but I'am still not sure of what you mean by passing an error that is proto based.
Lets say I have an struct that is called customError which satisfies the error interface of golang
type CustomError struct{
Code int
A int
B int
...
}
If I return this CustomError struct in the server, and in the client I do err.(*CustomError) will it be correctly casted?
Should I define anything in the .proto file?
you should define you error type in proto file like
message MyError {
int code = 1;
int a = 2;
int b = 3;
}
after that generate go code from proto definition.
next step add in the same dir utili function to satisfy error interface (Error() method)
after that you can use &MyError{} as error and it goes to you client from you server
Ok! I will try to update to go.13 and then try this out. Thanks again!
confirmed in slack pm that this is fixed in master
Most helpful comment
you should define you error type in proto file like
message MyError {
int code = 1;
int a = 2;
int b = 3;
}
after that generate go code from proto definition.
next step add in the same dir utili function to satisfy error interface (Error() method)
after that you can use &MyError{} as error and it goes to you client from you server