Skip to content

Commit

Permalink
Make errors a bit more sane
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Dec 12, 2024
1 parent e1f7849 commit e750614
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions packages/node_modules/pouchdb-errors/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class PouchError extends Error {
constructor(status, error, reason) {
super();
constructor(status, name, reason) {
super(reason);
this.status = status;
this.name = error;
this.message = reason;
this.name = name;
// TODO this looks pointless:
this.error = true;
}

Expand All @@ -17,52 +17,47 @@ class PouchError extends Error {
}
}

var UNAUTHORIZED = new PouchError(401, 'unauthorized', "Name or password is incorrect.");
var MISSING_BULK_DOCS = new PouchError(400, 'bad_request', "Missing JSON list of 'docs'");
var MISSING_DOC = new PouchError(404, 'not_found', 'missing');
var REV_CONFLICT = new PouchError(409, 'conflict', 'Document update conflict');
var INVALID_ID = new PouchError(400, 'bad_request', '_id field must contain a string');
var MISSING_ID = new PouchError(412, 'missing_id', '_id is required for puts');
var RESERVED_ID = new PouchError(400, 'bad_request', 'Only reserved document ids may start with underscore.');
var NOT_OPEN = new PouchError(412, 'precondition_failed', 'Database not open');
var UNKNOWN_ERROR = new PouchError(500, 'unknown_error', 'Database encountered an unknown error');
var BAD_ARG = new PouchError(500, 'badarg', 'Some query argument is invalid');
var INVALID_REQUEST = new PouchError(400, 'invalid_request', 'Request was invalid');
var QUERY_PARSE_ERROR = new PouchError(400, 'query_parse_error', 'Some query parameter is invalid');
var DOC_VALIDATION = new PouchError(500, 'doc_validation', 'Bad special document member');
var BAD_REQUEST = new PouchError(400, 'bad_request', 'Something wrong with the request');
var NOT_AN_OBJECT = new PouchError(400, 'bad_request', 'Document must be a JSON object');
var DB_MISSING = new PouchError(404, 'not_found', 'Database not found');
var IDB_ERROR = new PouchError(500, 'indexed_db_went_bad', 'unknown');
var WSQ_ERROR = new PouchError(500, 'web_sql_went_bad', 'unknown');
var LDB_ERROR = new PouchError(500, 'levelDB_went_went_bad', 'unknown');
var FORBIDDEN = new PouchError(403, 'forbidden', 'Forbidden by design doc validate_doc_update function');
var INVALID_REV = new PouchError(400, 'bad_request', 'Invalid rev format');
var FILE_EXISTS = new PouchError(412, 'file_exists', 'The database could not be created, the file already exists.');
var MISSING_STUB = new PouchError(412, 'missing_stub', 'A pre-existing attachment stub wasn\'t found');
var INVALID_URL = new PouchError(413, 'invalid_url', 'Provided URL is invalid');
const UNAUTHORIZED = { status:401, name:'unauthorized', message:"Name or password is incorrect." };
const MISSING_BULK_DOCS = { status:400, name:'bad_request', message:"Missing JSON list of 'docs'" };
const MISSING_DOC = { status:404, name:'not_found', message:'missing' };
const REV_CONFLICT = { status:409, name:'conflict', message:'Document update conflict' };
const INVALID_ID = { status:400, name:'bad_request', message:'_id field must contain a string' };
const MISSING_ID = { status:412, name:'missing_id', message:'_id is required for puts' };
const RESERVED_ID = { status:400, name:'bad_request', message:'Only reserved document ids may start with underscore.' };
const NOT_OPEN = { status:412, name:'precondition_failed', message:'Database not open' };
const UNKNOWN_ERROR = { status:500, name:'unknown_error', message:'Database encountered an unknown error' };
const BAD_ARG = { status:500, name:'badarg', message:'Some query argument is invalid' };
const INVALID_REQUEST = { status:400, name:'invalid_request', message:'Request was invalid' };
const QUERY_PARSE_ERROR = { status:400, name:'query_parse_error', message:'Some query parameter is invalid' };
const DOC_VALIDATION = { status:500, name:'doc_validation', message:'Bad special document member' };
const BAD_REQUEST = { status:400, name:'bad_request', message:'Something wrong with the request' };
const NOT_AN_OBJECT = { status:400, name:'bad_request', message:'Document must be a JSON object' };
const DB_MISSING = { status:404, name:'not_found', message:'Database not found' };
const IDB_ERROR = { status:500, name:'indexed_db_went_bad', message:'unknown' };
const WSQ_ERROR = { status:500, name:'web_sql_went_bad', message:'unknown' };
const LDB_ERROR = { status:500, name:'levelDB_went_went_bad', message:'unknown' };
const FORBIDDEN = { status:403, name:'forbidden', message:'Forbidden by design doc validate_doc_update function' };
const INVALID_REV = { status:400, name:'bad_request', message:'Invalid rev format' };
const FILE_EXISTS = { status:412, name:'file_exists', message:'The database could not be created, the file already exists.' };
const MISSING_STUB = { status:412, name:'missing_stub', message:'A pre-existing attachment stub wasn\'t found' };
const INVALID_URL = { status:413, name:'invalid_url', message:'Provided URL is invalid' };

function createError(error, reason) {
function CustomPouchError(reason) {
// inherit error properties from our parent error manually
// so as to allow proper JSON parsing.
var names = Object.getOwnPropertyNames(error);
for (var i = 0, len = names.length; i < len; i++) {
if (typeof error[names[i]] !== 'function') {
this[names[i]] = error[names[i]];
}
}
const pouchError = new PouchError(error.status, error.name, error.message);

if (this.stack === undefined) {
this.stack = (new Error()).stack;
// inherit error properties from our parent error manually
// so as to allow proper JSON parsing.
for (const name of Object.getOwnPropertyNames(error)) {
if (typeof error[name] !== 'function' && name !== 'message' && name !== 'status' && name !== 'error' && name !== 'name' && name !== 'stack') {
pouchError[name] = error[name];
}
}

if (reason !== undefined) {
this.reason = reason;
}
if (reason !== undefined) {
pouchError.reason = reason;
}
CustomPouchError.prototype = PouchError.prototype;
return new CustomPouchError(reason);

return pouchError;
}

function generateErrorFromResponse(err) {
Expand Down

0 comments on commit e750614

Please sign in to comment.