Skip to content

Commit

Permalink
Implement server OnError() hook (#46)
Browse files Browse the repository at this point in the history
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 = ""
	}
}
  • Loading branch information
VojtechVitek authored Sep 28, 2023
1 parent a5f5621 commit 979bdb8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 40 deletions.
78 changes: 57 additions & 21 deletions _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 35 additions & 10 deletions _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 979bdb8

Please sign in to comment.