Skip to content

Commit 0322417

Browse files
committed
fix test failures
1 parent 7996317 commit 0322417

File tree

4 files changed

+41
-39
lines changed

4 files changed

+41
-39
lines changed

base/error.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ var (
8383

8484
// ErrInvalidJSON is returned when the JSON being unmarshalled cannot be parsed.
8585
ErrInvalidJSON = HTTPErrorf(http.StatusBadRequest, "Invalid JSON")
86+
87+
ErrSyncFnDryRun = &sgError{"Error returned from Sync Function:"}
8688
)
8789

8890
func (e *sgError) Error() string {

db/crud.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,36 +1697,15 @@ func (db *DatabaseCollectionWithUser) PutExistingRevWithBody(ctx context.Context
16971697
// SyncFnDryrun Runs a document through the sync function and returns expiry, channels doc was placed in, access map for users, roles, handler errors and sync fn exceptions.
16981698
// If a docID is a non empty string, the document will be fetched from the bucket, otherwise the body will be used. If both are specified, this function returns an error.
16991699
// The first error return value represents an error that occurs before the sync function is run. The second error return value represents an exception from the sync function.
1700-
func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, body Body, docID, syncFn string) (*channels.ChannelMapperOutput, error, error) {
1701-
doc := &Document{
1702-
ID: docID,
1703-
_body: body,
1704-
}
1705-
oldDoc := doc
1706-
if docID != "" {
1707-
if docInBucket, err := db.GetDocument(ctx, docID, DocUnmarshalAll); err == nil {
1708-
oldDoc = docInBucket
1709-
if len(doc._body) == 0 {
1710-
body = oldDoc.Body(ctx)
1711-
doc._body = body
1712-
// If no body is given, use doc in bucket as doc with no old doc
1713-
oldDoc._body = nil
1714-
}
1715-
doc._body[BodyRev] = oldDoc.SyncData.GetRevTreeID()
1716-
} else {
1717-
return nil, err, nil
1718-
}
1719-
} else {
1720-
oldDoc._body = nil
1721-
}
1700+
func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, oldDoc *Document, body Body, docID, syncFn string) (*channels.ChannelMapperOutput, error) {
17221701

17231702
delete(body, BodyId)
17241703

17251704
// Get the revision ID to match, and the new generation number:
17261705
matchRev, _ := body[BodyRev].(string)
17271706
generation, _ := ParseRevID(ctx, matchRev)
17281707
if generation < 0 {
1729-
return nil, base.HTTPErrorf(http.StatusBadRequest, "Invalid revision ID"), nil
1708+
return nil, base.HTTPErrorf(http.StatusBadRequest, "Invalid revision ID")
17301709
}
17311710
generation++
17321711

@@ -1742,12 +1721,12 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, body Bod
17421721

17431722
err := validateAPIDocUpdate(body)
17441723
if err != nil {
1745-
return nil, err, nil
1724+
return nil, err
17461725
}
17471726
bodyWithoutInternalProps, wasStripped := StripInternalProperties(body)
17481727
canonicalBytesForRevID, err := base.JSONMarshalCanonical(bodyWithoutInternalProps)
17491728
if err != nil {
1750-
return nil, err, nil
1729+
return nil, err
17511730
}
17521731

17531732
// We needed to keep _deleted around in the body until we generated a rev ID, but now we can ditch it.
@@ -1770,30 +1749,33 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, body Bod
17701749
mutableBody, metaMap, _, err := db.prepareSyncFn(oldDoc, newDoc)
17711750
if err != nil {
17721751
base.InfofCtx(ctx, base.KeyDiagnostic, "Failed to prepare to run sync function: %v", err)
1773-
return nil, err, nil
1752+
return nil, err
17741753
}
17751754

17761755
syncOptions, err := MakeUserCtx(db.user, db.ScopeName, db.Name)
17771756
if err != nil {
1778-
return nil, err, nil
1757+
return nil, err
17791758
}
17801759
var output *channels.ChannelMapperOutput
17811760
if syncFn == "" {
17821761
output, err = db.ChannelMapper.MapToChannelsAndAccess(ctx, mutableBody, string(oldDoc._rawBody), metaMap, syncOptions)
1762+
if err != nil {
1763+
err = fmt.Errorf("%s%s", base.ErrSyncFnDryRun, err)
1764+
}
17831765
} else {
17841766
jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second
17851767
syncRunner, err := channels.NewSyncRunner(ctx, syncFn, jsTimeout)
17861768
if err != nil {
1787-
return nil, fmt.Errorf("failed to create sync runner: %v", err), nil
1769+
return nil, fmt.Errorf("failed to create sync runner: %v", err)
17881770
}
17891771
jsOutput, err := syncRunner.Call(ctx, mutableBody, string(oldDoc._rawBody), metaMap, syncOptions)
17901772
if err != nil {
1791-
return nil, err, nil
1773+
err = fmt.Errorf("%s%s", base.ErrSyncFnDryRun, err)
17921774
}
17931775
output = jsOutput.(*channels.ChannelMapperOutput)
17941776
}
17951777

1796-
return output, nil, err
1778+
return output, err
17971779
}
17981780

17991781
// revTreeConflictCheck checks for conflicts in the rev tree history and returns the parent revid, currentRevIndex

rest/diagnostic_doc_api.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"fmt"
1515
"mime"
1616
"net/http"
17+
"strings"
1718

1819
"github.com/couchbase/sync_gateway/auth"
1920
"github.com/couchbase/sync_gateway/base"
@@ -89,16 +90,33 @@ func (h *handler) handleSyncFnDryRun() error {
8990
return base.HTTPErrorf(http.StatusUnprocessableEntity, "Error reading sync function payload: %v", err)
9091
}
9192

92-
output, err, syncFnErr := h.collection.SyncFnDryrun(h.ctx(), syncDryRunPayload.Doc, docid, syncDryRunPayload.Function)
93-
if err != nil {
94-
return err
93+
oldDoc := &db.Document{ID: docid}
94+
oldDoc.UpdateBody(syncDryRunPayload.Doc)
95+
if docid != "" {
96+
if docInbucket, err := h.collection.GetDocument(h.ctx(), docid, db.DocUnmarshalAll); err == nil {
97+
oldDoc = docInbucket
98+
if len(syncDryRunPayload.Doc) == 0 {
99+
syncDryRunPayload.Doc = oldDoc.Body(h.ctx())
100+
oldDoc.UpdateBody(nil)
101+
}
102+
} else {
103+
return base.HTTPErrorf(http.StatusNotFound, "Error reading document: %v", err)
104+
}
105+
} else {
106+
oldDoc.UpdateBody(nil)
95107
}
96-
if syncFnErr != nil {
97-
resp := SyncFnDryRun{
98-
Exception: syncFnErr.Error(),
108+
109+
output, err := h.collection.SyncFnDryrun(h.ctx(), oldDoc, syncDryRunPayload.Doc, docid, syncDryRunPayload.Function)
110+
if err != nil {
111+
if strings.Contains(err.Error(), base.ErrSyncFnDryRun.Error()) {
112+
errMsg := strings.ReplaceAll(err.Error(), base.ErrSyncFnDryRun.Error(), "")
113+
resp := SyncFnDryRun{
114+
Exception: errMsg,
115+
}
116+
h.writeJSON(resp)
117+
return nil
99118
}
100-
h.writeJSON(resp)
101-
return nil
119+
return err
102120
}
103121
errorMsg := ""
104122
if output.Rejection != nil {

rest/diagnostic_doc_api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11431143
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11441144
expectedOutput: SyncFnDryRun{
11451145
Channels: base.SetFromArray([]string{"channel1"}),
1146-
Access: channels.AccessMap{"user1": channels.BaseSetOf(t, "channel1")},
1146+
Access: channels.AccessMap{},
11471147
Roles: channels.AccessMap{},
11481148
Exception: "",
11491149
},

0 commit comments

Comments
 (0)