Replication REST API error handling

If I initiate non-continuous replication via the CB Lite REST API, how can I determine if it was successful? I see that I can poll /_active_tasks, and for continuous replication that does provide error status. However, that does not work for one-shot replication since there is no active task to review once replication has failed.

Have you tried using feed=continuous on /_active_tasks?

curl -H 'Content-Type: application/json' -vX GET \
'http://localhost:5984/_active_tasks?feed=continuous'

Then when you start one-shot replications with invalid credentials the following is returned (only tried on iOS).

{
  "status":"Processed 0 \/ 0 changes",
  "task":"repl008",
  "type":"Replication",
  "source":"http:\/\/user2:passs@localhost:4984\/todo"
}
{
  "status":"Stopped",
  "task":"repl008",
  "type":"Replication",
  "source":"http:\/\/user2:passs@localhost:4984\/todo",
  "error":{
    "status":401,
    "reason":"The operation couldn’t be completed. (CBLHTTP error 401.)","url":"http:\/\/user2:passs@localhost:4984\/todo\/_changes?feed=normal&heartbeat=300000&style=all_docs",
    "error":"unauthorized"
  }
}

If you just need to check for 401 unauthorized though, I would use the POST /{db}/_session endpoint on Sync Gateway with the credentials of the user.

Thanks. I’ll give that a try.

In a one-shot replication, the POST to /_replicate doesn’t send a response until the replication completes, and the response data indicates success or failure.

That’s not what I saw using CB Lite Java. I did a one-shot POST /_replicate to a bogus URL and although the errors were logged, the response was 200 with just a session ID. I haven’t tested with other types of errors such as auth 401, but in any case I need to know if the target host was unreachable.

The response did not have the “ok” field. Am I to assume that if that field is missing then something failed? I guess that would be better than nothing, but I’d really like to get more specific feedback.

@jens’ suggestion (checking the response from /_replicate) is probably simpler to implement than using /_active_tasks?feed=continuous. If you noticed a different behaviour between Android and iOS then it’s most probably a bug.

This may be a bug/compatibility issue with the Java implementation. @hideki, do you know?

From the source code, CBL Java does not respond the result immediately if _replicator REST API is called with continuous=false (one-shot replication). So it should work as expected. But I need to verify if this is reproducible with the test. I submit the ticket, https://github.com/couchbase/couchbase-lite-java-core/issues/1540, to track the issue.