500 Internal Server Error
Server Error - Something went wrong on the server
HTTP 500 Internal Server Error
What It Means
The HTTP 500 Internal Server Error is a generic error indicating that the server encountered an unexpected condition that prevented it from fulfilling the request. It's the server saying "something broke, but I'm not telling you what."
Common Causes
- Unhandled exceptions: Code threw an error that wasn't caught
- Database connection failed: Can't connect to database
- Null pointer/undefined: Accessing property on null object
- Out of memory: Server ran out of RAM
- Misconfiguration: Wrong env vars, missing files
- Permission errors: Can't read/write required files
- Dependency failure: Required service is down
How to Debug
# Check application logs
tail -f /var/log/app/error.log
journalctl -u myapp -f
# Check web server logs
tail -f /var/log/nginx/error.log
# Check system resources
htop
free -m
df -h
Error Handling Best Practices
Express.js error handler
// Global error handler (add last)
app.use((err, req, res, next) => {
// Log the full error
console.error(err.stack);
// Don't leak error details in production
const message = process.env.NODE_ENV === 'production'
? 'Internal Server Error'
: err.message;
res.status(500).json({
error: 'Internal Server Error',
message,
requestId: req.id // For support tickets
});
});
Async error wrapper
// Wrap async handlers to catch errors
const asyncHandler = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
app.get('/api/users', asyncHandler(async (req, res) => {
const users = await User.find(); // Errors caught automatically
res.json(users);
}));
5xx Status Code Guide
| Code | Use When |
|---|---|
| 500 | Generic server error |
| 502 | Bad response from upstream |
| 503 | Temporarily unavailable |
| 504 | Upstream timeout |
Prevention Strategies
- Add comprehensive error handling throughout your code
- Use structured logging with request IDs
- Set up error monitoring (Sentry, Bugsnag)
- Implement health checks for dependencies
- Add circuit breakers for external services
- Use graceful degradation when possible
Security Note
Never expose stack traces in production. They reveal internal implementation details that could help attackers. Log full errors server-side, return generic messages to clients.