mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-21 02:08:19 +02:00
add support for suppressing ErrClosed in HandlerWithError logs
This commit is contained in:
parent
c6c7b9aa99
commit
c9e82539c3
1 changed files with 29 additions and 4 deletions
|
|
@ -7,11 +7,18 @@ import (
|
||||||
|
|
||||||
// HandlerWithError implements Handler by calling the func for each
|
// HandlerWithError implements Handler by calling the func for each
|
||||||
// request and handling returned errors and results.
|
// request and handling returned errors and results.
|
||||||
type HandlerWithError func(context.Context, *Conn, *Request) (result interface{}, err error)
|
func HandlerWithError(handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error)) *HandlerWithErrorConfigurer {
|
||||||
|
return &HandlerWithErrorConfigurer{handleFunc: handleFunc}
|
||||||
|
}
|
||||||
|
|
||||||
|
type HandlerWithErrorConfigurer struct {
|
||||||
|
handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error)
|
||||||
|
suppressErrClosed bool
|
||||||
|
}
|
||||||
|
|
||||||
// Handle implements Handler.
|
// Handle implements Handler.
|
||||||
func (h HandlerWithError) Handle(ctx context.Context, conn *Conn, req *Request) {
|
func (h *HandlerWithErrorConfigurer) Handle(ctx context.Context, conn *Conn, req *Request) {
|
||||||
result, err := h(ctx, conn, req)
|
result, err := h.handleFunc(ctx, conn, req)
|
||||||
if req.Notif {
|
if req.Notif {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("jsonrpc2 handler: notification %q handling error: %s", req.Method, err)
|
log.Printf("jsonrpc2 handler: notification %q handling error: %s", req.Method, err)
|
||||||
|
|
@ -33,7 +40,25 @@ func (h HandlerWithError) Handle(ctx context.Context, conn *Conn, req *Request)
|
||||||
|
|
||||||
if !req.Notif {
|
if !req.Notif {
|
||||||
if err := conn.SendResponse(ctx, resp); err != nil {
|
if err := conn.SendResponse(ctx, resp); err != nil {
|
||||||
log.Printf("jsonrpc2 handler: sending response %s: %s", resp.ID, err)
|
if err != ErrClosed || !h.suppressErrClosed {
|
||||||
|
log.Printf("jsonrpc2 handler: sending response %s: %s", resp.ID, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SuppressErrClosed makes the handler suppress jsonrpc2.ErrClosed errors from
|
||||||
|
// being logged. The original handler `h` is returned.
|
||||||
|
//
|
||||||
|
// This is optional because only in some cases is this behavior desired. For
|
||||||
|
// example, a handler that serves end-user connections may not want to log
|
||||||
|
// ErrClosed because it just indicates the end-user connection has gone away
|
||||||
|
// for any reason (they could have lost wifi connection, are no longer
|
||||||
|
// interested in the request and closed the connection, etc) and as such it
|
||||||
|
// would be log spam, whereas a handler that serves internal connections would
|
||||||
|
// never expect connections to go away unexpectedly (which could indicate
|
||||||
|
// service degradation, etc) and as such ErrClosed should always be logged.
|
||||||
|
func (h *HandlerWithErrorConfigurer) SuppressErrClosed() Handler {
|
||||||
|
h.suppressErrClosed = true
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue