Use errors.Is instead of checking underlying err messages (#5140)

* Use errors.Is instead of checking underlying err messages

Fixes #5132

This PR replaces the usage of `strings.hasSuffix` with `errors.Is`
wherever error messages are being checked. So, that the code is not
effected by changes in the underlying message. Also adds a string
const for http2 response body closed error

Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
This commit is contained in:
Tarun Pothulapati 2020-10-28 21:33:17 +05:30 committed by GitHub
parent dd42da343f
commit 3a16baa141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 8 deletions

View File

@ -435,7 +435,7 @@ func recvEvents(tapByteStream *bufio.Reader, eventCh chan<- *pb.TapEvent, closin
if err != nil {
if err == io.EOF {
fmt.Println("Tap stream terminated")
} else if !strings.HasSuffix(err.Error(), "http2: response body closed") {
} else if !strings.HasSuffix(err.Error(), tap.ErrClosedResponseBody) {
fmt.Println(err.Error())
}

View File

@ -3,8 +3,10 @@ package profiles
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"os"
"sort"
"strings"
@ -83,10 +85,11 @@ func routeSpecFromTap(tapByteStream *bufio.Reader, routeLimit int) []*sp.RouteSp
err := protohttp.FromByteStreamToProtocolBuffers(tapByteStream, &event)
if err != nil {
// expected errors when hitting the tapDuration deadline
var e net.Error
if err != io.EOF &&
!strings.HasSuffix(err.Error(), "(Client.Timeout or context cancellation while reading body)") &&
!strings.HasSuffix(err.Error(), "context deadline exceeded") &&
!strings.HasSuffix(err.Error(), "http2: response body closed") {
!(errors.As(err, &e) && e.Timeout()) &&
!errors.Is(err, context.DeadlineExceeded) &&
!strings.HasSuffix(err.Error(), tap.ErrClosedResponseBody) {
fmt.Fprintln(os.Stderr, err)
}
break

View File

@ -131,14 +131,14 @@ func deserializePayloadFromReader(reader *bufio.Reader) ([]byte, error) {
messageLengthAsBytes := make([]byte, numBytesForMessageLength)
_, err := io.ReadFull(reader, messageLengthAsBytes)
if err != nil {
return nil, fmt.Errorf("error while reading message length: %v", err)
return nil, fmt.Errorf("error while reading message length: %w", err)
}
messageLength := int(binary.LittleEndian.Uint32(messageLengthAsBytes))
messageContentsAsBytes := make([]byte, messageLength)
_, err = io.ReadFull(reader, messageContentsAsBytes)
if err != nil {
return nil, fmt.Errorf("error while reading bytes from message: %v", err)
return nil, fmt.Errorf("error while reading bytes from message: %w", err)
}
return messageContentsAsBytes, nil
@ -190,12 +190,12 @@ func CheckIfResponseHasError(rsp *http.Response) error {
func FromByteStreamToProtocolBuffers(byteStreamContainingMessage *bufio.Reader, out proto.Message) error {
messageAsBytes, err := deserializePayloadFromReader(byteStreamContainingMessage)
if err != nil {
return fmt.Errorf("error reading byte stream header: %v", err)
return fmt.Errorf("error reading byte stream header: %w", err)
}
err = proto.Unmarshal(messageAsBytes, out)
if err != nil {
return fmt.Errorf("error unmarshalling array of [%d] bytes error: %v", len(messageAsBytes), err)
return fmt.Errorf("error unmarshalling array of [%d] bytes error: %w", len(messageAsBytes), err)
}
return nil

View File

@ -16,6 +16,11 @@ import (
log "github.com/sirupsen/logrus"
)
const (
// ErrClosedResponseBody is returned when response body is closed in http2
ErrClosedResponseBody = "http2: response body closed"
)
// TapRbacURL is the link users should visit to remedy issues when attempting
// to tap resources with missing authorizations
const TapRbacURL = "https://linkerd.io/tap-rbac"