Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware#16
Merged
ayshadogo merged 5 commits intoDfunder:mainfrom Mar 2, 2026
Conversation
Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware
…-Error-Handling-Middleware
set up the npm run lint to check for errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
i have successfully completed all the required tasks for configuring environment variables and setting up global error handling middleware:
✅ Completed Tasks:
Created .env.example with all required keys:
PORT, MONGO_URI, JWT_SECRET, JWT_REFRESH_SECRET, JWT_EXPIRES_IN
EMAIL_HOST, EMAIL_PORT, EMAIL_USER, EMAIL_PASS
STELLAR_HORIZON_URL, STELLAR_NETWORK
Also included NODE_ENV and ALLOWED_ORIGINS for completeness
Created .env for local development:
Added a properly configured .env file with realistic values for local development
The file is properly added to .gitignore
Added config/env.js to validate required variables:
Created a validation function that checks for all required environment variables
Throws a descriptive error with a list of missing variables if any are missing
Exits the process with code 1 when required variables are missing
Created src/middlewares/errorHandler.js for centralized error handling:
Handles ValidationError (Mongoose validation errors)
Handles CastError (invalid ObjectId format)
Handles MongoDB duplicate key errors (11000)
Handles JWT errors (JsonWebTokenError, TokenExpiredError)
Returns consistent error shape: { success: false, message: "...", errors: [...] }
Includes stack trace in development mode for debugging
Registered middleware in app.js:
Added the error handler middleware at the bottom of app.js as required
Ensured it's registered after all routes to catch unhandled errors
🧪 Verification:

The environment variable validation successfully runs when the server starts
When required variables are missing, the application throws a descriptive error
The error handler middleware will catch and format errors consistently
All unhandled errors will return a JSON response with the correct HTTP status code
The implementation meets all acceptance criteria:
App throws a descriptive error at startup if any required env variables are missing
All unhandled errors return a JSON response with the correct HTTP status code
The error format is consistent across the application
The middleware handles the specified error types (ValidationError, CastError, MongoDB 11000 errors
Closes #2