apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

Status resolve/reject #282

Open davidretireup opened 2 years ago

davidretireup commented 2 years ago

The handling of 400 level statusCodes via error callback or reject is not backward compatible after 9.0.1 (due to changes in axios somewhere around 0.21.1)

Expected Behavior

In the following code example, if a database does not exist when using nano 9.0.1 or earlier, exists would be true if missing_db does not exist:

const {db} = require('nano')({
    url: 'http://some-db.server.com:5984/',
    parseUrl: false
})
const exists = await db.get('missing_db')
    .then(() => true)
    .catch(err => err.statusCode === 404 ? false : Promise.reject(err))

Current Behavior

The above example code rejects with err as:

 {
     message: 'error happened in your connection',
     scope: 'socket',
     errid: 'request'
 }

Possible Solution

The following change to the config object corrects the issue, as a workaround:

 const {db} = require('nano')({
     url: 'http://some-db.server.com:5984/',
     parseUrl: false,
     requestDefaults: {
         validateStatus: status => status < 500 // Resolve only if the status code is less than 500
     }
 })

Steps to Reproduce (for bugs)

  1. Ensure missing_db does not exist
  2. Execute the example code

Context

It is necessary to be able to know if a database exists or not

Your Environment