Skip to content

Commit

Permalink
Merge pull request #4556 from systay/errors
Browse files Browse the repository at this point in the history
Use vterrors to store stack traces
  • Loading branch information
deepthi authored Mar 3, 2019
2 parents 7afe66c + c6cae69 commit 6585831
Show file tree
Hide file tree
Showing 89 changed files with 992 additions and 869 deletions.
7 changes: 4 additions & 3 deletions go/mysql/auth_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"fmt"
"net"
"strings"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// AuthServer is the interface that servers must implement to validate
Expand Down Expand Up @@ -226,7 +227,7 @@ func AuthServerReadPacketString(c *Conn) (string, error) {
return "", err
}
if len(data) == 0 || data[len(data)-1] != 0 {
return "", fmt.Errorf("received invalid response packet, datalen=%v", len(data))
return "", vterrors.Errorf(vtrpc.Code_INTERNAL, "received invalid response packet, datalen=%v", len(data))
}
return string(data[:len(data)-1]), nil
}
Expand All @@ -244,6 +245,6 @@ func AuthServerNegotiateClearOrDialog(c *Conn, method string) (string, error) {
return AuthServerReadPacketString(c)

default:
return "", fmt.Errorf("unrecognized method: %v", method)
return "", vterrors.Errorf(vtrpc.Code_INTERNAL, "unrecognized method: %v", method)
}
}
5 changes: 3 additions & 2 deletions go/mysql/auth_server_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
Expand All @@ -30,6 +29,8 @@ import (

"vitess.io/vitess/go/vt/log"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

var (
Expand Down Expand Up @@ -180,7 +181,7 @@ func validateConfig(config map[string][]*AuthServerStaticEntry) error {
for _, entries := range config {
for _, entry := range entries {
if entry.SourceHost != "" && entry.SourceHost != localhostName {
return fmt.Errorf("Invalid SourceHost found (only localhost is supported): %v", entry.SourceHost)
return vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "Invalid SourceHost found (only localhost is supported): %v", entry.SourceHost)
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions go/mysql/binlog_event_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package mysql
import (
"bytes"
"encoding/binary"
"fmt"

binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// binlogEvent wraps a raw packet buffer and provides methods to examine it
Expand Down Expand Up @@ -185,12 +186,12 @@ func (ev binlogEvent) Format() (f BinlogFormat, err error) {

f.FormatVersion = binary.LittleEndian.Uint16(data[:2])
if f.FormatVersion != 4 {
return f, fmt.Errorf("format version = %d, we only support version 4", f.FormatVersion)
return f, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "format version = %d, we only support version 4", f.FormatVersion)
}
f.ServerVersion = string(bytes.TrimRight(data[2:2+50], "\x00"))
f.HeaderLength = data[2+50+4]
if f.HeaderLength < 19 {
return f, fmt.Errorf("header length = %d, should be >= 19", f.HeaderLength)
return f, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "header length = %d, should be >= 19", f.HeaderLength)
}

// MySQL/MariaDB 5.6.1+ always adds a 4-byte checksum to the end of a
Expand Down Expand Up @@ -230,7 +231,7 @@ func (ev binlogEvent) Query(f BinlogFormat) (query Query, err error) {
// position of SQL query
sqlPos := dbPos + dbLen + 1 // +1 for NULL terminator
if sqlPos > len(data) {
return query, fmt.Errorf("SQL query position overflows buffer (%v > %v)", sqlPos, len(data))
return query, vterrors.Errorf(vtrpc.Code_INTERNAL, "SQL query position overflows buffer (%v > %v)", sqlPos, len(data))
}

// We've checked that the buffer is big enough for sql, so everything before
Expand All @@ -257,17 +258,17 @@ varsLoop:
pos += 8
case QCatalog: // Used in MySQL 5.0.0 - 5.0.3
if pos+1 > len(vars) {
return query, fmt.Errorf("Q_CATALOG status var overflows buffer (%v + 1 > %v)", pos, len(vars))
return query, vterrors.Errorf(vtrpc.Code_INTERNAL, "Q_CATALOG status var overflows buffer (%v + 1 > %v)", pos, len(vars))
}
pos += 1 + int(vars[pos]) + 1
case QCatalogNZCode: // Used in MySQL > 5.0.3 to replace QCatalog
if pos+1 > len(vars) {
return query, fmt.Errorf("Q_CATALOG_NZ_CODE status var overflows buffer (%v + 1 > %v)", pos, len(vars))
return query, vterrors.Errorf(vtrpc.Code_INTERNAL, "Q_CATALOG_NZ_CODE status var overflows buffer (%v + 1 > %v)", pos, len(vars))
}
pos += 1 + int(vars[pos])
case QCharsetCode:
if pos+6 > len(vars) {
return query, fmt.Errorf("Q_CHARSET_CODE status var overflows buffer (%v + 6 > %v)", pos, len(vars))
return query, vterrors.Errorf(vtrpc.Code_INTERNAL, "Q_CHARSET_CODE status var overflows buffer (%v + 6 > %v)", pos, len(vars))
}
query.Charset = &binlogdatapb.Charset{
Client: int32(binary.LittleEndian.Uint16(vars[pos : pos+2])),
Expand Down Expand Up @@ -295,7 +296,7 @@ func (ev binlogEvent) IntVar(f BinlogFormat) (byte, uint64, error) {

typ := data[0]
if typ != IntVarLastInsertID && typ != IntVarInsertID {
return 0, 0, fmt.Errorf("invalid IntVar ID: %v", data[0])
return 0, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "invalid IntVar ID: %v", data[0])
}

value := binary.LittleEndian.Uint64(data[1 : 1+8])
Expand Down
12 changes: 7 additions & 5 deletions go/mysql/binlog_event_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

const (
Expand Down Expand Up @@ -97,7 +99,7 @@ func printJSONValue(typ byte, data []byte, toplevel bool, result *bytes.Buffer)
case jsonTypeOpaque:
return printJSONOpaque(data, toplevel, result)
default:
return fmt.Errorf("unknown object type in JSON: %v", typ)
return vterrors.Errorf(vtrpc.Code_INTERNAL, "unknown object type in JSON: %v", typ)
}

return nil
Expand All @@ -108,7 +110,7 @@ func printJSONObject(data []byte, large bool, result *bytes.Buffer) error {
elementCount, pos := readOffsetOrSize(data, pos, large)
size, pos := readOffsetOrSize(data, pos, large)
if size > len(data) {
return fmt.Errorf("not enough data for object, have %v bytes need %v", len(data), size)
return vterrors.Errorf(vtrpc.Code_INTERNAL, "not enough data for object, have %v bytes need %v", len(data), size)
}

// Build an array for each key.
Expand Down Expand Up @@ -152,7 +154,7 @@ func printJSONArray(data []byte, large bool, result *bytes.Buffer) error {
elementCount, pos := readOffsetOrSize(data, pos, large)
size, pos := readOffsetOrSize(data, pos, large)
if size > len(data) {
return fmt.Errorf("not enough data for object, have %v bytes need %v", len(data), size)
return vterrors.Errorf(vtrpc.Code_INTERNAL, "not enough data for object, have %v bytes need %v", len(data), size)
}

// Now read each value, and output them. The value entry is
Expand Down Expand Up @@ -231,7 +233,7 @@ func printJSONLiteral(b byte, toplevel bool, result *bytes.Buffer) error {
case jsonFalseLiteral:
result.WriteString("false")
default:
return fmt.Errorf("unknown literal value %v", b)
return vterrors.Errorf(vtrpc.Code_INTERNAL, "unknown literal value %v", b)
}
if toplevel {
result.WriteByte('\'')
Expand Down Expand Up @@ -374,7 +376,7 @@ func printJSONOpaque(data []byte, toplevel bool, result *bytes.Buffer) error {
// not straightforward (for instance, a bit field seems to
// have one byte as metadata, not two as would be expected).
// To be on the safer side, we just reject these cases for now.
return fmt.Errorf("opaque type %v is not supported yet, with data %v", typ, data[1:])
return vterrors.Errorf(vtrpc.Code_INTERNAL, "opaque type %v is not supported yet, with data %v", typ, data[1:])
}

func printJSONDate(data []byte, toplevel bool, result *bytes.Buffer) error {
Expand Down
27 changes: 15 additions & 12 deletions go/mysql/binlog_event_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package mysql

import (
"fmt"
"strings"
"testing"
)

Expand Down Expand Up @@ -123,19 +123,22 @@ func TestJSON(t *testing.T) {
}, {
// opaque, bit field. Not yet implemented.
data: []byte{15, 16, 2, 202, 254},
expected: `ERROR: opaque type 16 is not supported yet, with data [2 202 254]`,
expected: `opaque type 16 is not supported yet, with data [2 202 254]`,
}}

for _, tcase := range testcases {
r, err := printJSONData(tcase.data)
got := ""
if err != nil {
got = fmt.Sprintf("ERROR: %v", err)
} else {
got = string(r)
}
if got != tcase.expected {
t.Errorf("unexpected output for %v: got %v expected %v", tcase.data, got, tcase.expected)
}
t.Run(tcase.expected, func(t *testing.T) {
r, err := printJSONData(tcase.data)
if err != nil {
if got := err.Error(); !strings.HasPrefix(got, tcase.expected) {
t.Errorf("unexpected output for %v: got [%v] expected [%v]", tcase.data, got, tcase.expected)
}
} else {
if got := string(r); got != tcase.expected {
t.Errorf("unexpected output for %v: got [%v] expected [%v]", tcase.data, got, tcase.expected)
}
}

})
}
}
6 changes: 4 additions & 2 deletions go/mysql/binlog_event_mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package mysql

import (
"encoding/binary"
"fmt"

"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// mariadbBinlogEvent wraps a raw packet buffer and provides methods to examine
Expand Down Expand Up @@ -60,7 +62,7 @@ func (ev mariadbBinlogEvent) GTID(f BinlogFormat) (GTID, bool, error) {

// PreviousGTIDs implements BinlogEvent.PreviousGTIDs().
func (ev mariadbBinlogEvent) PreviousGTIDs(f BinlogFormat) (Position, error) {
return Position{}, fmt.Errorf("MariaDB should not provide PREVIOUS_GTIDS_EVENT events")
return Position{}, vterrors.Errorf(vtrpc.Code_INTERNAL, "MariaDB should not provide PREVIOUS_GTIDS_EVENT events")
}

// StripChecksum implements BinlogEvent.StripChecksum().
Expand Down
6 changes: 4 additions & 2 deletions go/mysql/binlog_event_mysql56.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package mysql

import (
"encoding/binary"
"fmt"

"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// mysql56BinlogEvent wraps a raw packet buffer and provides methods to examine
Expand Down Expand Up @@ -81,6 +83,6 @@ func (ev mysql56BinlogEvent) StripChecksum(f BinlogFormat) (BinlogEvent, []byte,
default:
// MySQL 5.6 does not guarantee that future checksum algorithms will be
// 4 bytes, so we can't support them a priori.
return ev, nil, fmt.Errorf("unsupported checksum algorithm: %v", f.ChecksumAlgorithm)
return ev, nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported checksum algorithm: %v", f.ChecksumAlgorithm)
}
}
26 changes: 14 additions & 12 deletions go/mysql/binlog_event_rbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"time"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"

querypb "vitess.io/vitess/go/vt/proto/query"
)
Expand Down Expand Up @@ -89,7 +91,7 @@ func (ev binlogEvent) TableMap(f BinlogFormat) (*TableMap, error) {
}
}
if pos != expectedEnd {
return nil, fmt.Errorf("unexpected metadata end: got %v was expecting %v (data=%v)", pos, expectedEnd, data)
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "unexpected metadata end: got %v was expecting %v (data=%v)", pos, expectedEnd, data)
}

// A bit array that says if each colum can be NULL.
Expand Down Expand Up @@ -119,7 +121,7 @@ func metadataLength(typ byte) int {

default:
// Unknown type. This is used in tests only, so panic.
panic(fmt.Errorf("metadataLength: unhandled data type: %v", typ))
panic(vterrors.Errorf(vtrpc.Code_INTERNAL, "metadataLength: unhandled data type: %v", typ))
}
}

Expand Down Expand Up @@ -155,7 +157,7 @@ func metadataRead(data []byte, pos int, typ byte) (uint16, int, error) {

default:
// Unknown types, we can't go on.
return 0, 0, fmt.Errorf("metadataRead: unhandled data type: %v", typ)
return 0, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "metadataRead: unhandled data type: %v", typ)
}
}

Expand Down Expand Up @@ -186,7 +188,7 @@ func metadataWrite(data []byte, pos int, typ byte, value uint16) int {

default:
// Unknown type. This is used in tests only, so panic.
panic(fmt.Errorf("metadataRead: unhandled data type: %v", typ))
panic(vterrors.Errorf(vtrpc.Code_INTERNAL, "metadataRead: unhandled data type: %v", typ))
}
}

Expand Down Expand Up @@ -286,7 +288,7 @@ func cellLength(data []byte, pos int, typ byte, metadata uint16) (int, error) {
uint32(data[pos+2])<<16|
uint32(data[pos+3])<<24), nil
default:
return 0, fmt.Errorf("unsupported blob/geometry metadata value %v (data: %v pos: %v)", metadata, data, pos)
return 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported blob/geometry metadata value %v (data: %v pos: %v)", metadata, data, pos)
}
case TypeString:
// This may do String, Enum, and Set. The type is in
Expand All @@ -307,7 +309,7 @@ func cellLength(data []byte, pos int, typ byte, metadata uint16) (int, error) {
return l + 1, nil

default:
return 0, fmt.Errorf("unsupported type %v (data: %v pos: %v)", typ, data, pos)
return 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported type %v (data: %v pos: %v)", typ, data, pos)
}
}

Expand Down Expand Up @@ -772,7 +774,7 @@ func CellValue(data []byte, pos int, typ byte, metadata uint16, styp querypb.Typ
return sqltypes.MakeTrusted(querypb.Type_ENUM,
strconv.AppendUint(nil, uint64(val), 10)), 2, nil
default:
return sqltypes.NULL, 0, fmt.Errorf("unexpected enum size: %v", metadata&0xff)
return sqltypes.NULL, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unexpected enum size: %v", metadata&0xff)
}

case TypeSet:
Expand Down Expand Up @@ -800,15 +802,15 @@ func CellValue(data []byte, pos int, typ byte, metadata uint16, styp querypb.Typ
uint32(data[pos+2])<<16 |
uint32(data[pos+3])<<24)
default:
return sqltypes.NULL, 0, fmt.Errorf("unsupported blob metadata value %v (data: %v pos: %v)", metadata, data, pos)
return sqltypes.NULL, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported blob metadata value %v (data: %v pos: %v)", metadata, data, pos)
}
pos += int(metadata)

// For JSON, we parse the data, and emit SQL.
if typ == TypeJSON {
d, err := printJSONData(data[pos : pos+l])
if err != nil {
return sqltypes.NULL, 0, fmt.Errorf("error parsing JSON data %v: %v", data[pos:pos+l], err)
return sqltypes.NULL, 0, vterrors.Wrapf(err, "error parsing JSON data %v", data[pos:pos+l])
}
return sqltypes.MakeTrusted(sqltypes.Expression,
d), l + int(metadata), nil
Expand All @@ -835,7 +837,7 @@ func CellValue(data []byte, pos int, typ byte, metadata uint16, styp querypb.Typ
return sqltypes.MakeTrusted(querypb.Type_UINT16,
strconv.AppendUint(nil, uint64(val), 10)), 2, nil
default:
return sqltypes.NULL, 0, fmt.Errorf("unexpected enum size: %v", metadata&0xff)
return sqltypes.NULL, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unexpected enum size: %v", metadata&0xff)
}
}
if t == TypeSet {
Expand Down Expand Up @@ -892,14 +894,14 @@ func CellValue(data []byte, pos int, typ byte, metadata uint16, styp querypb.Typ
uint32(data[pos+2])<<16 |
uint32(data[pos+3])<<24)
default:
return sqltypes.NULL, 0, fmt.Errorf("unsupported geometry metadata value %v (data: %v pos: %v)", metadata, data, pos)
return sqltypes.NULL, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported geometry metadata value %v (data: %v pos: %v)", metadata, data, pos)
}
pos += int(metadata)
return sqltypes.MakeTrusted(querypb.Type_GEOMETRY,
data[pos:pos+l]), l + int(metadata), nil

default:
return sqltypes.NULL, 0, fmt.Errorf("unsupported type %v", typ)
return sqltypes.NULL, 0, vterrors.Errorf(vtrpc.Code_INTERNAL, "unsupported type %v", typ)
}
}

Expand Down
6 changes: 4 additions & 2 deletions go/mysql/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"strconv"

binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

// This file contains utility methods for Conn objects. Only useful on the client
Expand All @@ -34,10 +36,10 @@ func ExecuteFetchMap(conn *Conn, query string) (map[string]string, error) {
return nil, err
}
if len(qr.Rows) != 1 {
return nil, fmt.Errorf("query %#v returned %d rows, expected 1", query, len(qr.Rows))
return nil, vterrors.Errorf(vtrpc.Code_OUT_OF_RANGE, "query %#v returned %d rows, expected 1", query, len(qr.Rows))
}
if len(qr.Fields) != len(qr.Rows[0]) {
return nil, fmt.Errorf("query %#v returned %d column names, expected %d", query, len(qr.Fields), len(qr.Rows[0]))
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "query %#v returned %d column names, expected %d", query, len(qr.Fields), len(qr.Rows[0]))
}

rowMap := make(map[string]string)
Expand Down
Loading

0 comments on commit 6585831

Please sign in to comment.