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

custom error to code handler #1330

Merged
merged 4 commits into from
Nov 30, 2022
Merged

custom error to code handler #1330

merged 4 commits into from
Nov 30, 2022

Conversation

pjain1
Copy link
Member

@pjain1 pjain1 commented Nov 30, 2022

  • Context cancelled err mapped to codes.Canceled which is logged as Info
  • All status error codes are used as it is and logged as per DefaultServerCodeToLevel func in options.go file
  • Removed panic from RegistryStore method implementation to handle context cancelled

Copy link
Contributor

@begelundmuller begelundmuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of returning three values like this:

FindInstance(ctx context.Context, id string) (*Instance, bool, error)

we should return two values like this:

FindInstance(ctx context.Context, id string) (*Instance, error)

and use drivers.ErrNotFound as a constant error for when the value wasn't found. Then from the caller, instead of doing:

 	inst, found, err := registry.FindInstance(ctx, req.InstanceId)
 	if err != nil {
 		return nil, err
 	}
 	if !found {
 		return nil, status.Error(codes.InvalidArgument, "instance not found")
 	}

we would do:

 	inst, err := registry.FindInstance(ctx, req.InstanceId)
 	if err != nil {
 	 	if err == drivers.ErrNotFound {
 	 	 	return nil, status.Error(codes.InvalidArgument, "instance not found")
 	 	}
 	 	return nil, err
 	}

Comment on lines +92 to +103
// CustomErrorToCode returns the Code of the error if it is a Status error
// otherwise use status.FromContextError to determine the Code.
// Log level for error codes is defined in logging.DefaultServerCodeToLevel
func CustomErrorToCode(err error) codes.Code {
if se, ok := err.(interface {
GRPCStatus() *status.Status
}); ok {
return se.GRPCStatus().Code()
}
contextStatus := status.FromContextError(err)
return contextStatus.Code()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe drop the Custom and just call this ErrorToCode?

Also, do you think we should override logging.DefaultServerCodeToLevel and only emit codes.Internal as ERROR and everything else as info (or maybe some others as warning)?

Copy link
Member Author

@pjain1 pjain1 Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its ok for now since apart from codes.Internal

codes.Unknown, codes.Unimplemented, codes.DataLoss

gets logged as error which seems valid, may be we can wait and change later if needed. Anyways no strong opinions here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new queries refactor, there will probably be more "unknown" errors returned, so would be nice to make those "info" messages instead

@pjain1
Copy link
Member Author

pjain1 commented Nov 30, 2022

changed FindInstance to return two values

@begelundmuller begelundmuller merged commit f0908b5 into main Nov 30, 2022
@begelundmuller begelundmuller deleted the custom_error_to_code branch November 30, 2022 16:23
djbarnwal pushed a commit that referenced this pull request Aug 3, 2023
* custom error to code handler

* comment

* fix test

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

Successfully merging this pull request may close these issues.

Runtime server: log cancelled requests as info messages instead of error messages
2 participants