Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
sql: Standardize SQL error format using PG's structured errors
Browse files Browse the repository at this point in the history
  • Loading branch information
a6802739 authored and nvanbenschoten committed Jan 7, 2017
1 parent 138f023 commit 5b3ccd0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
25 changes: 15 additions & 10 deletions servererrfieldtype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const (
serverErrFieldSeverity serverErrFieldType = 'S'
serverErrFieldSQLState serverErrFieldType = 'C'
serverErrFieldMsgPrimary serverErrFieldType = 'M'
serverErrFileldDetail serverErrFieldType = 'D'
serverErrFileldHint serverErrFieldType = 'H'
serverErrFieldSrcFile serverErrFieldType = 'F'
serverErrFieldSrcLine serverErrFieldType = 'L'
serverErrFieldSrcFunction serverErrFieldType = 'R'
Expand Down Expand Up @@ -425,7 +427,7 @@ func (c *v3Conn) serve(ctx context.Context, reserved mon.BoundAccount) error {
// The spec says to drop any extra of these messages.

default:
err = c.sendErrorWithCode(pgerror.CodeProtocolViolationError, sqlbase.MakeSrcCtx(0),
err = c.sendErrorWithCode(pgerror.CodeProtocolViolationError, "", "", sqlbase.MakeSrcCtx(0),
fmt.Sprintf("unrecognized client message type %s", typ))
}
if err != nil {
Expand Down Expand Up @@ -812,20 +814,22 @@ func (c *v3Conn) sendCommandComplete(tag []byte) error {

func (c *v3Conn) sendError(err error) error {
if sqlErr, ok := err.(sqlbase.ErrorWithPGCode); ok {
return c.sendErrorWithCode(sqlErr.Code(), sqlErr.SrcContext(), err.Error())
return c.sendErrorWithCode(sqlErr.Code(), sqlErr.Detail(), sqlErr.Hint(), sqlErr.SrcContext(), err.Error())
}
return c.sendInternalError(err.Error())
}

// TODO(andrei): Figure out the correct codes to send for all the errors
// in this file and remove this function.
func (c *v3Conn) sendInternalError(errToSend string) error {
return c.sendErrorWithCode(pgerror.CodeInternalError, sqlbase.MakeSrcCtx(1), errToSend)
return c.sendErrorWithCode(pgerror.CodeInternalError, "", "", sqlbase.MakeSrcCtx(1), errToSend)
}

// errCode is a postgres error code, plus our extensions.
// See http://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
func (c *v3Conn) sendErrorWithCode(errCode string, errCtx sqlbase.SrcCtx, errToSend string) error {
func (c *v3Conn) sendErrorWithCode(
errCode, detail, hint string, errCtx sqlbase.SrcCtx, errToSend string,
) error {
if c.doingExtendedQueryMessage {
c.ignoreTillSync = true
}
Expand All @@ -835,6 +839,16 @@ func (c *v3Conn) sendErrorWithCode(errCode string, errCtx sqlbase.SrcCtx, errToS
c.writeBuf.putErrFieldMsg(serverErrFieldSeverity)
c.writeBuf.writeTerminatedString("ERROR")

if detail != "" {
c.writeBuf.putErrFieldMsg(serverErrFileldDetail)
c.writeBuf.writeTerminatedString(detail)
}

if hint != "" {
c.writeBuf.putErrFieldMsg(serverErrFileldHint)
c.writeBuf.writeTerminatedString(hint)
}

c.writeBuf.putErrFieldMsg(serverErrFieldSQLState)
c.writeBuf.writeTerminatedString(errCode)

Expand Down Expand Up @@ -1037,7 +1051,7 @@ func (c *v3Conn) copyIn(columns []sql.ResultColumn) error {
// Spec says to "ignore Flush and Sync messages received during copy-in mode".

default:
return c.sendErrorWithCode(pgerror.CodeProtocolViolationError, sqlbase.MakeSrcCtx(0),
return c.sendErrorWithCode(pgerror.CodeProtocolViolationError, "", "", sqlbase.MakeSrcCtx(0),
fmt.Sprintf("unrecognized client message type %s", typ))
}
for _, res := range sr.ResultList {
Expand Down

0 comments on commit 5b3ccd0

Please sign in to comment.