Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CaCaoCritics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bilel El Yaagoubi
CaCaoCritics
Commits
8099a147
Commit
8099a147
authored
4 years ago
by
Louis-Marie Michelin
Browse files
Options
Downloads
Patches
Plain Diff
fix: improve error handler
parent
6f1f5ee3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/server.js
+1
-1
1 addition, 1 deletion
backend/server.js
backend/services/jsonErrorHandler.js
+8
-14
8 additions, 14 deletions
backend/services/jsonErrorHandler.js
with
9 additions
and
15 deletions
backend/server.js
+
1
−
1
View file @
8099a147
...
@@ -2,10 +2,10 @@ const express = require("express");
...
@@ -2,10 +2,10 @@ const express = require("express");
const
logger
=
require
(
"
morgan
"
);
const
logger
=
require
(
"
morgan
"
);
const
cors
=
require
(
"
cors
"
);
const
cors
=
require
(
"
cors
"
);
const
mongoose
=
require
(
"
mongoose
"
);
const
mongoose
=
require
(
"
mongoose
"
);
const
jsonErrorHandler
=
require
(
"
./services/jsonErrorHandler
"
);
const
indexRouter
=
require
(
"
./routes/index
"
);
const
indexRouter
=
require
(
"
./routes/index
"
);
const
usersRouter
=
require
(
"
./routes/users
"
);
const
usersRouter
=
require
(
"
./routes/users
"
);
const
routeNotFoundJsonHandler
=
require
(
"
./services/routeNotFoundJsonHandler
"
);
const
routeNotFoundJsonHandler
=
require
(
"
./services/routeNotFoundJsonHandler
"
);
const
jsonErrorHandler
=
require
(
"
./services/jsonErrorHandler
"
);
mongoose
.
connect
(
process
.
env
.
MONGO_DB_URL
,
{
mongoose
.
connect
(
process
.
env
.
MONGO_DB_URL
,
{
useNewUrlParser
:
true
,
useNewUrlParser
:
true
,
...
...
This diff is collapsed.
Click to expand it.
backend/services/jsonErrorHandler.js
+
8
−
14
View file @
8099a147
// Custom error handler to send unhandled errors in JSON instead of HTML.
// Custom error handler to send unhandled errors in JSON instead of HTML.
// The error-handling middleware is a special type of Express middleware
// The error-handling middleware is a special type of Express middleware
// that needs to have four arguments as opposed to a regular middleware.
// that needs to have four arguments as opposed to a regular middleware.
// eslint-disable-next-line no-unused-vars
const
jsonErrorHandler
=
function
(
error
,
req
,
res
,
next
)
{
const
jsonErrorHandler
=
function
(
error
,
req
,
res
,
next
)
{
console
.
error
(
error
);
if
(
!
res
.
headersSent
)
{
if
(
res
.
headersSent
)
return
next
(
error
);
if
(
process
.
env
.
NODE_ENV
===
"
development
"
)
{
if
(
process
.
env
.
NODE_ENV
===
"
development
"
)
{
return
res
.
status
(
500
).
json
({
res
.
status
(
500
).
json
({
message
:
error
.
toString
()
});
message
:
error
.
toString
(),
stackTrace
:
error
.
stack
,
});
}
else
{
}
else
{
// Hide error details in production to avoid security issues
// Hide error details in production to avoid security issues
return
res
.
status
(
500
).
json
({
res
.
status
(
500
).
json
({
message
:
"
Internal server error
"
});
message
:
"
Internal server error
"
,
}
});
}
}
next
(
error
);
};
};
module
.
exports
=
jsonErrorHandler
;
module
.
exports
=
jsonErrorHandler
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment