blob: fc8a52bd744eadfc605c97e0f687b9a81edebdc2 [file] [log] [blame]
import inherits from 'inherits';
inherits(PouchError, Error);
function PouchError(opts) {
Error.call(this, opts.reason);
this.status = opts.status;
this.name = opts.error;
this.message = opts.reason;
this.error = true;
}
PouchError.prototype.toString = function () {
return JSON.stringify({
status: this.status,
name: this.name,
message: this.message,
reason: this.reason
});
};
var UNAUTHORIZED = new PouchError({
status: 401,
error: 'unauthorized',
reason: "Name or password is incorrect."
});
var MISSING_BULK_DOCS = new PouchError({
status: 400,
error: 'bad_request',
reason: "Missing JSON list of 'docs'"
});
var MISSING_DOC = new PouchError({
status: 404,
error: 'not_found',
reason: 'missing'
});
var REV_CONFLICT = new PouchError({
status: 409,
error: 'conflict',
reason: 'Document update conflict'
});
var INVALID_ID = new PouchError({
status: 400,
error: 'invalid_id',
reason: '_id field must contain a string'
});
var MISSING_ID = new PouchError({
status: 412,
error: 'missing_id',
reason: '_id is required for puts'
});
var RESERVED_ID = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Only reserved document ids may start with underscore.'
});
var NOT_OPEN = new PouchError({
status: 412,
error: 'precondition_failed',
reason: 'Database not open'
});
var UNKNOWN_ERROR = new PouchError({
status: 500,
error: 'unknown_error',
reason: 'Database encountered an unknown error'
});
var BAD_ARG = new PouchError({
status: 500,
error: 'badarg',
reason: 'Some query argument is invalid'
});
var INVALID_REQUEST = new PouchError({
status: 400,
error: 'invalid_request',
reason: 'Request was invalid'
});
var QUERY_PARSE_ERROR = new PouchError({
status: 400,
error: 'query_parse_error',
reason: 'Some query parameter is invalid'
});
var DOC_VALIDATION = new PouchError({
status: 500,
error: 'doc_validation',
reason: 'Bad special document member'
});
var BAD_REQUEST = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Something wrong with the request'
});
var NOT_AN_OBJECT = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Document must be a JSON object'
});
var DB_MISSING = new PouchError({
status: 404,
error: 'not_found',
reason: 'Database not found'
});
var IDB_ERROR = new PouchError({
status: 500,
error: 'indexed_db_went_bad',
reason: 'unknown'
});
var WSQ_ERROR = new PouchError({
status: 500,
error: 'web_sql_went_bad',
reason: 'unknown'
});
var LDB_ERROR = new PouchError({
status: 500,
error: 'levelDB_went_went_bad',
reason: 'unknown'
});
var FORBIDDEN = new PouchError({
status: 403,
error: 'forbidden',
reason: 'Forbidden by design doc validate_doc_update function'
});
var INVALID_REV = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Invalid rev format'
});
var FILE_EXISTS = new PouchError({
status: 412,
error: 'file_exists',
reason: 'The database could not be created, the file already exists.'
});
var MISSING_STUB = new PouchError({
status: 412,
error: 'missing_stub'
});
var INVALID_URL = new PouchError({
status: 413,
error: 'invalid_url',
reason: 'Provided URL is invalid'
});
var allErrors = {
UNAUTHORIZED: UNAUTHORIZED,
MISSING_BULK_DOCS: MISSING_BULK_DOCS,
MISSING_DOC: MISSING_DOC,
REV_CONFLICT: REV_CONFLICT,
INVALID_ID: INVALID_ID,
MISSING_ID: MISSING_ID,
RESERVED_ID: RESERVED_ID,
NOT_OPEN: NOT_OPEN,
UNKNOWN_ERROR: UNKNOWN_ERROR,
BAD_ARG: BAD_ARG,
INVALID_REQUEST: INVALID_REQUEST,
QUERY_PARSE_ERROR: QUERY_PARSE_ERROR,
DOC_VALIDATION: DOC_VALIDATION,
BAD_REQUEST: BAD_REQUEST,
NOT_AN_OBJECT: NOT_AN_OBJECT,
DB_MISSING: DB_MISSING,
WSQ_ERROR: WSQ_ERROR,
LDB_ERROR: LDB_ERROR,
FORBIDDEN: FORBIDDEN,
INVALID_REV: INVALID_REV,
FILE_EXISTS: FILE_EXISTS,
MISSING_STUB: MISSING_STUB,
IDB_ERROR: IDB_ERROR,
INVALID_URL: INVALID_URL
};
function createError(error, reason, name) {
function CustomPouchError(reason) {
// inherit error properties from our parent error manually
// so as to allow proper JSON parsing.
/* jshint ignore:start */
for (var p in error) {
if (typeof error[p] !== 'function') {
this[p] = error[p];
}
}
/* jshint ignore:end */
if (name !== undefined) {
this.name = name;
}
if (reason !== undefined) {
this.reason = reason;
}
}
CustomPouchError.prototype = PouchError.prototype;
return new CustomPouchError(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 keys = Object.keys(allErrors).filter(function (key) {
var error = allErrors[key];
return typeof error !== 'function' && error[prop] === value;
});
var key = reason && keys.filter(function (key) {
var error = allErrors[key];
return error.message === reason;
})[0] || keys[0];
return (key) ? allErrors[key] : null;
};
function generateErrorFromResponse(res) {
var error, errName, errType, errMsg, errReason;
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;
}
// fallback to error by status or unknown error.
if (!errType) {
errType = getErrorTypeByProp('status', res.status, errReason) ||
UNKNOWN_ERROR;
}
error = createError(errType, errReason, errName);
// Keep custom message.
if (errMsg) {
error.message = errMsg;
}
// Keep helpful response data in our error messages.
if (res.id) {
error.id = res.id;
}
if (res.status) {
error.status = res.status;
}
if (res.missing) {
error.missing = res.missing;
}
return error;
}
export {
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,
getErrorTypeByProp,
createError,
generateErrorFromResponse,
allErrors as errors
};