@@ -1694,58 +1694,11 @@ func (db *DatabaseCollectionWithUser) PutExistingRevWithBody(ctx context.Context
16941694
16951695}
16961696
1697- // 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.
1698- // 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 .
1699- // 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 , oldDoc * Document , body Body , docID , syncFn string ) (* channels.ChannelMapperOutput , error ) {
1697+ // SyncFnDryrun Runs the given document body through a sync function and returns expiry, channels doc was placed in,
1698+ // access map for users, roles, handler errors and sync fn exceptions .
1699+ // If syncFn is provided, it will be used instead of the one configured on the database .
1700+ func (db * DatabaseCollectionWithUser ) SyncFnDryrun (ctx context.Context , newDoc , oldDoc * Document , syncFn string ) (* channels.ChannelMapperOutput , error ) {
17011701
1702- delete (body , BodyId )
1703-
1704- // Get the revision ID to match, and the new generation number:
1705- matchRev , _ := body [BodyRev ].(string )
1706- generation , _ := ParseRevID (ctx , matchRev )
1707- if generation < 0 {
1708- return nil , base .HTTPErrorf (http .StatusBadRequest , "Invalid revision ID" )
1709- }
1710- generation ++
1711-
1712- // Create newDoc which will be used to pass around Body
1713- newDoc := & Document {
1714- ID : docID ,
1715- }
1716- // Pull out attachments
1717- newDoc .SetAttachments (GetBodyAttachments (body ))
1718- delete (body , BodyAttachments )
1719-
1720- delete (body , BodyRevisions )
1721-
1722- err := validateAPIDocUpdate (body )
1723- if err != nil {
1724- return nil , err
1725- }
1726- bodyWithoutInternalProps , wasStripped := StripInternalProperties (body )
1727- canonicalBytesForRevID , err := base .JSONMarshalCanonical (bodyWithoutInternalProps )
1728- if err != nil {
1729- return nil , err
1730- }
1731-
1732- // We needed to keep _deleted around in the body until we generated a rev ID, but now we can ditch it.
1733- _ , isDeleted := body [BodyDeleted ]
1734- if isDeleted {
1735- delete (body , BodyDeleted )
1736- }
1737-
1738- // and now we can finally update the newDoc body to be without any special properties
1739- newDoc .UpdateBody (body )
1740-
1741- // If no special properties were stripped and document wasn't deleted, the canonical bytes represent the current
1742- // body. In this scenario, store canonical bytes as newDoc._rawBody
1743- if ! wasStripped && ! isDeleted {
1744- newDoc ._rawBody = canonicalBytesForRevID
1745- }
1746-
1747- newRev := CreateRevIDWithBytes (generation , matchRev , canonicalBytesForRevID )
1748- newDoc .RevID = newRev
17491702 mutableBody , metaMap , _ , err := db .prepareSyncFn (oldDoc , newDoc )
17501703 if err != nil {
17511704 base .InfofCtx (ctx , base .KeyDiagnostic , "Failed to prepare to run sync function: %v" , err )
@@ -1761,7 +1714,7 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, oldDoc *
17611714 if syncFn == "" {
17621715 output , err = db .ChannelMapper .MapToChannelsAndAccess (ctx , mutableBody , string (oldDoc ._rawBody ), metaMap , syncOptions )
17631716 if err != nil {
1764- syncErr = fmt . Errorf ( "%s%s" , base .ErrSyncFnDryRun , err )
1717+ return nil , & base.SyncFnDryRunError { Err : err }
17651718 }
17661719 } else {
17671720 jsTimeout := time .Duration (base .DefaultJavascriptTimeoutSecs ) * time .Second
@@ -1771,7 +1724,8 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, oldDoc *
17711724 }
17721725 jsOutput , err := syncRunner .Call (ctx , mutableBody , string (oldDoc ._rawBody ), metaMap , syncOptions )
17731726 if err != nil {
1774- syncErr = fmt .Errorf ("%s%s" , base .ErrSyncFnDryRun , err )
1727+
1728+ return nil , fmt .Errorf ("failed to create sync runner: %v" , err )
17751729 }
17761730 output = jsOutput .(* channels.ChannelMapperOutput )
17771731 }
0 commit comments