2017-05-31 16:29:03 +03:00
|
|
|
// Package errors provides a way to return detailed information
|
|
|
|
// for an RPC request error. The error is normally JSON encoded.
|
2021-10-02 19:55:07 +03:00
|
|
|
package errors // import "go.unistack.org/micro/v3/errors"
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
import (
|
2022-01-25 00:40:50 +03:00
|
|
|
"bytes"
|
2023-10-23 02:35:10 +03:00
|
|
|
"context"
|
2015-01-14 02:31:27 +03:00
|
|
|
"encoding/json"
|
2023-10-23 02:35:10 +03:00
|
|
|
"errors"
|
2017-05-31 16:29:03 +03:00
|
|
|
"fmt"
|
2023-10-23 02:35:10 +03:00
|
|
|
"io"
|
2015-01-14 02:31:27 +03:00
|
|
|
"net/http"
|
2022-01-21 00:50:16 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-10-23 02:35:10 +03:00
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2020-09-28 13:08:53 +03:00
|
|
|
var (
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrBadRequest returns then requests contains invalid data
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrBadRequest = &Error{Code: 400}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrUnauthorized returns then user have unauthorized call
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrUnauthorized = &Error{Code: 401}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrForbidden returns then user have not access the resource
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrForbidden = &Error{Code: 403}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrNotFound returns then user specify invalid endpoint
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrNotFound = &Error{Code: 404}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrMethodNotAllowed returns then user try to get invalid method
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrMethodNotAllowed = &Error{Code: 405}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrTimeout returns then timeout exceeded
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrTimeout = &Error{Code: 408}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrConflict returns then request create duplicate resource
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrConflict = &Error{Code: 409}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrInternalServerError returns then server cant process request because of internal error
|
2020-09-28 13:08:53 +03:00
|
|
|
ErrInternalServerError = &Error{Code: 500}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErNotImplemented returns then server does not have desired endpoint method
|
2020-12-08 00:38:37 +03:00
|
|
|
ErNotImplemented = &Error{Code: 501}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrBadGateway returns then server cant process request
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrBadGateway = &Error{Code: 502}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrServiceUnavailable returns then service unavailable
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrServiceUnavailable = &Error{Code: 503}
|
2021-02-14 11:28:50 +03:00
|
|
|
// ErrGatewayTimeout returns then server have long time to process request
|
2020-12-08 00:38:37 +03:00
|
|
|
ErrGatewayTimeout = &Error{Code: 504}
|
2020-09-28 13:08:53 +03:00
|
|
|
)
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Error type
|
2020-07-19 11:29:48 +03:00
|
|
|
type Error struct {
|
2021-09-30 21:00:02 +03:00
|
|
|
// ID holds error id or service, usually someting like my_service or id
|
|
|
|
ID string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Detail holds some useful details about error
|
2020-07-19 11:29:48 +03:00
|
|
|
Detail string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Status usually holds text of http status
|
2020-07-19 11:29:48 +03:00
|
|
|
Status string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Code holds error code
|
|
|
|
Code int32
|
2020-07-19 11:29:48 +03:00
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Error satisfies error interface
|
2015-01-14 02:31:27 +03:00
|
|
|
func (e *Error) Error() string {
|
|
|
|
b, _ := json.Marshal(e)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2021-12-28 09:18:52 +03:00
|
|
|
/*
|
|
|
|
// Generator struct holds id of error
|
|
|
|
type Generator struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generator can emit new error with static id
|
|
|
|
func NewGenerator(id string) *Generator {
|
|
|
|
return &Generator{id: id}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Generator) BadRequest(format string, args ...interface{}) error {
|
|
|
|
return BadRequest(g.id, format, args...)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-03-06 19:45:13 +03:00
|
|
|
// New generates a custom error
|
2015-01-14 02:31:27 +03:00
|
|
|
func New(id, detail string, code int32) error {
|
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2015-01-14 02:31:27 +03:00
|
|
|
Code: code,
|
|
|
|
Detail: detail,
|
|
|
|
Status: http.StatusText(int(code)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// Parse tries to parse a JSON string into an error. If that
|
|
|
|
// fails, it will set the given string as the error detail.
|
2015-01-14 02:31:27 +03:00
|
|
|
func Parse(err string) *Error {
|
2022-01-21 18:20:56 +03:00
|
|
|
e := &Error{}
|
|
|
|
nerr := json.Unmarshal([]byte(err), e)
|
|
|
|
if nerr != nil {
|
2015-01-14 02:31:27 +03:00
|
|
|
e.Detail = err
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// BadRequest generates a 400 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func BadRequest(id, format string, args ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2015-01-14 02:31:27 +03:00
|
|
|
Code: 400,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(400),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// Unauthorized generates a 401 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func Unauthorized(id, format string, args ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2015-01-14 02:31:27 +03:00
|
|
|
Code: 401,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(401),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// Forbidden generates a 403 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func Forbidden(id, format string, args ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2015-01-14 02:31:27 +03:00
|
|
|
Code: 403,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(403),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:29:03 +03:00
|
|
|
// NotFound generates a 404 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func NotFound(id, format string, args ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2015-01-14 02:31:27 +03:00
|
|
|
Code: 404,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2015-01-14 02:31:27 +03:00
|
|
|
Status: http.StatusText(404),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:42:34 +03:00
|
|
|
// MethodNotAllowed generates a 405 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func MethodNotAllowed(id, format string, args ...interface{}) error {
|
2018-09-12 17:42:34 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2018-09-12 17:42:34 +03:00
|
|
|
Code: 405,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2018-09-12 17:42:34 +03:00
|
|
|
Status: http.StatusText(405),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-25 12:41:28 +03:00
|
|
|
// Timeout generates a 408 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func Timeout(id, format string, args ...interface{}) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2018-11-25 12:41:28 +03:00
|
|
|
Code: 408,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2018-11-25 12:41:28 +03:00
|
|
|
Status: http.StatusText(408),
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-11 16:51:33 +03:00
|
|
|
|
|
|
|
// Conflict generates a 409 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func Conflict(id, format string, args ...interface{}) error {
|
2018-02-11 16:51:33 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2018-02-11 16:51:33 +03:00
|
|
|
Code: 409,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2018-02-11 16:51:33 +03:00
|
|
|
Status: http.StatusText(409),
|
|
|
|
}
|
|
|
|
}
|
2018-11-25 12:41:28 +03:00
|
|
|
|
|
|
|
// InternalServerError generates a 500 error.
|
2021-12-28 09:18:52 +03:00
|
|
|
func InternalServerError(id, format string, args ...interface{}) error {
|
2018-11-25 12:41:28 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2018-11-25 12:41:28 +03:00
|
|
|
Code: 500,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2018-11-25 12:41:28 +03:00
|
|
|
Status: http.StatusText(500),
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 14:27:20 +03:00
|
|
|
|
2020-07-06 22:14:59 +03:00
|
|
|
// NotImplemented generates a 501 error
|
2021-12-28 09:18:52 +03:00
|
|
|
func NotImplemented(id, format string, args ...interface{}) error {
|
2020-07-06 22:14:59 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2020-07-19 11:29:48 +03:00
|
|
|
Code: 501,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2020-07-19 11:29:48 +03:00
|
|
|
Status: http.StatusText(501),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadGateway generates a 502 error
|
2021-12-28 09:18:52 +03:00
|
|
|
func BadGateway(id, format string, args ...interface{}) error {
|
2020-07-06 22:14:59 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2020-07-19 11:29:48 +03:00
|
|
|
Code: 502,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2020-07-19 11:29:48 +03:00
|
|
|
Status: http.StatusText(502),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceUnavailable generates a 503 error
|
2021-12-28 09:18:52 +03:00
|
|
|
func ServiceUnavailable(id, format string, args ...interface{}) error {
|
2020-07-06 22:14:59 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2020-07-19 11:29:48 +03:00
|
|
|
Code: 503,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2020-07-19 11:29:48 +03:00
|
|
|
Status: http.StatusText(503),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GatewayTimeout generates a 504 error
|
2021-12-28 09:18:52 +03:00
|
|
|
func GatewayTimeout(id, format string, args ...interface{}) error {
|
2020-07-06 22:14:59 +03:00
|
|
|
return &Error{
|
2021-09-30 21:00:02 +03:00
|
|
|
ID: id,
|
2020-07-19 11:29:48 +03:00
|
|
|
Code: 504,
|
2021-12-28 09:18:52 +03:00
|
|
|
Detail: fmt.Sprintf(format, args...),
|
2020-07-19 11:29:48 +03:00
|
|
|
Status: http.StatusText(504),
|
2020-07-06 22:14:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:10:38 +03:00
|
|
|
// Equal tries to compare errors
|
2020-03-17 14:27:20 +03:00
|
|
|
func Equal(err1 error, err2 error) bool {
|
|
|
|
verr1, ok1 := err1.(*Error)
|
|
|
|
verr2, ok2 := err2.(*Error)
|
|
|
|
|
|
|
|
if ok1 != ok2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok1 {
|
|
|
|
return err1 == err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if verr1.Code != verr2.Code {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2020-03-18 03:10:38 +03:00
|
|
|
|
2022-04-19 17:12:25 +03:00
|
|
|
// CodeIn return true if err has specified code
|
|
|
|
func CodeIn(err interface{}, codes ...int32) bool {
|
|
|
|
var code int32
|
|
|
|
switch verr := err.(type) {
|
|
|
|
case *Error:
|
|
|
|
code = verr.Code
|
|
|
|
case int32:
|
|
|
|
code = verr
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, check := range codes {
|
|
|
|
if code == check {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:10:38 +03:00
|
|
|
// FromError try to convert go error to *Error
|
|
|
|
func FromError(err error) *Error {
|
|
|
|
if verr, ok := err.(*Error); ok && verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
|
|
|
return Parse(err.Error())
|
|
|
|
}
|
2022-01-21 00:50:16 +03:00
|
|
|
|
|
|
|
// MarshalJSON returns error data
|
|
|
|
func (e *Error) MarshalJSON() ([]byte, error) {
|
|
|
|
return e.Marshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON set error data
|
|
|
|
func (e *Error) UnmarshalJSON(data []byte) error {
|
|
|
|
return e.Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProtoMessage noop func
|
|
|
|
func (e *Error) ProtoMessage() {}
|
|
|
|
|
|
|
|
// Reset resets error
|
|
|
|
func (e *Error) Reset() {
|
|
|
|
*e = Error{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns error as string
|
|
|
|
func (e *Error) String() string {
|
2022-01-25 00:40:50 +03:00
|
|
|
return fmt.Sprintf(`{"id":"%s","detail":"%s","status":"%s","code":%d}`, addslashes(e.ID), addslashes(e.Detail), addslashes(e.Status), e.Code)
|
2022-01-21 00:50:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal returns error data
|
|
|
|
func (e *Error) Marshal() ([]byte, error) {
|
|
|
|
return []byte(e.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal set error data
|
|
|
|
func (e *Error) Unmarshal(data []byte) error {
|
|
|
|
str := string(data)
|
|
|
|
if len(data) < 41 {
|
|
|
|
return fmt.Errorf("invalid data")
|
|
|
|
}
|
|
|
|
parts := strings.FieldsFunc(str[1:len(str)-1], func(r rune) bool {
|
|
|
|
return r == ','
|
|
|
|
})
|
|
|
|
for _, part := range parts {
|
|
|
|
nparts := strings.FieldsFunc(part, func(r rune) bool {
|
|
|
|
return r == ':'
|
|
|
|
})
|
2022-01-21 19:13:21 +03:00
|
|
|
for idx := 0; idx < len(nparts)/2; idx += 2 {
|
|
|
|
val := strings.Trim(nparts[idx+1], `"`)
|
|
|
|
if len(val) == 0 {
|
2022-01-21 18:20:56 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-01-21 00:50:16 +03:00
|
|
|
switch {
|
|
|
|
case nparts[idx] == `"id"`:
|
2022-01-21 19:13:21 +03:00
|
|
|
e.ID = val
|
2022-01-21 00:50:16 +03:00
|
|
|
case nparts[idx] == `"detail"`:
|
2022-01-21 19:13:21 +03:00
|
|
|
e.Detail = val
|
2022-01-21 00:50:16 +03:00
|
|
|
case nparts[idx] == `"status"`:
|
2022-01-21 19:13:21 +03:00
|
|
|
e.Status = val
|
2022-01-21 00:50:16 +03:00
|
|
|
case nparts[idx] == `"code"`:
|
2022-01-21 19:13:21 +03:00
|
|
|
c, err := strconv.ParseInt(val, 10, 32)
|
2022-01-21 00:50:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.Code = int32(c)
|
|
|
|
}
|
2022-01-21 18:20:56 +03:00
|
|
|
idx++
|
2022-01-21 00:50:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-25 00:40:50 +03:00
|
|
|
|
|
|
|
func addslashes(str string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, char := range str {
|
|
|
|
switch char {
|
|
|
|
case '\'', '"', '\\':
|
|
|
|
buf.WriteRune('\\')
|
|
|
|
}
|
|
|
|
buf.WriteRune(char)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
2023-10-23 02:35:10 +03:00
|
|
|
|
|
|
|
type retryableError struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retryable returns error that can be retried later
|
|
|
|
func Retryable(err error) error {
|
|
|
|
return &retryableError{err: err}
|
|
|
|
}
|
|
|
|
|
2023-10-25 02:36:52 +03:00
|
|
|
type IsRetryableFunc func(error) bool
|
2023-10-23 02:35:10 +03:00
|
|
|
|
2023-10-25 02:36:52 +03:00
|
|
|
var (
|
|
|
|
RetrayableOracleErrors = []IsRetryableFunc{
|
|
|
|
func(err error) bool {
|
|
|
|
errmsg := err.Error()
|
|
|
|
switch {
|
|
|
|
case strings.Contains(errmsg, `ORA-`):
|
|
|
|
return true
|
|
|
|
case strings.Contains(errmsg, `can not assign`):
|
|
|
|
return true
|
|
|
|
case strings.Contains(errmsg, `can't assign`):
|
|
|
|
return true
|
|
|
|
}
|
2023-10-23 02:35:10 +03:00
|
|
|
return false
|
2023-10-25 02:36:52 +03:00
|
|
|
},
|
2023-10-23 02:35:10 +03:00
|
|
|
}
|
2023-10-25 02:36:52 +03:00
|
|
|
RetrayablePostgresErrors = []IsRetryableFunc{
|
|
|
|
func(err error) bool {
|
2023-10-23 02:35:10 +03:00
|
|
|
errmsg := err.Error()
|
2023-10-25 02:36:52 +03:00
|
|
|
switch {
|
|
|
|
case strings.Contains(errmsg, `number of field descriptions must equal number of`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case strings.Contains(errmsg, `not a pointer`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case strings.Contains(errmsg, `values, but dst struct has only`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case strings.Contains(errmsg, `struct doesn't have corresponding row field`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case strings.Contains(errmsg, `cannot find field`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case strings.Contains(errmsg, `cannot scan`) || strings.Contains(errmsg, `cannot convert`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case strings.Contains(errmsg, `failed to connect to`):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-10-25 02:36:52 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
RetryableMicroErrors = []IsRetryableFunc{
|
|
|
|
func(err error) bool {
|
|
|
|
switch verr := err.(type) {
|
|
|
|
case *Error:
|
|
|
|
switch verr.Code {
|
|
|
|
case 401, 403, 408, 500, 501, 502, 503, 504:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case *retryableError:
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
|
|
|
}
|
2023-10-25 02:36:52 +03:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
RetryableGoErrors = []IsRetryableFunc{
|
|
|
|
func(err error) bool {
|
|
|
|
switch verr := err.(type) {
|
|
|
|
case interface{ SafeToRetry() bool }:
|
|
|
|
return verr.SafeToRetry()
|
|
|
|
case interface{ Timeout() bool }:
|
|
|
|
return verr.Timeout()
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, io.EOF), errors.Is(err, io.ErrUnexpectedEOF):
|
|
|
|
return true
|
|
|
|
case errors.Is(err, context.DeadlineExceeded):
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case errors.Is(err, io.ErrClosedPipe), errors.Is(err, io.ErrShortBuffer), errors.Is(err, io.ErrShortWrite):
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
RetryableGrpcErrors = []IsRetryableFunc{
|
|
|
|
func(err error) bool {
|
|
|
|
st, ok := status.FromError(err)
|
|
|
|
if !ok {
|
|
|
|
return false
|
2023-10-23 02:35:10 +03:00
|
|
|
}
|
2023-10-25 02:36:52 +03:00
|
|
|
switch st.Code() {
|
|
|
|
case codes.Unavailable, codes.ResourceExhausted:
|
2023-10-23 02:35:10 +03:00
|
|
|
return true
|
2023-10-25 02:36:52 +03:00
|
|
|
case codes.DeadlineExceeded:
|
|
|
|
return true
|
|
|
|
case codes.Internal:
|
|
|
|
switch {
|
|
|
|
case strings.Contains(st.Message(), `transport: received the unexpected content-type "text/html; charset=UTF-8"`):
|
|
|
|
return true
|
|
|
|
case strings.Contains(st.Message(), io.ErrUnexpectedEOF.Error()):
|
|
|
|
return true
|
|
|
|
case strings.Contains(st.Message(), `stream terminated by RST_STREAM with error code: INTERNAL_ERROR`):
|
|
|
|
return true
|
|
|
|
}
|
2023-10-23 02:35:10 +03:00
|
|
|
}
|
|
|
|
return false
|
2023-10-25 02:36:52 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Unwrap provides error wrapping
|
|
|
|
func (e *retryableError) Unwrap() error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the error string
|
|
|
|
func (e *retryableError) Error() string {
|
|
|
|
if e.err == nil {
|
|
|
|
return ""
|
2023-10-23 02:35:10 +03:00
|
|
|
}
|
2023-10-25 02:36:52 +03:00
|
|
|
return e.err.Error()
|
|
|
|
}
|
2023-10-23 02:35:10 +03:00
|
|
|
|
2023-10-25 02:36:52 +03:00
|
|
|
// IsRetryable checks error for ability to retry later
|
|
|
|
func IsRetryable(err error, fns ...IsRetryableFunc) bool {
|
|
|
|
for _, fn := range fns {
|
|
|
|
if ok := fn(err); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 02:35:10 +03:00
|
|
|
return false
|
|
|
|
}
|