Skip to content

Commit

Permalink
Implement server OnError() hook
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 committed Sep 28, 2023
1 parent dd5f914 commit 50a4cc3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 41 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.

47 changes: 37 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.

2 changes: 1 addition & 1 deletion errors.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError {
return rpcErr.WithCause(cause)
}

{{- if $opts.errorStackTrace -}}
{{if $opts.errorStackTrace -}}
func (e WebRPCError) StackFrames() []uintptr {
return e.frame.frames[:]
}
Expand Down
Loading

0 comments on commit 50a4cc3

Please sign in to comment.