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

Implement server OnError() hook #46

Merged
merged 1 commit into from
Sep 28, 2023
Merged

Implement server OnError() hook #46

merged 1 commit into from
Sep 28, 2023

Conversation

VojtechVitek
Copy link
Contributor

@VojtechVitek VojtechVitek commented Sep 28, 2023

Example:

rpcServer := proto.NewVideoServer(app.Rpc)

rpcServer.OnError = func(r *http.Request, err *proto.WebRPCError) {
	ctx := r.Context()

	// Log error with request log
	httplog.LogEntrySetField(ctx context.Context, "webrpcError", err.Error())

	// Report crucial DB errors to Sentry
	var pgErr *pgconn.PgError
	if errors.As(err, &pgErr) {
		// https://www.postgresql.org/docs/16/errcodes-appendix.html
		if strings.HasPrefix(pgErr.Code, "42") {
			sentry.CaptureException(err)
		}
	}

	// Add requestID to error message itself.
	rpcErr.Message = fmt.Sprintf("%s (requestID: %v)", rpcErr.Message, middleware.GetReqID(ctx))

	// Hide error details from users in production.
	if app.Config.Environment.IsProduction() {
		rpcErr.Cause = ""
	}
}

(supersedes #40)

@VojtechVitek VojtechVitek force-pushed the on_error_hook branch 2 times, most recently from fbee6e0 to c070bdf Compare September 28, 2023 13:29
Example:

rpcServer := proto.NewVideoServer(app.Rpc)

rpcServer.OnError = func(r *http.Request, err *proto.WebRPCError) {
	ctx := r.Context()

	// Log error with request log
	httplog.LogEntrySetField(ctx context.Context, "webrpcError", err.Error())

	// Report crucial DB errors to Sentry
	var pgErr *pgconn.PgError
	if errors.As(err, &pgErr) {
		// https://www.postgresql.org/docs/16/errcodes-appendix.html
		if strings.HasPrefix(pgErr.Code, "42") {
			sentry.CaptureException(err)
		}
	}

	// Add requestID to error message itself.
	rpcErr.Message = fmt.Sprintf("%s (requestID: %v)", rpcErr.Message, middleware.GetReqID(ctx))

	// Hide error details from users in production.
	if app.Config.Environment.IsProduction() {
		rpcErr.Cause = ""
	}
}
Copy link
Contributor

@LukasJenicek LukasJenicek left a comment

Choose a reason for hiding this comment

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

Looks good :)

@VojtechVitek
Copy link
Contributor Author

Peter Kieltyka  [5:48 PM]
i think this is good too :slightly_smiling_face: because at least we can get the service and method from the context .. the downside by not using a standard middleware is you don’t get to use middleware going back up the middleware stack, it breaks that pattern

@VojtechVitek VojtechVitek merged commit 979bdb8 into master Sep 28, 2023
1 check passed
@VojtechVitek VojtechVitek deleted the on_error_hook branch September 28, 2023 16:28
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.

2 participants