Skip to content
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

How to modify the returned metadata #7125

Open
baozi-2019 opened this issue Apr 14, 2024 · 3 comments
Open

How to modify the returned metadata #7125

baozi-2019 opened this issue Apr 14, 2024 · 3 comments
Assignees

Comments

@baozi-2019
Copy link

I want to add error message to returned metadata,but it does not work.This is my code.

func ErrorServerInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
	resp, err = handler(ctx, req)
	if err != nil {
		data := make(map[string]string)
		data["exceptionResult"] = err.Error()
		errorMessage := &grpc_message.ErrorMessage{
			ServiceName: ServiceName,
			Reason:      err.Error(),
			Metadata:    data,
		}
		//incomingContext, _ := metadata.FromIncomingContext(ctx)
		bytes, _ := json.Marshal(errorMessage)
		md, ok := metadata.FromOutgoingContext(ctx)
		if !ok {
			md = metadata.New(map[string]string{})
		}
		md.Append("custom-key", string(bytes))
		ctx = metadata.NewOutgoingContext(ctx, md)
		return nil, nil
	}
	return resp, err
}
@aranjans aranjans self-assigned this Apr 17, 2024
@aranjans aranjans removed their assignment Apr 30, 2024
@purnesh42H
Copy link
Collaborator

@aranjans you can assign it to me

@purnesh42H
Copy link
Collaborator

purnesh42H commented Apr 30, 2024

@baozi-2019 could you provide more context on why you are not returning the error (returning nil) from your interceptor?

@purnesh42H
Copy link
Collaborator

@baozi-2019 you need to return a non-nil error in order to add the error message to the metadata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants