Skip to content

Commit

Permalink
🐛 Fix AppError class conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
maqsudtolipov committed Sep 7, 2024
1 parent 6fa4d23 commit 71212c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions server/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ exports.login = catchAsync(async (req, res, next) => {
};

if (!email || !password) {
return next(new AppError(401, 'Invalid email or password'));
return next(new AppError('Invalid email or password', 401));
}

const user = await User.findOne({ email }).select('+password');

if (!user || !(await user.comparePassword(password, user.password))) {
return next(new AppError(401, 'Invalid email or password'));
return next(new AppError('Invalid email or password', 401));
}
user.password = undefined;

Expand All @@ -78,15 +78,15 @@ exports.protect = catchAsync(async (req, res, next) => {
const accessToken = req.cookies['access-token'];

if (!accessToken) {
return next(new AppError(401, '🎫 Token not found'));
return next(new AppError('🎫 Token not found', 401));
}

const decoded = jwt.verify(accessToken, process.env.ACCESS_SECRET);

const user = await User.findById(decoded.id).select('+role');

if (!user) {
return next(new AppError(401, '🎫 Invalid token'));
return next(new AppError('🎫 Invalid token', 401));
}

req.user = user;
Expand Down
21 changes: 15 additions & 6 deletions server/controllers/errorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken');
const AppError = require('../utils/appError');

const handleValidatorError = (error) => {
return new AppError(403, error.message);
return new AppError(error.message, 401);
};

const handleAccessTokenExpiredError = (res) => {
Expand All @@ -27,7 +27,7 @@ const handleDuplicationError = (error) => {
message = 'User already exists. Please log in.';
}

return new AppError(409, message);
return new AppError(message, 409);
};

const errorController = (err, req, res, next) => {
Expand All @@ -46,10 +46,19 @@ const errorController = (err, req, res, next) => {
handleAccessTokenInvalidError(res);
}

res.status(error.statusCode).json({
status: error.status,
message: error.message,
});
if (error.isOperational) {
res.status(error.statusCode).json({
status: error.status,
message: error.message,
});
} else {
console.log('💥 ERROR', error);

res.status(500).json({
status: 'error',
message: '💥 Something went wrong.',
});
}
};

module.exports = errorController;
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ app.use('/api/v1/auth', authRouter);
app.use('/api/v1/songs', songRouter);

app.all('*', (req, res, next) => {
next(new AppError(404, `Can't find '${req.originalUrl}' on this server`));
next(new AppError(`Can't find '${req.originalUrl}' on this server`, 404));
});

// Error handler
Expand Down

0 comments on commit 71212c6

Please sign in to comment.