remove PayloadError and switched to APIError where it was used, set res status where possible and some fixed eslint issues
This commit is contained in:
@@ -19,7 +19,7 @@ class ExtendableError extends Error {
|
||||
* Class representing an API error.
|
||||
* @extends ExtendableError
|
||||
*/
|
||||
class APIError extends ExtendableError {
|
||||
export class APIError extends ExtendableError {
|
||||
/**
|
||||
* Creates an API error.
|
||||
* @param {string} message - Error message.
|
||||
@@ -30,5 +30,3 @@ class APIError extends ExtendableError {
|
||||
super(message, status, isPublic);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = APIError;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PayloadError } from './PayloadError';
|
||||
import { APIError } from './APIError';
|
||||
|
||||
export class DuplicateCollection extends PayloadError {
|
||||
export class DuplicateCollection extends APIError {
|
||||
constructor(config) {
|
||||
super(`Collection name "${config.labels.singular}" is already in use`);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PayloadError } from './PayloadError';
|
||||
import { APIError } from './APIError';
|
||||
|
||||
export class DuplicateGlobal extends PayloadError {
|
||||
export class DuplicateGlobal extends APIError {
|
||||
constructor(config) {
|
||||
super(`Global label "${config.label}" is already in use`);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { PayloadError } from './PayloadError';
|
||||
import { APIError } from './APIError';
|
||||
|
||||
export class ExistingUser extends PayloadError {
|
||||
export class ExistingUser extends APIError {
|
||||
constructor(config) {
|
||||
super(`Error when registering ${config.labels.singular}: a user model already exists.`);
|
||||
super(`Error when registering "${ config.labels.singular }": a user model already exists.`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PayloadError } from './PayloadError';
|
||||
import { APIError } from './APIError';
|
||||
|
||||
export class MissingCollectionLabel extends PayloadError {
|
||||
export class MissingCollectionLabel extends APIError {
|
||||
constructor(config) {
|
||||
super('payload.config.collection object is missing label');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PayloadError } from './PayloadError';
|
||||
import { APIError } from './APIError';
|
||||
|
||||
export class MissingGlobalLabel extends PayloadError {
|
||||
export class MissingGlobalLabel extends APIError {
|
||||
constructor(config) {
|
||||
super(`${config.globals} object is missing label`);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { PayloadError } from './PayloadError';
|
||||
import httpStatus from 'http-status';
|
||||
import { APIError } from './APIError';
|
||||
|
||||
export class NotFound extends PayloadError {
|
||||
export class NotFound extends APIError {
|
||||
constructor() {
|
||||
super('Not found.');
|
||||
super('Not found.', httpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
export class PayloadError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
export { PayloadError } from './PayloadError';
|
||||
export { APIError } from './APIError';
|
||||
export { DuplicateCollection } from './DuplicateCollection';
|
||||
export { DuplicateGlobal } from './DuplicateGlobal';
|
||||
export { MissingCollectionLabel } from './MissingCollectionLabel';
|
||||
|
||||
@@ -4,10 +4,11 @@ import { createAutopopulateOptions } from '../mongoose/createAutopopulateOptions
|
||||
|
||||
export const upsert = (req, res) => {
|
||||
if (!req.model.schema.tree[req.params.slug]) {
|
||||
return res.status(httpStatus.NOT_FOUND).json({ error: 'not found' });
|
||||
res.status(httpStatus.NOT_FOUND).json({ error: 'not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
req.model.findOne({}, (err, doc) => {
|
||||
req.model.findOne({}, (findErr, doc) => {
|
||||
let global = {};
|
||||
|
||||
if (!doc) {
|
||||
@@ -16,8 +17,8 @@ export const upsert = (req, res) => {
|
||||
} else {
|
||||
global = req.body;
|
||||
}
|
||||
return req.model.create(global, (err, result) => {
|
||||
if (err) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
|
||||
return req.model.create(global, (createErr, result) => {
|
||||
if (createErr) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: createErr });
|
||||
|
||||
return res.status(httpStatus.CREATED)
|
||||
.json({
|
||||
|
||||
@@ -8,7 +8,7 @@ const create = (req, res) => {
|
||||
return res.status(httpStatus.CREATED)
|
||||
.json({
|
||||
message: 'success',
|
||||
result: result.toJSON({ virtuals: true })
|
||||
result: result.toJSON({ virtuals: true }),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
import httpStatus from 'http-status';
|
||||
import { modelById } from '../resolvers';
|
||||
import { createAutopopulateOptions } from '../createAutopopulateOptions';
|
||||
|
||||
const findOne = (req, res) => {
|
||||
|
||||
const query = {
|
||||
Model: req.model,
|
||||
id: req.params.id,
|
||||
locale: req.locale,
|
||||
fallback: req.query['fallback-locale'],
|
||||
depth: req.query.depth
|
||||
depth: req.query.depth,
|
||||
};
|
||||
|
||||
modelById(query, createAutopopulateOptions(query))
|
||||
.then(doc => res.json(doc))
|
||||
.catch(err => res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }));
|
||||
.catch(err => res.status(err.status).json({ error: err }));
|
||||
};
|
||||
|
||||
export default findOne;
|
||||
|
||||
@@ -8,15 +8,15 @@ const query = (req, res) => {
|
||||
}
|
||||
return res.json({
|
||||
...result,
|
||||
docs: result.docs.map(doc => {
|
||||
docs: result.docs.map((doc) => {
|
||||
if (req.locale && doc.setLocale) {
|
||||
doc.setLocale(req.locale, req.query['fallback-locale']);
|
||||
}
|
||||
|
||||
return doc.toJSON({ virtuals: true })
|
||||
})
|
||||
return doc.toJSON({ virtuals: true });
|
||||
}),
|
||||
});
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
export default query;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { APIError } from '../../errors';
|
||||
|
||||
const create = query => {
|
||||
const create = (query) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
query.Model.create(query.input, (err, doc) => {
|
||||
console.log(err, doc);
|
||||
if (err || !doc) {
|
||||
return reject({message: err})
|
||||
reject(new APIError(err));
|
||||
return;
|
||||
}
|
||||
resolve(doc);
|
||||
});
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { NotFound } from '../../errors';
|
||||
|
||||
const find = (query) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
query.Model.find({}, (err, docs) => {
|
||||
if (err || !docs) {
|
||||
return reject({ message: 'not found' });
|
||||
reject(new NotFound());
|
||||
}
|
||||
|
||||
let result = docs;
|
||||
|
||||
if (query.locale) {
|
||||
docs.setLocale(query.locale, query.fallback);
|
||||
const json = docs.toJSON({ virtuals: true });
|
||||
result = json;
|
||||
result = docs.toJSON({ virtuals: true });
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
import NotFound from '../../errors';
|
||||
|
||||
const find = ({ Model, locale, fallback }) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
Model.findOne(null, (err, doc) => {
|
||||
if (err || !doc) {
|
||||
return reject({ message: 'not found' });
|
||||
console.log('ok');
|
||||
reject(new NotFound());
|
||||
}
|
||||
|
||||
let result = doc;
|
||||
|
||||
if (locale) {
|
||||
doc.setLocale(locale, fallback);
|
||||
const json = doc.toJSON({ virtuals: true });
|
||||
result = json;
|
||||
result = doc.toJSON({ virtuals: true });
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
|
||||
@@ -7,5 +7,5 @@ export {
|
||||
modelById,
|
||||
find,
|
||||
findOne,
|
||||
create
|
||||
}
|
||||
create,
|
||||
};
|
||||
|
||||
@@ -1,24 +1,25 @@
|
||||
const modelById = (query, options) => {
|
||||
import { NotFound } from '../../errors';
|
||||
|
||||
const modelById = (query, options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
query.Model.findOne({ _id: query.id }, {}, options, (err, doc) => {
|
||||
|
||||
if (err || !doc) {
|
||||
return reject({ message: 'not found' })
|
||||
reject(new NotFound());
|
||||
return;
|
||||
}
|
||||
|
||||
let result = doc;
|
||||
|
||||
if (query.locale) {
|
||||
doc.setLocale && doc.setLocale(query.locale, query.fallback);
|
||||
if (doc.setLocale) doc.setLocale(query.locale, query.fallback);
|
||||
result = doc.toJSON({ virtuals: true });
|
||||
}
|
||||
|
||||
resolve(options.returnRawDoc
|
||||
? doc
|
||||
: result);
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default modelById;
|
||||
|
||||
Reference in New Issue
Block a user