Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit bce02b5

Browse files
authored
Merge pull request #305 from cloudant/303-document-error
Add 'Callback Signature' section to README.
2 parents 6011243 + 65832ab commit bce02b5

File tree

1 file changed

+87
-11
lines changed

1 file changed

+87
-11
lines changed

README.md

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ This is the official Cloudant library for Node.js.
77
* [Installation and Usage](#installation-and-usage)
88
* [Getting Started](#getting-started)
99
* [Initialization](#initialization)
10+
* [Initialization Callback](#initialization-callback)
1011
* [Callback Signature](#callback-signature)
11-
* [Password Authentication](#password-authentication)
12-
* [Cloudant Local](#cloudant-local)
1312
* [Request Plugins](#request-plugins)
1413
* [API Reference](#api-reference)
1514
* [Authorization and API Keys](#authorization-and-api-keys)
@@ -19,9 +18,12 @@ This is the official Cloudant library for Node.js.
1918
* [Virtual Hosts](#virtual-hosts)
2019
* [Cloudant Query](#cloudant-query)
2120
* [Cloudant Search](#cloudant-search)
22-
* [Cookie Authentication](#cookie-authentication)
21+
* [Cloudant Geospatial](#cloudant-geospatial)
22+
* [TypeScript Support](#typescript-support)
2323
* [Advanced Features](#advanced-features)
24+
* [Debugging](#debugging)
2425
* [Advanced Configuration](#advanced-configuration)
26+
* [TLS 1.2 Support](#tls-12-support)
2527
* [Pool size and open sockets](#pool-size-and-open-sockets)
2628
* [Extending the Cloudant Library](#extending-the-cloudant-library)
2729
* [Pipes](#pipes)
@@ -33,7 +35,6 @@ This is the official Cloudant library for Node.js.
3335
* [License](#license)
3436
* [Reference](#reference)
3537

36-
3738
## Installation and Usage
3839

3940
The best way to use the Cloudant client is to begin with your own Node.js project, and define this work as your dependency. In other words, put me in your package.json dependencies. The `npm` tool can do this for you, from the command line:
@@ -106,7 +107,7 @@ cloudant.db.destroy('alice', function(err) {
106107
var alice = cloudant.db.use('alice')
107108

108109
// ...and insert a document in it.
109-
alice.insert({ crazy: true }, 'panda', function(err, body, header) {
110+
alice.insert({ crazy: true }, 'panda', function(err, body, headers) {
110111
if (err) {
111112
return console.log('[alice.insert] ', err.message);
112113
}
@@ -220,13 +221,88 @@ Cloudant({account:me, password:password}, function(err, cloudant) {
220221
});
221222
~~~
222223

223-
After initialization, in general, callback functions receive three arguments:
224+
### Callback Signature
225+
226+
Callback functions receive three arguments:
227+
228+
```js
229+
function(err, body, headers) {}
230+
```
231+
232+
* `err` - The _error_ (if any). For example, fetching a document that doesn't exist:
233+
234+
```js
235+
var mydb = cloudant.db.use('mydb');
236+
mydb.get('non-existent-doc', function(err, data) {
237+
console.log(err);
238+
});
239+
```
240+
241+
```
242+
{ Error: deleted
243+
at Object.clientCallback (/usr/src/app/node_modules/nano/lib/nano.js:248:15)
244+
at Request._callback (/usr/src/app/node_modules/@cloudant/cloudant/lib/clientutils.js:154:11)
245+
...
246+
name: 'Error',
247+
error: 'not_found',
248+
reason: 'deleted',
249+
scope: 'couch',
250+
statusCode: 404,
251+
request:
252+
{ method: 'GET',
253+
headers:
254+
{ 'content-type': 'application/json',
255+
accept: 'application/json' },
256+
uri: 'http://localhost:5984/_users/895c3440-42e7-11e8-b9b2-358fa5dee4a0' },
257+
headers:
258+
{ 'x-couchdb-body-time': '0',
259+
'x-couch-request-id': '1c16b2b81f',
260+
'transfer-encoding': 'chunked',
261+
etag: '"7Q4MT2X8W1RO3JQOLSA4KGMV7"',
262+
date: 'Fri, 27 Apr 2018 08:49:26 GMT',
263+
'content-type': 'application/json',
264+
'cache-control': 'must-revalidate',
265+
statusCode: 404,
266+
uri: 'http://localhost:5984/_users/895c3440-42e7-11e8-b9b2-358fa5dee4a0' },
267+
errid: 'non_200',
268+
description: 'couch returned 404' }
269+
```
270+
271+
As shown above, the corresponding database `request`, `headers` and `statusCode` are also returned in the error.
224272

225-
* `err` - the error, if any
226-
* `body` - the http _response body_ from Cloudant, if no error.
227-
* `header` - the http _response header_ from Cloudant, if no error
273+
* `body` - The HTTP _response body_ (if no error). For example:
274+
275+
```js
276+
cloudant.db.list(function(err, body, headers) {
277+
console.log(body);
278+
});
279+
```
280+
281+
```
282+
[ '_replicator', '_users' ]
283+
```
284+
285+
* `headers` - The HTTP _response headers_ (if no error). For example:
286+
287+
```js
288+
cloudant.db.list(function(err, body, headers) {
289+
console.log(headers);
290+
});
291+
```
292+
293+
```
294+
{ 'x-couchdb-body-time': '0',
295+
'x-couch-request-id': '591be401f1',
296+
'transfer-encoding': 'chunked',
297+
etag: '"7Q4MT2X8W1RO3JQOLSA4KGMV7"',
298+
date: 'Fri, 27 Apr 2018 08:49:49 GMT',
299+
'content-type': 'application/json',
300+
'cache-control': 'must-revalidate',
301+
statusCode: 200,
302+
uri: 'http://localhost:5984/_all_dbs' }
303+
```
228304

229-
The `ping()` function is the only exception to this rule. It does not return headers since a "ping" is made from multiple requests to gather various bits of information.
305+
Note that the `statusCode` and `uri` and also included amongst the response headers.
230306

231307
### Request Plugins
232308

@@ -976,7 +1052,7 @@ Cloudant({account:me, password:password}, function(er, cloudant) {
9761052
// specify the database we are going to use
9771053
var alice = cloudant.db.use('alice')
9781054
// and insert a document in it
979-
alice.insert({ crazy: true }, 'panda', function(err, body, header) {
1055+
alice.insert({ crazy: true }, 'panda', function(err, body, headers) {
9801056
if (err)
9811057
return console.log('[alice.insert] ', err.message)
9821058

0 commit comments

Comments
 (0)