File: errors.go

package info (click to toggle)
golang-gopkg-dancannon-gorethink.v1 1.4.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 764 kB
  • sloc: makefile: 3
file content (182 lines) | stat: -rw-r--r-- 4,909 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gorethink

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"strings"

	p "gopkg.in/dancannon/gorethink.v1/ql2"
)

var (
	// ErrNoHosts is returned when no hosts to the Connect method.
	ErrNoHosts = errors.New("no hosts provided")
	// ErrNoConnectionsStarted is returned when the driver couldn't to any of
	// the provided hosts.
	ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
	// ErrInvalidNode is returned when attempting to connect to a node which
	// returns an invalid response.
	ErrInvalidNode = errors.New("invalid node")
	// ErrNoConnections is returned when there are no active connections in the
	// clusters connection pool.
	ErrNoConnections = errors.New("gorethink: no connections were available")
	// ErrConnectionClosed is returned when trying to send a query with a closed
	// connection.
	ErrConnectionClosed = errors.New("gorethink: the connection is closed")
)

func printCarrots(t Term, frames []*p.Frame) string {
	var frame *p.Frame
	if len(frames) > 1 {
		frame, frames = frames[0], frames[1:]
	} else if len(frames) == 1 {
		frame, frames = frames[0], []*p.Frame{}
	}

	for i, arg := range t.args {
		if frame.GetPos() == int64(i) {
			t.args[i] = Term{
				termType: p.Term_DATUM,
				data:     printCarrots(arg, frames),
			}
		}
	}

	for k, arg := range t.optArgs {
		if frame.GetOpt() == k {
			t.optArgs[k] = Term{
				termType: p.Term_DATUM,
				data:     printCarrots(arg, frames),
			}
		}
	}

	b := &bytes.Buffer{}
	for _, c := range t.String() {
		if c != '^' {
			b.WriteString(" ")
		} else {
			b.WriteString("^")
		}
	}

	return b.String()
}

// Error constants
var ErrEmptyResult = errors.New("The result does not contain any more rows")

// Connection/Response errors

// rqlResponseError is the base type for all errors, it formats both
// for the response and query if set.
type rqlServerError struct {
	response *Response
	term     *Term
}

func (e rqlServerError) Error() string {
	var err = "An error occurred"
	if e.response != nil {
		json.Unmarshal(e.response.Responses[0], &err)
	}

	if e.term == nil {
		return fmt.Sprintf("gorethink: %s", err)
	}

	return fmt.Sprintf("gorethink: %s in: \n%s", err, e.term.String())

}

func (e rqlServerError) String() string {
	return e.Error()
}

type rqlError string

func (e rqlError) Error() string {
	return fmt.Sprintf("gorethink: %s", string(e))
}

func (e rqlError) String() string {
	return e.Error()
}

// Exported Error "Implementations"

type RQLClientError struct{ rqlServerError }
type RQLCompileError struct{ rqlServerError }
type RQLDriverCompileError struct{ RQLCompileError }
type RQLServerCompileError struct{ RQLCompileError }
type RQLAuthError struct{ RQLDriverError }
type RQLRuntimeError struct{ rqlServerError }

type RQLQueryLogicError struct{ RQLRuntimeError }
type RQLNonExistenceError struct{ RQLQueryLogicError }
type RQLResourceLimitError struct{ RQLRuntimeError }
type RQLUserError struct{ RQLRuntimeError }
type RQLInternalError struct{ RQLRuntimeError }
type RQLTimeoutError struct{ rqlServerError }
type RQLAvailabilityError struct{ RQLRuntimeError }
type RQLOpFailedError struct{ RQLAvailabilityError }
type RQLOpIndeterminateError struct{ RQLAvailabilityError }

// RQLDriverError represents an unexpected error with the driver, if this error
// persists please create an issue.
type RQLDriverError struct {
	rqlError
}

// RQLConnectionError represents an error when communicating with the database
// server.
type RQLConnectionError struct {
	rqlError
}

func createRuntimeError(errorType p.Response_ErrorType, response *Response, term *Term) error {
	serverErr := rqlServerError{response, term}

	switch errorType {
	case p.Response_QUERY_LOGIC:
		return RQLQueryLogicError{RQLRuntimeError{serverErr}}
	case p.Response_NON_EXISTENCE:
		return RQLNonExistenceError{RQLQueryLogicError{RQLRuntimeError{serverErr}}}
	case p.Response_RESOURCE_LIMIT:
		return RQLResourceLimitError{RQLRuntimeError{serverErr}}
	case p.Response_USER:
		return RQLUserError{RQLRuntimeError{serverErr}}
	case p.Response_INTERNAL:
		return RQLInternalError{RQLRuntimeError{serverErr}}
	case p.Response_OP_FAILED:
		return RQLOpFailedError{RQLAvailabilityError{RQLRuntimeError{serverErr}}}
	case p.Response_OP_INDETERMINATE:
		return RQLOpIndeterminateError{RQLAvailabilityError{RQLRuntimeError{serverErr}}}
	default:
		return RQLRuntimeError{serverErr}
	}
}

// Error type helpers

// IsConflictErr returns true if the error is non-nil and the query failed
// due to a duplicate primary key.
func IsConflictErr(err error) bool {
	if err == nil {
		return false
	}

	return strings.HasPrefix(err.Error(), "Duplicate primary key")
}

// IsTypeErr returns true if the error is non-nil and the query failed due
// to a type error.
func IsTypeErr(err error) bool {
	if err == nil {
		return false
	}

	return strings.HasPrefix(err.Error(), "Expected type")
}