* Perform assertions and invoke `fn(err, res)`. * * @param {?Error} resError * @param {Response} res * @param {Function} fn * @api private
(resError, res, fn)
| 168 | * @api private |
| 169 | */ |
| 170 | assert(resError, res, fn) { |
| 171 | let errorObj; |
| 172 | |
| 173 | // check for unexpected network errors or server not running/reachable errors |
| 174 | // when there is no response and superagent sends back a System Error |
| 175 | // do not check further for other asserts, if any, in such case |
| 176 | // https://nodejs.org/api/errors.html#errors_common_system_errors |
| 177 | const sysErrors = { |
| 178 | ECONNREFUSED: 'Connection refused', |
| 179 | ECONNRESET: 'Connection reset by peer', |
| 180 | EPIPE: 'Broken pipe', |
| 181 | ETIMEDOUT: 'Operation timed out' |
| 182 | }; |
| 183 | |
| 184 | if (!res && resError) { |
| 185 | if (resError instanceof Error && resError.syscall === 'connect' |
| 186 | && Object.getOwnPropertyNames(sysErrors).indexOf(resError.code) >= 0) { |
| 187 | errorObj = new Error(resError.code + ': ' + sysErrors[resError.code]); |
| 188 | } else { |
| 189 | errorObj = resError; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // asserts |
| 194 | for (let i = 0; i < this._asserts.length && !errorObj; i += 1) { |
| 195 | errorObj = this._assertFunction(this._asserts[i], res); |
| 196 | } |
| 197 | |
| 198 | // set unexpected superagent error if no other error has occurred. |
| 199 | if (!errorObj && resError instanceof Error && (!res || resError.status !== res.status)) { |
| 200 | errorObj = resError; |
| 201 | } |
| 202 | |
| 203 | if (fn) { |
| 204 | fn.call(this, errorObj || null, res); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Adds a set Authorization Bearer |
no test coverage detected