Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update post.js #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/errors/ApiError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ class ApiError {
this.code = code;
}
}

// Is this class doing anything besides receiving data? what is the return?
// Take a look at this Method in the Erro object: Error.captureStackTrace(this);
// Also, read this: https://stackoverflow.com/questions/59625425/understanding-error-capturestacktrace-and-stack-trace-persistance
export default ApiError;
1 change: 1 addition & 0 deletions src/middlewares/handleErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ApiError from '../errors/ApiError';

export default function ErrorHandler(error, req, res, next) {
//Ok, now I got why you created the de ApiError class. I thought you were going to throw the stack traces. You may "ignore" what I commented, but don't forget to read about it.
if (error instanceof ApiError) {
return res.status(error.code).send({
error: {
Expand Down
2 changes: 2 additions & 0 deletions src/routes/photos.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const upload = multer({
|| file.mimetype === 'image/jpeg'
) {
cb(null, true);
// try to always avoid the use of "else" statement. In this case you can just return cb(null, true)
} else {
// Why do you declare cb(null,false), and right after return it differently (as an ApiError instance)?
cb(null, false);
return cb(new ApiError(400, 'Only .png, .jpg and .jpeg format allowed!'));
}
Expand Down
2 changes: 1 addition & 1 deletion src/validation/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from '@hapi/joi';

// Switch it 'npm install joi'
const createPhoto = Joi.object({
caption: Joi.string().min(1).max(266).trim()
.required(),
Expand Down