github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/error.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileservice
    16  
    17  import (
    18  	"errors"
    19  	"io"
    20  	"strings"
    21  )
    22  
    23  func IsRetryableError(err error) bool {
    24  	// Is error
    25  	if errors.Is(err, io.ErrUnexpectedEOF) {
    26  		return true
    27  	}
    28  	str := err.Error()
    29  	// match exact string
    30  	switch str {
    31  	case "connection reset by peer",
    32  		"connection timed out":
    33  		return true
    34  	}
    35  	// match sub-string
    36  	if strings.Contains(str, "unexpected EOF") ||
    37  		strings.Contains(str, "connection reset by peer") ||
    38  		strings.Contains(str, "connection timed out") ||
    39  		strings.Contains(str, "dial tcp: lookup") ||
    40  		strings.Contains(str, "write: broken pipe") ||
    41  		strings.Contains(str, "use of closed network connection") {
    42  		return true
    43  	}
    44  	return false
    45  }
    46  
    47  type errorStr string
    48  
    49  func (e errorStr) Error() string {
    50  	return string(e)
    51  }
    52  
    53  type throwError struct {
    54  	err error
    55  }
    56  
    57  func throw(err error) {
    58  	panic(throwError{
    59  		err: err,
    60  	})
    61  }
    62  
    63  func catch(ptr *error) {
    64  	p := recover()
    65  	if p == nil {
    66  		return
    67  	}
    68  	e, ok := p.(throwError)
    69  	if !ok {
    70  		panic(p)
    71  	} else {
    72  		*ptr = e.err
    73  	}
    74  }