gopkg.in/rethinkdb/rethinkdb-go.v6@v6.2.2/errors.go (about)

     1  package rethinkdb
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  	"strings"
     9  
    10  	p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2"
    11  )
    12  
    13  var (
    14  	// ErrNoHosts is returned when no hosts to the Connect method.
    15  	ErrNoHosts = errors.New("no hosts provided")
    16  	// ErrNoConnectionsStarted is returned when the driver couldn't to any of
    17  	// the provided hosts.
    18  	ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
    19  	// ErrInvalidNode is returned when attempting to connect to a node which
    20  	// returns an invalid response.
    21  	ErrInvalidNode = errors.New("invalid node")
    22  	// ErrNoConnections is returned when there are no active connections in the
    23  	// clusters connection pool.
    24  	ErrNoConnections = errors.New("rethinkdb: no connections were available")
    25  	// ErrConnectionClosed is returned when trying to send a query with a connClosed
    26  	// connection.
    27  	ErrConnectionClosed = errors.New("rethinkdb: the connection is closed")
    28  	// ErrQueryTimeout is returned when query context deadline exceeded.
    29  	ErrQueryTimeout = errors.New("rethinkdb: query timeout")
    30  )
    31  
    32  func printCarrots(t Term, frames []*p.Frame) string {
    33  	var frame *p.Frame
    34  	if len(frames) > 1 {
    35  		frame, frames = frames[0], frames[1:]
    36  	} else if len(frames) == 1 {
    37  		frame, frames = frames[0], []*p.Frame{}
    38  	}
    39  
    40  	for i, arg := range t.args {
    41  		if frame.GetPos() == int64(i) {
    42  			t.args[i] = Term{
    43  				termType: p.Term_DATUM,
    44  				data:     printCarrots(arg, frames),
    45  			}
    46  		}
    47  	}
    48  
    49  	for k, arg := range t.optArgs {
    50  		if frame.GetOpt() == k {
    51  			t.optArgs[k] = Term{
    52  				termType: p.Term_DATUM,
    53  				data:     printCarrots(arg, frames),
    54  			}
    55  		}
    56  	}
    57  
    58  	b := &bytes.Buffer{}
    59  	for _, c := range t.String() {
    60  		if c != '^' {
    61  			b.WriteString(" ")
    62  		} else {
    63  			b.WriteString("^")
    64  		}
    65  	}
    66  
    67  	return b.String()
    68  }
    69  
    70  // Error constants
    71  var ErrEmptyResult = errors.New("The result does not contain any more rows")
    72  
    73  // Connection/Response errors
    74  
    75  // rqlResponseError is the base type for all errors, it formats both
    76  // for the response and query if set.
    77  type rqlServerError struct {
    78  	response *Response
    79  	term     *Term
    80  }
    81  
    82  func (e rqlServerError) Error() string {
    83  	var err = "An error occurred"
    84  	if e.response != nil {
    85  		json.Unmarshal(e.response.Responses[0], &err)
    86  	}
    87  
    88  	if e.term == nil {
    89  		return fmt.Sprintf("rethinkdb: %s", err)
    90  	}
    91  
    92  	return fmt.Sprintf("rethinkdb: %s in:\n%s", err, e.term.String())
    93  
    94  }
    95  
    96  func (e rqlServerError) String() string {
    97  	return e.Error()
    98  }
    99  
   100  type rqlError string
   101  
   102  func (e rqlError) Error() string {
   103  	return fmt.Sprintf("rethinkdb: %s", string(e))
   104  }
   105  
   106  func (e rqlError) String() string {
   107  	return e.Error()
   108  }
   109  
   110  // Exported Error "Implementations"
   111  
   112  type RQLClientError struct{ rqlServerError }
   113  type RQLCompileError struct{ rqlServerError }
   114  type RQLDriverCompileError struct{ RQLCompileError }
   115  type RQLServerCompileError struct{ RQLCompileError }
   116  type RQLAuthError struct{ RQLDriverError }
   117  type RQLRuntimeError struct{ rqlServerError }
   118  
   119  type RQLQueryLogicError struct{ RQLRuntimeError }
   120  type RQLNonExistenceError struct{ RQLQueryLogicError }
   121  type RQLResourceLimitError struct{ RQLRuntimeError }
   122  type RQLUserError struct{ RQLRuntimeError }
   123  type RQLInternalError struct{ RQLRuntimeError }
   124  type RQLTimeoutError struct{ rqlServerError }
   125  type RQLAvailabilityError struct{ RQLRuntimeError }
   126  type RQLOpFailedError struct{ RQLAvailabilityError }
   127  type RQLOpIndeterminateError struct{ RQLAvailabilityError }
   128  
   129  // RQLDriverError represents an unexpected error with the driver, if this error
   130  // persists please create an issue.
   131  type RQLDriverError struct {
   132  	rqlError
   133  }
   134  
   135  // RQLConnectionError represents an error when communicating with the database
   136  // server.
   137  type RQLConnectionError struct {
   138  	rqlError
   139  }
   140  
   141  func createClientError(response *Response, term *Term) error {
   142  	return RQLClientError{rqlServerError{response, term}}
   143  }
   144  
   145  func createCompileError(response *Response, term *Term) error {
   146  	return RQLCompileError{rqlServerError{response, term}}
   147  }
   148  
   149  func createRuntimeError(errorType p.Response_ErrorType, response *Response, term *Term) error {
   150  	serverErr := rqlServerError{response, term}
   151  
   152  	switch errorType {
   153  	case p.Response_QUERY_LOGIC:
   154  		return RQLQueryLogicError{RQLRuntimeError{serverErr}}
   155  	case p.Response_NON_EXISTENCE:
   156  		return RQLNonExistenceError{RQLQueryLogicError{RQLRuntimeError{serverErr}}}
   157  	case p.Response_RESOURCE_LIMIT:
   158  		return RQLResourceLimitError{RQLRuntimeError{serverErr}}
   159  	case p.Response_USER:
   160  		return RQLUserError{RQLRuntimeError{serverErr}}
   161  	case p.Response_INTERNAL:
   162  		return RQLInternalError{RQLRuntimeError{serverErr}}
   163  	case p.Response_OP_FAILED:
   164  		return RQLOpFailedError{RQLAvailabilityError{RQLRuntimeError{serverErr}}}
   165  	case p.Response_OP_INDETERMINATE:
   166  		return RQLOpIndeterminateError{RQLAvailabilityError{RQLRuntimeError{serverErr}}}
   167  	default:
   168  		return RQLRuntimeError{serverErr}
   169  	}
   170  }
   171  
   172  // Error type helpers
   173  
   174  // IsConflictErr returns true if the error is non-nil and the query failed
   175  // due to a duplicate primary key.
   176  func IsConflictErr(err error) bool {
   177  	if err == nil {
   178  		return false
   179  	}
   180  
   181  	return strings.HasPrefix(err.Error(), "Duplicate primary key")
   182  }
   183  
   184  // IsTypeErr returns true if the error is non-nil and the query failed due
   185  // to a type error.
   186  func IsTypeErr(err error) bool {
   187  	if err == nil {
   188  		return false
   189  	}
   190  
   191  	return strings.HasPrefix(err.Error(), "Expected type")
   192  }