[CALCITE-3320] Use transitional x/xerrors package when working with errors
diff --git a/class_mappings.go b/class_mappings.go
index dc063e2..086af31 100644
--- a/class_mappings.go
+++ b/class_mappings.go
@@ -18,11 +18,11 @@
 package avatica
 
 import (
-	"fmt"
 	"strings"
 
 	avaticaMessage "github.com/apache/calcite-avatica-go/v4/message"
 	"github.com/golang/protobuf/proto"
+	"golang.org/x/xerrors"
 )
 
 const (
@@ -117,6 +117,6 @@
 	case "SyncResultsResponse":
 		return &avaticaMessage.SyncResultsResponse{}, nil
 	default:
-		return nil, fmt.Errorf("Unable to create response from the string: %s", className)
+		return nil, xerrors.Errorf("unable to create response from the string: %s", className)
 	}
 }
diff --git a/compat_go18.go b/compat_go18.go
index 5ea09e2..f9d9029 100644
--- a/compat_go18.go
+++ b/compat_go18.go
@@ -21,7 +21,8 @@
 
 import (
 	"database/sql/driver"
-	"fmt"
+
+	"golang.org/x/xerrors"
 )
 
 func driverNamedValueToNamedValue(values []driver.NamedValue) ([]namedValue, error) {
@@ -31,7 +32,7 @@
 		list[i] = namedValue(nv)
 
 		if nv.Name != "" {
-			return list, fmt.Errorf("named parameters are not supported: %s given", nv.Name)
+			return list, xerrors.Errorf("named parameters are not supported: %s given", nv.Name)
 		}
 	}
 
diff --git a/connection.go b/connection.go
index 222cc9d..4eba2ef 100644
--- a/connection.go
+++ b/connection.go
@@ -23,6 +23,7 @@
 
 	"github.com/apache/calcite-avatica-go/v4/errors"
 	"github.com/apache/calcite-avatica-go/v4/message"
+	"golang.org/x/xerrors"
 )
 
 type conn struct {
@@ -206,7 +207,9 @@
 
 func (c *conn) avaticaErrorToResponseErrorOrError(err error) error {
 
-	avaticaErr, ok := err.(avaticaError)
+	var avaticaErr avaticaError
+
+	ok := xerrors.As(err, &avaticaErr)
 
 	if !ok {
 		return err
diff --git a/connection_go18.go b/connection_go18.go
index 1c75b0d..9e9c4f1 100644
--- a/connection_go18.go
+++ b/connection_go18.go
@@ -23,14 +23,14 @@
 	"context"
 	"database/sql"
 	"database/sql/driver"
-	"errors"
-	"fmt"
+
+	"golang.org/x/xerrors"
 )
 
 func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
 
 	if opts.ReadOnly {
-		return nil, errors.New("Read-only transactions are not supported")
+		return nil, xerrors.New("read-only transactions are not supported")
 	}
 
 	var isolation isoLevel
@@ -43,17 +43,17 @@
 	case sql.LevelReadCommitted:
 		isolation = isolationReadComitted
 	case sql.LevelWriteCommitted:
-		return nil, errors.New("LevelWriteCommitted isolation level is not supported")
+		return nil, xerrors.New("LevelWriteCommitted isolation level is not supported")
 	case sql.LevelRepeatableRead:
 		isolation = isolationRepeatableRead
 	case sql.LevelSnapshot:
-		return nil, errors.New("LevelSnapshot isolation level is not supported")
+		return nil, xerrors.New("LevelSnapshot isolation level is not supported")
 	case sql.LevelSerializable:
 		isolation = isolationSerializable
 	case sql.LevelLinearizable:
-		return nil, errors.New("LevelLinearizable isolation level is not supported")
+		return nil, xerrors.New("LevelLinearizable isolation level is not supported")
 	default:
-		return nil, fmt.Errorf("Unsupported transaction isolation level: %d", opts.Isolation)
+		return nil, xerrors.Errorf("unsupported transaction isolation level: %d", opts.Isolation)
 	}
 
 	return c.begin(ctx, isolation)
@@ -67,7 +67,7 @@
 	list, err := driverNamedValueToNamedValue(args)
 
 	if err != nil {
-		return nil, fmt.Errorf("could not execute statement: %s", err)
+		return nil, xerrors.Errorf("could not execute statement: %v", err)
 	}
 
 	return c.exec(ctx, query, list)
@@ -78,7 +78,7 @@
 	_, err := c.ExecContext(ctx, c.adapter.GetPingStatement(), []driver.NamedValue{})
 
 	if err != nil {
-		return fmt.Errorf("Error pinging database: %s", err)
+		return xerrors.Errorf("error pinging database: %v", err)
 	}
 
 	return nil
@@ -88,7 +88,7 @@
 	list, err := driverNamedValueToNamedValue(args)
 
 	if err != nil {
-		return nil, fmt.Errorf("could not execute query: %s", err)
+		return nil, xerrors.Errorf("could not execute query: %v", err)
 	}
 
 	return c.query(ctx, query, list)
diff --git a/driver.go b/driver.go
index 7e63488..9a41544 100644
--- a/driver.go
+++ b/driver.go
@@ -35,7 +35,6 @@
 	"context"
 	"database/sql"
 	"database/sql/driver"
-	"fmt"
 	"net/http"
 
 	"github.com/apache/calcite-avatica-go/v4/generic"
@@ -43,6 +42,7 @@
 	"github.com/apache/calcite-avatica-go/v4/message"
 	"github.com/apache/calcite-avatica-go/v4/phoenix"
 	"github.com/hashicorp/go-uuid"
+	"golang.org/x/xerrors"
 )
 
 // Driver is exported to allow it to be used directly.
@@ -66,18 +66,18 @@
 	config, err := ParseDSN(c.dsn)
 
 	if err != nil {
-		return nil, fmt.Errorf("Unable to open connection: %s", err)
+		return nil, xerrors.Errorf("unable to open connection: %v", err)
 	}
 
 	httpClient, err := NewHTTPClient(config.endpoint, c.Client, config)
 
 	if err != nil {
-		return nil, fmt.Errorf("Unable to create HTTP client: %s", err)
+		return nil, xerrors.Errorf("unable to create HTTP client: %v", err)
 	}
 
 	connectionId, err := uuid.GenerateUUID()
 	if err != nil {
-		return nil, fmt.Errorf("Error generating connection id: %s", err)
+		return nil, xerrors.Errorf("error generating connection id: %v", err)
 	}
 
 	info := map[string]string{
diff --git a/dsn.go b/dsn.go
index b5fc8f5..a7a7ce1 100644
--- a/dsn.go
+++ b/dsn.go
@@ -18,11 +18,12 @@
 package avatica
 
 import (
-	"fmt"
 	"net/url"
 	"strconv"
 	"strings"
 	"time"
+
+	"golang.org/x/xerrors"
 )
 
 type authentication int
@@ -70,7 +71,7 @@
 	parsed, err := url.ParseRequestURI(dsn)
 
 	if err != nil {
-		return nil, fmt.Errorf("Unable to parse DSN: %s", err)
+		return nil, xerrors.Errorf("unable to parse DSN: %v", err)
 	}
 
 	queries := parsed.Query()
@@ -80,7 +81,7 @@
 		maxRowTotal, err := strconv.Atoi(v)
 
 		if err != nil {
-			return nil, fmt.Errorf("Invalid value for maxRowsTotal: %s", err)
+			return nil, xerrors.Errorf("invalid value for maxRowsTotal: %v", err)
 		}
 
 		conf.maxRowsTotal = int64(maxRowTotal)
@@ -91,7 +92,7 @@
 		maxRowTotal, err := strconv.Atoi(v)
 
 		if err != nil {
-			return nil, fmt.Errorf("Invalid value for frameMaxSize: %s", err)
+			return nil, xerrors.Errorf("invalid value for frameMaxSize: %v", err)
 		}
 
 		conf.frameMaxSize = int32(maxRowTotal)
@@ -102,7 +103,7 @@
 		loc, err := time.LoadLocation(v)
 
 		if err != nil {
-			return nil, fmt.Errorf("Invalid value for location: %s", err)
+			return nil, xerrors.Errorf("invalid value for location: %v", err)
 		}
 
 		conf.location = loc
@@ -113,11 +114,11 @@
 		isolation, err := strconv.Atoi(v)
 
 		if err != nil {
-			return nil, fmt.Errorf("Invalid value for transactionIsolation: %s", err)
+			return nil, xerrors.Errorf("invalid value for transactionIsolation: %v", err)
 		}
 
 		if isolation < 0 || isolation > 8 || isolation&(isolation-1) != 0 {
-			return nil, fmt.Errorf("transactionIsolation must be 0, 1, 2, 4 or 8, %d given", isolation)
+			return nil, xerrors.Errorf("transactionIsolation must be 0, 1, 2, 4 or 8, %d given", isolation)
 		}
 
 		conf.transactionIsolation = uint32(isolation)
@@ -134,7 +135,7 @@
 		} else if auth == "SPNEGO" {
 			conf.authentication = spnego
 		} else {
-			return nil, fmt.Errorf("authentication must be either BASIC, DIGEST or SPNEGO")
+			return nil, xerrors.New("authentication must be either BASIC, DIGEST or SPNEGO")
 		}
 
 		if conf.authentication == basic || conf.authentication == digest {
@@ -142,7 +143,7 @@
 			user := queries.Get("avaticaUser")
 
 			if user == "" {
-				return nil, fmt.Errorf("authentication is set to %s, but avaticaUser is empty", v)
+				return nil, xerrors.Errorf("authentication is set to %s, but avaticaUser is empty", v)
 			}
 
 			conf.avaticaUser = user
@@ -150,7 +151,7 @@
 			pass := queries.Get("avaticaPassword")
 
 			if pass == "" {
-				return nil, fmt.Errorf("authentication is set to %s, but avaticaPassword is empty", v)
+				return nil, xerrors.Errorf("authentication is set to %s, but avaticaPassword is empty", v)
 			}
 
 			conf.avaticaPassword = pass
@@ -165,15 +166,15 @@
 			krb5CredentialCache := queries.Get("krb5CredentialCache")
 
 			if principal == "" && keytab == "" && krb5Conf == "" && krb5CredentialCache == "" {
-				return nil, fmt.Errorf("when using SPNEGO authetication, you must provide the principal, keytab and krb5Conf parameters or a krb5TicketCache parameter")
+				return nil, xerrors.New("when using SPNEGO authetication, you must provide the principal, keytab and krb5Conf parameters or a krb5TicketCache parameter")
 			}
 
 			if !((principal != "" && keytab != "" && krb5Conf != "") || (principal == "" && keytab == "" && krb5Conf == "")) {
-				return nil, fmt.Errorf("when using SPNEGO authentication with a principal and keytab, the principal, keytab and krb5Conf parameters are required")
+				return nil, xerrors.New("when using SPNEGO authentication with a principal and keytab, the principal, keytab and krb5Conf parameters are required")
 			}
 
 			if (principal != "" || keytab != "" || krb5Conf != "") && krb5CredentialCache != "" {
-				return nil, fmt.Errorf("ambigious configuration for SPNEGO authentication: use either principal, keytab and krb5Conf or krb5TicketCache")
+				return nil, xerrors.New("ambigious configuration for SPNEGO authentication: use either principal, keytab and krb5Conf or krb5TicketCache")
 			}
 
 			if principal != "" {
@@ -181,7 +182,7 @@
 				splittedPrincipal := strings.Split(principal, "@")
 
 				if len(splittedPrincipal) != 2 {
-					return nil, fmt.Errorf("invalid kerberos principal (%s): the principal should be in the format primary/instance@realm where instance is optional", principal)
+					return nil, xerrors.Errorf("invalid kerberos principal (%s): the principal should be in the format primary/instance@realm where instance is optional", principal)
 				}
 
 				conf.principal = krb5Principal{
diff --git a/go.mod b/go.mod
index 18ac111..0298fad 100644
--- a/go.mod
+++ b/go.mod
@@ -9,6 +9,7 @@
 	github.com/stretchr/testify v1.3.0 // indirect
 	github.com/xinsnake/go-http-digest-auth-client v0.4.0
 	golang.org/x/crypto v0.0.0-20190424203555-c05e17bb3b2d // indirect
+	golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
 	gopkg.in/jcmturner/aescts.v1 v1.0.1 // indirect
 	gopkg.in/jcmturner/dnsutils.v1 v1.0.1 // indirect
 	gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect
diff --git a/go.sum b/go.sum
index 0b1d162..7d8b463 100644
--- a/go.sum
+++ b/go.sum
@@ -20,6 +20,8 @@
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 gopkg.in/jcmturner/aescts.v1 v1.0.1 h1:cVVZBK2b1zY26haWB4vbBiZrfFQnfbTVrE3xZq6hrEw=
 gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo=
 gopkg.in/jcmturner/dnsutils.v1 v1.0.1 h1:cIuC1OLRGZrld+16ZJvvZxVJeKPsvd5eUIvxfoN5hSM=
diff --git a/http_client.go b/http_client.go
index d877e45..e7c5468 100644
--- a/http_client.go
+++ b/http_client.go
@@ -31,6 +31,7 @@
 
 	avaticaMessage "github.com/apache/calcite-avatica-go/v4/message"
 	"github.com/golang/protobuf/proto"
+	"golang.org/x/xerrors"
 )
 
 var (
@@ -80,7 +81,7 @@
 			realm := config.principal.realm
 			cli, err := WithKerberosAuth(baseClient, user, realm, config.keytab, config.krb5Conf, config.krb5CredentialCache)
 			if err != nil {
-				return nil, err
+				return nil, xerrors.Errorf("can't add kerberos authentication to http client: %v", err)
 			}
 			baseClient = cli
 		}
@@ -100,7 +101,7 @@
 	wrapped, err := proto.Marshal(message)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error marshaling request message to protobuf: %v", err)
 	}
 
 	wire := &avaticaMessage.WireMessage{
@@ -111,13 +112,13 @@
 	body, err := proto.Marshal(wire)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error marshaling wire message to protobuf: %v", err)
 	}
 
 	req, err := http.NewRequest("POST", c.host, bytes.NewReader(body))
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error creating http request: %v", err)
 	}
 
 	req.Header.Set("Content-Type", "application/x-google-protobuf")
@@ -127,7 +128,7 @@
 	res, err := c.httpClient.Do(req)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error executing http request: %v", err)
 	}
 
 	defer res.Body.Close()
@@ -135,7 +136,7 @@
 	response, err := ioutil.ReadAll(res.Body)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error reading response body: %v", err)
 	}
 
 	result := &avaticaMessage.WireMessage{}
@@ -143,19 +144,19 @@
 	err = proto.Unmarshal(response, result)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error unmarshaling wire message: %v", err)
 	}
 
 	inner, err := responseFromClassName(result.Name)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error getting wrapped response from wire message: %v", err)
 	}
 
 	err = proto.Unmarshal(result.WrappedMessage, inner)
 
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error unmarshaling wrapped message: %v", err)
 	}
 
 	if v, ok := inner.(*avaticaMessage.ErrorResponse); ok {
diff --git a/http_client_wrappers.go b/http_client_wrappers.go
index c45e0f7..f0a0900 100644
--- a/http_client_wrappers.go
+++ b/http_client_wrappers.go
@@ -18,10 +18,10 @@
 package avatica
 
 import (
-	"fmt"
 	"net/http"
 
 	digest_auth_client "github.com/xinsnake/go-http-digest-auth-client"
+	"golang.org/x/xerrors"
 	"gopkg.in/jcmturner/gokrb5.v7/client"
 	"gopkg.in/jcmturner/gokrb5.v7/config"
 	"gopkg.in/jcmturner/gokrb5.v7/credentials"
@@ -49,26 +49,26 @@
 	if krb5CredentialCache != "" {
 		tc, err := credentials.LoadCCache(krb5CredentialCache)
 		if err != nil {
-			return nil, fmt.Errorf("error reading kerberos ticket cache: %s", err)
+			return nil, xerrors.Errorf("error reading kerberos ticket cache: %v", err)
 		}
 		kc, err := client.NewClientFromCCache(tc, config.NewConfig())
 		if err != nil {
-			return nil, fmt.Errorf("error creating kerberos client: %s", err)
+			return nil, xerrors.Errorf("error creating kerberos client: %v", err)
 		}
 		kerberosClient = kc
 	} else {
 		cfg, err := config.Load(krb5Conf)
 		if err != nil {
-			return nil, fmt.Errorf("error reading kerberos config: %s", err)
+			return nil, xerrors.Errorf("error reading kerberos config: %v", err)
 		}
 		kt, err := keytab.Load(keyTab)
 		if err != nil {
-			return nil, fmt.Errorf("error reading kerberos keytab: %s", err)
+			return nil, xerrors.Errorf("error reading kerberos keytab: %v", err)
 		}
 		kc := client.NewClientWithKeytab(username, realm, kt, cfg)
 		err = kc.Login()
 		if err != nil {
-			return nil, fmt.Errorf("error performing kerberos login with keytab: %s", err)
+			return nil, xerrors.Errorf("error performing kerberos login with keytab: %v", err)
 		}
 		kerberosClient = kc
 	}
@@ -104,7 +104,7 @@
 func (t *krb5Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
 	err = gokrbSPNEGO.SetSPNEGOHeader(t.kerberosClient, req, "")
 	if err != nil {
-		return nil, err
+		return nil, xerrors.Errorf("error setting SPNEGO header: %v", err)
 	}
 	return t.baseTransport.RoundTrip(req)
 }
diff --git a/result.go b/result.go
index 696c6a5..db0b73c 100644
--- a/result.go
+++ b/result.go
@@ -17,7 +17,9 @@
 
 package avatica
 
-import "errors"
+import (
+	"golang.org/x/xerrors"
+)
 
 type result struct {
 	affectedRows int64
@@ -28,7 +30,7 @@
 // after, for example, an INSERT into a table with primary
 // key.
 func (r *result) LastInsertId() (int64, error) {
-	return 0, errors.New("Use 'SELECT CURRENT VALUE FOR your.sequence' to get the last inserted id. For more information, see: https://phoenix.apache.org/sequences.html.")
+	return 0, xerrors.New("use 'SELECT CURRENT VALUE FOR your.sequence' to get the last inserted id. For more information, see: https://phoenix.apache.org/sequences.html.")
 }
 
 // RowsAffected returns the number of rows affected by the
diff --git a/statement.go b/statement.go
index 162a1fb..e7d3bc4 100644
--- a/statement.go
+++ b/statement.go
@@ -20,11 +20,11 @@
 import (
 	"context"
 	"database/sql/driver"
-	"errors"
 	"math"
 	"time"
 
 	"github.com/apache/calcite-avatica-go/v4/message"
+	"golang.org/x/xerrors"
 )
 
 type stmt struct {
@@ -101,7 +101,7 @@
 	results := res.(*message.ExecuteResponse).Results
 
 	if len(results) <= 0 {
-		return nil, errors.New("empty ResultSet in ExecuteResponse")
+		return nil, xerrors.New("empty ResultSet in ExecuteResponse")
 	}
 
 	// Currently there is only 1 ResultSet per response
diff --git a/statement_go18.go b/statement_go18.go
index c6ea56f..0370641 100644
--- a/statement_go18.go
+++ b/statement_go18.go
@@ -22,7 +22,8 @@
 import (
 	"context"
 	"database/sql/driver"
-	"fmt"
+
+	"golang.org/x/xerrors"
 )
 
 func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
@@ -30,7 +31,7 @@
 	list, err := driverNamedValueToNamedValue(args)
 
 	if err != nil {
-		return nil, fmt.Errorf("Error executing statement: %s", err)
+		return nil, xerrors.Errorf("error executing statement: %v", err)
 	}
 
 	return s.exec(ctx, list)
@@ -41,7 +42,7 @@
 	list, err := driverNamedValueToNamedValue(args)
 
 	if err != nil {
-		return nil, fmt.Errorf("Error executing query: %s", err)
+		return nil, xerrors.Errorf("error executing query: %v", err)
 	}
 
 	return s.query(ctx, list)