Skip to content

Commit e9ef6a0

Browse files
Merge pull request #42 from contentstack/feat/DX-3457-improve-error-msgs
Updated error messages
2 parents be9df8a + f7a9105 commit e9ef6a0

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ const logger = require('morgan')
44
const rateLimit = require('express-rate-limit')
55
const app = express()
66
const nunjucks = require('nunjucks')
7-
const helmet = require('helmet');
7+
const helmet = require('helmet')
8+
const path = require('path')
9+
const messages = require(path.join(__dirname, 'config', 'messages'))
810

911
app.use(helmet());
1012

1113
const limiter = rateLimit({
1214
windowMs: 15 * 60 * 1000,
1315
max: 100,
14-
message: 'Too many requests from this IP, please try again after 15 minutes',
16+
message: messages.errors.rateLimitExceeded,
1517
standardHeaders: true,
1618
legacyHeaders: false,
1719
})

bin/www

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var path = require('path')
1010
var app = require('../app')
1111
var env = process.env.NODE_ENV || 'development'
1212
var config = require(path.join('..', 'config', env))
13+
var messages = require(path.join('..', 'config', 'messages'))
1314

1415
/**
1516
* Get port from environment and store in Express.
@@ -68,11 +69,11 @@ function onError (error) {
6869
// handle specific listen errors with friendly messages
6970
switch (error.code) {
7071
case 'EACCES':
71-
console.error(bind + ' requires elevated privileges')
72+
console.error(messages.errors.portElevatedPrivileges(bind))
7273
process.exit(1)
7374
break
7475
case 'EADDRINUSE':
75-
console.error(bind + ' is already in use');
76+
console.error(messages.errors.portInUse(bind))
7677
process.exit(1)
7778
break
7879
default:

config/messages.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Centralized messages for the application
3+
* Includes error messages, success messages, and informational messages
4+
*/
5+
6+
module.exports = {
7+
// Success Messages
8+
success: {
9+
contentstackConnected: 'Contentstack connection established.',
10+
},
11+
12+
// Error Messages
13+
errors: {
14+
// Server errors
15+
portElevatedPrivileges: (bind) => `Error: ${bind} requires elevated privileges. Try running with sudo or use a port above 1024.`,
16+
portInUse: (bind) => `Error: ${bind} is already in use. Please stop the other process or use a different port.`,
17+
18+
// Rate limiting
19+
rateLimitExceeded: 'Too many requests from this IP, please try again after 15 minutes.',
20+
21+
// Generic errors
22+
notFound: 'The requested resource was not found.',
23+
internalServerError: 'An internal server error occurred.',
24+
},
25+
26+
// Informational Messages
27+
info: {
28+
serverStarting: (port) => `Server starting on port ${port}`,
29+
serverListening: (bind) => `Listening on ${bind}`,
30+
}
31+
}
32+

models/contentstack.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var path = require('path')
22
var env = process.env.NODE_ENV || 'development'
33
var config = require(path.join('..', 'config', env))
4+
var messages = require(path.join('..', 'config', 'messages'))
45
const Contentstack = require(config.sdk).Contentstack
56

67
const Stack = Contentstack.Stack(config)
78

89
Stack.connect(config.contentStore).then(() => {
9-
console.log("Connected")
10+
console.log(messages.success.contentstackConnected)
1011
}).catch(console.error)
1112

1213

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"express": "^4.21.2",
1717
"express-rate-limit": "^7.1.5",
1818
"http-errors": "~2.0.0",
19-
"morgan": "~1.10.0",
19+
"morgan": "~1.10.1",
2020
"nunjucks": "^3.2.4",
2121
"path": "^0.12.7"
2222
},

0 commit comments

Comments
 (0)