mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-16 04:04:56 +02:00
Use errors.Is/errors.As instead of == comparisons against errors
This commit is contained in:
parent
4756698b1e
commit
95b3fbfc6d
3 changed files with 8 additions and 4 deletions
2
conn.go
2
conn.go
|
|
@ -190,7 +190,7 @@ func (c *Conn) close(cause error) error {
|
||||||
close(call.done)
|
close(call.done)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cause != nil && cause != io.EOF && cause != io.ErrUnexpectedEOF {
|
if cause != nil && !errors.Is(cause, io.EOF) && !errors.Is(cause, io.ErrUnexpectedEOF) {
|
||||||
c.logger.Printf("jsonrpc2: protocol error: %v\n", cause)
|
c.logger.Printf("jsonrpc2: protocol error: %v\n", cause)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package jsonrpc2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HandlerWithError implements Handler by calling the func for each
|
// HandlerWithError implements Handler by calling the func for each
|
||||||
|
|
@ -31,14 +32,15 @@ func (h *HandlerWithErrorConfigurer) Handle(ctx context.Context, conn *Conn, req
|
||||||
err = resp.SetResult(result)
|
err = resp.SetResult(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
if e, ok := err.(*Error); ok {
|
var e *Error
|
||||||
|
if errors.As(err, &e) {
|
||||||
resp.Error = e
|
resp.Error = e
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
resp.Error = &Error{Message: err.Error()}
|
resp.Error = &Error{Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = conn.SendResponse(ctx, resp)
|
err = conn.SendResponse(ctx, resp)
|
||||||
if err != nil && (err != ErrClosed || !h.suppressErrClosed) {
|
if err != nil && (!errors.Is(err, ErrClosed) || !h.suppressErrClosed) {
|
||||||
conn.logger.Printf("jsonrpc2 handler: sending response %s: %v\n", resp.ID, err)
|
conn.logger.Printf("jsonrpc2 handler: sending response %s: %v\n", resp.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
ws "github.com/gorilla/websocket"
|
ws "github.com/gorilla/websocket"
|
||||||
|
|
@ -28,7 +29,8 @@ func (t ObjectStream) WriteObject(obj interface{}) error {
|
||||||
// ReadObject implements jsonrpc2.ObjectStream.
|
// ReadObject implements jsonrpc2.ObjectStream.
|
||||||
func (t ObjectStream) ReadObject(v interface{}) error {
|
func (t ObjectStream) ReadObject(v interface{}) error {
|
||||||
err := t.conn.ReadJSON(v)
|
err := t.conn.ReadJSON(v)
|
||||||
if e, ok := err.(*ws.CloseError); ok {
|
var e *ws.CloseError
|
||||||
|
if errors.As(err, &e) {
|
||||||
if e.Code == ws.CloseAbnormalClosure && e.Text == io.ErrUnexpectedEOF.Error() {
|
if e.Code == ws.CloseAbnormalClosure && e.Text == io.ErrUnexpectedEOF.Error() {
|
||||||
// Suppress a noisy (but harmless) log message by
|
// Suppress a noisy (but harmless) log message by
|
||||||
// unwrapping this error.
|
// unwrapping this error.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue