Skip to content

Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware#16

Merged
ayshadogo merged 5 commits intoDfunder:mainfrom
shamoo53:Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware
Mar 2, 2026
Merged

Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware#16
ayshadogo merged 5 commits intoDfunder:mainfrom
shamoo53:Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware

Conversation

@shamoo53
Copy link
Copy Markdown
Contributor

@shamoo53 shamoo53 commented Mar 1, 2026

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
image
Closes #2

shamoo53 added 5 commits March 1, 2026 16:42
Configure-Environment-Variables-Setup-Global-Error-Handling-Middleware
set up the npm run lint  to check for errors
@ayshadogo ayshadogo merged commit 1448230 into Dfunder:main Mar 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configure Environment Variables & Setup Global Error Handling Middleware

2 participants