Skip to content

Commit

Permalink
mssql: send entire error struct as Message stringer
Browse files Browse the repository at this point in the history
  • Loading branch information
kardianos committed May 19, 2022
1 parent 7bb1ded commit 1598eaf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
12 changes: 5 additions & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Error struct {
Number int32
State uint8
Class uint8
Message Message
Message string
ServerName string
ProcName string
LineNo int32
Expand All @@ -23,14 +23,12 @@ type Error struct {
All []Error
}

type Message string

func (e Error) Error() string {
return "mssql: " + e.Message.String()
return "mssql: " + e.Message
}

func (m Message) String() string {
return string(m)
func (e Error) String() string {
return e.Message
}

// SQLErrorNumber returns the SQL Server error number.
Expand All @@ -47,7 +45,7 @@ func (e Error) SQLErrorClass() uint8 {
}

func (e Error) SQLErrorMessage() string {
return e.Message.String()
return e.Message
}

func (e Error) SQLErrorServerName() string {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/denisenkom/go-mssqldb

go 1.11
go 1.13

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188
github.com/golang-sql/sqlexp v0.1.0
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4=
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
Expand Down
5 changes: 3 additions & 2 deletions queries_go19_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.9
// +build go1.9

package mssql
Expand Down Expand Up @@ -1125,10 +1126,10 @@ func TestMessageQueue(t *testing.T) {
active := true

msgs := []interface{}{
sqlexp.MsgNotice{Message: Message("msg1")},
sqlexp.MsgNotice{Message: Error{Message: "msg1"}},
sqlexp.MsgNext{},
sqlexp.MsgRowsAffected{Count: 1},
sqlexp.MsgNotice{Message: Message("msg2")},
sqlexp.MsgNotice{Message: Error{Message: "msg2"}},
sqlexp.MsgNextResultSet{},
}
i := 0
Expand Down
10 changes: 5 additions & 5 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func parseError72(r *tdsBuffer) (res Error) {
res.Number = r.int32()
res.State = r.byte()
res.Class = r.byte()
res.Message = Message(r.UsVarChar())
res.Message = r.UsVarChar()
res.ServerName = r.BVarChar()
res.ProcName = r.BVarChar()
res.LineNo = r.int32()
Expand All @@ -615,7 +615,7 @@ func parseInfo(r *tdsBuffer) (res Error) {
res.Number = r.int32()
res.State = r.byte()
res.Class = r.byte()
res.Message = Message(r.UsVarChar())
res.Message = r.UsVarChar()
res.ServerName = r.BVarChar()
res.ProcName = r.BVarChar()
res.LineNo = r.int32()
Expand Down Expand Up @@ -773,7 +773,7 @@ func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenS
}
errs = append(errs, err)
if sess.logFlags&logErrors != 0 {
sess.logger.Log(ctx, msdsn.LogErrors, err.Message.String())
sess.logger.Log(ctx, msdsn.LogErrors, err.Message)
}
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgError{Error: err})
Expand All @@ -784,10 +784,10 @@ func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenS
sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got INFO %d %s", info.Number, info.Message))
}
if sess.logFlags&logMessages != 0 {
sess.logger.Log(ctx, msdsn.LogMessages, info.Message.String())
sess.logger.Log(ctx, msdsn.LogMessages, info.Message)
}
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNotice{Message: info.Message})
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNotice{Message: info})
}
case tokenReturnValue:
nv := parseReturnValue(sess.buf)
Expand Down

1 comment on commit 1598eaf

@shueybubbles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, this is what I had in mind originally!

Please sign in to comment.