Skip to content

Commit

Permalink
MB-50274 Expose error's "cause" field as "reason" when not a transaction
Browse files Browse the repository at this point in the history
error to satisfy SDK requirements.

Change-Id: I88fcf4c80778d47edcfd2473194edbb910bccd31
Reviewed-on: https://review.couchbase.org/c/query/+/168327
Reviewed-by: Bingjie Miao <bingjie.miao@couchbase.com>
Reviewed-by: Isha Kandaswamy <isha@couchbase.com>
Reviewed-by: Sitaram Vemulapalli <sitaram.vemulapalli@couchbase.com>
Tested-by: Donald Haggart <donald.haggart@couchbase.com>
  • Loading branch information
dhaggart committed Jan 14, 2022
1 parent 4bc5961 commit b31c4fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions errors/transactions.go
Expand Up @@ -10,16 +10,21 @@ package errors

import (
"fmt"
"strings"
)

// rewrite errors 17000-17099

func IsTransactionError(e Error) bool {
return strings.HasPrefix(e.TranslationKey(), "transaction")
}

func NewTransactionError(e error, msg string) Error {
switch e := e.(type) {
case Error: // if given error is already an Error, just return it:
return e
default:
return &err{level: EXCEPTION, ICode: E_TRANSACTION, IKey: "transaction_error", ICause: e,
return &err{level: EXCEPTION, ICode: E_TRANSACTION, IKey: "transaction.error", ICause: e,
InternalMsg: msg, InternalCaller: CallerN(1)}
}
}
Expand Down Expand Up @@ -130,7 +135,7 @@ func NewKeyNotFoundError(k string, c interface{}) Error {
}

func NewCasMissmatch(op, key string, aCas, eCas uint64) Error {
return &err{level: EXCEPTION, ICode: E_CAS_MISSMATCH, IKey: "transaction.statement.keynotfound",
return &err{level: EXCEPTION, ICode: E_CAS_MISSMATCH, IKey: "transaction.statement.casmismatch",
InternalMsg: fmt.Sprintf("%s cas (actual:%v, expected:%v) missmatch for key: %v", op, aCas, eCas, key),
InternalCaller: CallerN(1)}
}
Expand Down
6 changes: 5 additions & 1 deletion server/http/service_response.go
Expand Up @@ -416,7 +416,11 @@ func (this *httpRequest) writeError(err errors.Error, first bool, prefix, indent
m["retry"] = value.ToBool(retry)
}
if err.Cause() != nil {
m["cause"] = err.Cause()
if !errors.IsTransactionError(err) {
m["reason"] = err.Cause()
} else {
m["cause"] = err.Cause()
}
}

var er error
Expand Down

0 comments on commit b31c4fa

Please sign in to comment.