pouchdb-errors
Advanced tools
Comparing version 5.4.4 to 5.4.5
105
lib/index.js
@@ -54,3 +54,3 @@ 'use strict'; | ||
status: 400, | ||
error: 'invalid_id', | ||
error: 'bad_request', | ||
reason: '_id field must contain a string' | ||
@@ -172,30 +172,3 @@ }); | ||
var allErrors = [ | ||
UNAUTHORIZED, | ||
MISSING_BULK_DOCS, | ||
MISSING_DOC, | ||
REV_CONFLICT, | ||
INVALID_ID, | ||
MISSING_ID, | ||
RESERVED_ID, | ||
NOT_OPEN, | ||
UNKNOWN_ERROR, | ||
BAD_ARG, | ||
INVALID_REQUEST, | ||
QUERY_PARSE_ERROR, | ||
DOC_VALIDATION, | ||
BAD_REQUEST, | ||
NOT_AN_OBJECT, | ||
DB_MISSING, | ||
WSQ_ERROR, | ||
LDB_ERROR, | ||
FORBIDDEN, | ||
INVALID_REV, | ||
FILE_EXISTS, | ||
MISSING_STUB, | ||
IDB_ERROR, | ||
INVALID_URL | ||
]; | ||
function createError(error, reason, name) { | ||
function createError(error, reason) { | ||
function CustomPouchError(reason) { | ||
@@ -211,5 +184,2 @@ // inherit error properties from our parent error manually | ||
/* jshint ignore:end */ | ||
if (name !== undefined) { | ||
this.name = name; | ||
} | ||
if (reason !== undefined) { | ||
@@ -223,66 +193,28 @@ this.reason = reason; | ||
// Find one of the errors defined above based on the value | ||
// of the specified property. | ||
// If reason is provided prefer the error matching that reason. | ||
// This is for differentiating between errors with the same name and status, | ||
// eg, bad_request. | ||
var getErrorTypeByProp = function (prop, value, reason) { | ||
var errorsByProp = allErrors.filter(function (error) { | ||
return error[prop] === value; | ||
}); | ||
return (reason && errorsByProp.filter(function (error) { | ||
return error.message === reason; | ||
})[0]) || errorsByProp[0]; | ||
}; | ||
function generateErrorFromResponse(err) { | ||
function generateErrorFromResponse(res) { | ||
var error, errName, errType, errMsg, errReason; | ||
if (typeof err !== 'object') { | ||
var data = err; | ||
err = UNKNOWN_ERROR; | ||
err.data = data; | ||
} | ||
errName = (res.error === true && typeof res.name === 'string') ? | ||
res.name : | ||
res.error; | ||
errReason = res.reason; | ||
errType = getErrorTypeByProp('name', errName, errReason); | ||
if (res.missing || | ||
errReason === 'missing' || | ||
errReason === 'deleted' || | ||
errName === 'not_found') { | ||
errType = MISSING_DOC; | ||
} else if (errName === 'doc_validation') { | ||
// doc validation needs special treatment since | ||
// res.reason depends on the validation error. | ||
// see utils.js | ||
errType = DOC_VALIDATION; | ||
errMsg = errReason; | ||
} else if (errName === 'bad_request' && errType.message !== errReason) { | ||
// if bad_request error already found based on reason don't override. | ||
errType = BAD_REQUEST; | ||
if ('error' in err && err.error === 'conflict') { | ||
err.name = 'conflict'; | ||
err.status = 409; | ||
} | ||
// fallback to error by status or unknown error. | ||
if (!errType) { | ||
errType = getErrorTypeByProp('status', res.status, errReason) || | ||
UNKNOWN_ERROR; | ||
if (!('name' in err)) { | ||
err.name = err.error || 'unknown'; | ||
} | ||
error = createError(errType, errReason, errName); | ||
// Keep custom message. | ||
if (errMsg) { | ||
error.message = errMsg; | ||
if (!('status' in err)) { | ||
err.status = 500; | ||
} | ||
// Keep helpful response data in our error messages. | ||
if (res.id) { | ||
error.id = res.id; | ||
if (!('message' in err)) { | ||
err.message = err.message || err.reason; | ||
} | ||
if (res.status) { | ||
error.status = res.status; | ||
} | ||
if (res.missing) { | ||
error.missing = res.missing; | ||
} | ||
return error; | ||
return err; | ||
} | ||
@@ -314,4 +246,3 @@ | ||
exports.INVALID_URL = INVALID_URL; | ||
exports.getErrorTypeByProp = getErrorTypeByProp; | ||
exports.createError = createError; | ||
exports.generateErrorFromResponse = generateErrorFromResponse; |
{ | ||
"name": "pouchdb-errors", | ||
"version": "5.4.4", | ||
"version": "5.4.5", | ||
"description": "Errors exposed by PouchDB.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
107
src/index.js
@@ -47,3 +47,3 @@ import inherits from 'inherits'; | ||
status: 400, | ||
error: 'invalid_id', | ||
error: 'bad_request', | ||
reason: '_id field must contain a string' | ||
@@ -165,30 +165,3 @@ }); | ||
var allErrors = [ | ||
UNAUTHORIZED, | ||
MISSING_BULK_DOCS, | ||
MISSING_DOC, | ||
REV_CONFLICT, | ||
INVALID_ID, | ||
MISSING_ID, | ||
RESERVED_ID, | ||
NOT_OPEN, | ||
UNKNOWN_ERROR, | ||
BAD_ARG, | ||
INVALID_REQUEST, | ||
QUERY_PARSE_ERROR, | ||
DOC_VALIDATION, | ||
BAD_REQUEST, | ||
NOT_AN_OBJECT, | ||
DB_MISSING, | ||
WSQ_ERROR, | ||
LDB_ERROR, | ||
FORBIDDEN, | ||
INVALID_REV, | ||
FILE_EXISTS, | ||
MISSING_STUB, | ||
IDB_ERROR, | ||
INVALID_URL | ||
]; | ||
function createError(error, reason, name) { | ||
function createError(error, reason) { | ||
function CustomPouchError(reason) { | ||
@@ -204,5 +177,2 @@ // inherit error properties from our parent error manually | ||
/* jshint ignore:end */ | ||
if (name !== undefined) { | ||
this.name = name; | ||
} | ||
if (reason !== undefined) { | ||
@@ -216,66 +186,28 @@ this.reason = reason; | ||
// Find one of the errors defined above based on the value | ||
// of the specified property. | ||
// If reason is provided prefer the error matching that reason. | ||
// This is for differentiating between errors with the same name and status, | ||
// eg, bad_request. | ||
var getErrorTypeByProp = function (prop, value, reason) { | ||
var errorsByProp = allErrors.filter(function (error) { | ||
return error[prop] === value; | ||
}); | ||
return (reason && errorsByProp.filter(function (error) { | ||
return error.message === reason; | ||
})[0]) || errorsByProp[0]; | ||
}; | ||
function generateErrorFromResponse(err) { | ||
function generateErrorFromResponse(res) { | ||
var error, errName, errType, errMsg, errReason; | ||
if (typeof err !== 'object') { | ||
var data = err; | ||
err = UNKNOWN_ERROR; | ||
err.data = data; | ||
} | ||
errName = (res.error === true && typeof res.name === 'string') ? | ||
res.name : | ||
res.error; | ||
errReason = res.reason; | ||
errType = getErrorTypeByProp('name', errName, errReason); | ||
if (res.missing || | ||
errReason === 'missing' || | ||
errReason === 'deleted' || | ||
errName === 'not_found') { | ||
errType = MISSING_DOC; | ||
} else if (errName === 'doc_validation') { | ||
// doc validation needs special treatment since | ||
// res.reason depends on the validation error. | ||
// see utils.js | ||
errType = DOC_VALIDATION; | ||
errMsg = errReason; | ||
} else if (errName === 'bad_request' && errType.message !== errReason) { | ||
// if bad_request error already found based on reason don't override. | ||
errType = BAD_REQUEST; | ||
if ('error' in err && err.error === 'conflict') { | ||
err.name = 'conflict'; | ||
err.status = 409; | ||
} | ||
// fallback to error by status or unknown error. | ||
if (!errType) { | ||
errType = getErrorTypeByProp('status', res.status, errReason) || | ||
UNKNOWN_ERROR; | ||
if (!('name' in err)) { | ||
err.name = err.error || 'unknown'; | ||
} | ||
error = createError(errType, errReason, errName); | ||
// Keep custom message. | ||
if (errMsg) { | ||
error.message = errMsg; | ||
if (!('status' in err)) { | ||
err.status = 500; | ||
} | ||
// Keep helpful response data in our error messages. | ||
if (res.id) { | ||
error.id = res.id; | ||
if (!('message' in err)) { | ||
err.message = err.message || err.reason; | ||
} | ||
if (res.status) { | ||
error.status = res.status; | ||
} | ||
if (res.missing) { | ||
error.missing = res.missing; | ||
} | ||
return error; | ||
return err; | ||
} | ||
@@ -308,5 +240,4 @@ | ||
INVALID_URL, | ||
getErrorTypeByProp, | ||
createError, | ||
generateErrorFromResponse | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22619
406