Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web-et-numerique
Factory
Resin
Server
Commits
60b991dc
Commit
60b991dc
authored
1 year ago
by
Etienne LOUPIAS
Browse files
Options
Downloads
Patches
Plain Diff
wip exception handler
parent
145d0da8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main.ts
+40
-0
40 additions, 0 deletions
src/main.ts
src/shared/exception.service.ts
+27
-0
27 additions, 0 deletions
src/shared/exception.service.ts
with
67 additions
and
0 deletions
src/main.ts
+
40
−
0
View file @
60b991dc
...
...
@@ -2,6 +2,7 @@ import { ValidationPipe } from '@nestjs/common';
import
{
NestFactory
}
from
'
@nestjs/core
'
;
import
{
DocumentBuilder
,
SwaggerModule
}
from
'
@nestjs/swagger
'
;
import
{
AppModule
}
from
'
./app.module
'
;
import
{
MailerService
}
from
'
./mailer/mailer.service
'
;
async
function
bootstrap
()
{
const
app
=
await
NestFactory
.
create
(
AppModule
,
{
...
...
@@ -9,6 +10,44 @@ async function bootstrap() {
process
.
env
.
NODE_ENV
===
'
production
'
?
[
'
error
'
,
'
warn
'
,
'
log
'
]
:
[
'
log
'
,
'
debug
'
,
'
error
'
,
'
verbose
'
,
'
warn
'
],
});
app
.
useGlobalPipes
(
new
ValidationPipe
());
//const exceptionService = app.get(ExceptionService);
/*
const mailerService = app.get(MailerService);
process.on('uncaughtException', async (err) => {
//exceptionService.handleUncaughtException(err);
console.log('Uncaught Exception:', err);
try {
await mailerService.send(
'to_complete@xxx.com',
'Uncaught Exception in NestJS Application',
`An uncaught exception occurred: <br />${err.name} : ${err.message}<br />${err.stack}`
);
} catch (e) {
console.log('Uncaught Exception process failed:', e);
}
// Exit the application on an uncaught exception to maintain stability and facilitate debugging
process.exit(1);
});
process.on('unhandledRejection', async (reason, promise) => {
console.log('Unhandled rejection at ', promise, `reason: ${reason}`);
try {
await mailerService.send(
'to_complete@xxx.com',
'Unhandled rejection in NestJS Application',
`An unhandled rejection occurred: <br />${JSON.stringify(promise)}<br />reason: ${reason}`
);
} catch (e) {
console.log('Unhandled rejection process failed:', e);
}
// Exit the application on an unhandled rejection to maintain stability and facilitate debugging
process.exit(1);
});
*/
const
options
=
new
DocumentBuilder
()
.
setTitle
(
`Res'in`
)
.
setDescription
(
`Res'in API documentation`
)
...
...
@@ -18,6 +57,7 @@ async function bootstrap() {
app
.
setGlobalPrefix
(
'
api
'
);
const
document
=
SwaggerModule
.
createDocument
(
app
,
options
);
SwaggerModule
.
setup
(
'
api/doc
'
,
app
,
document
);
await
app
.
listen
(
3000
);
}
bootstrap
();
This diff is collapsed.
Click to expand it.
src/shared/exception.service.ts
0 → 100644
+
27
−
0
View file @
60b991dc
import
{
Injectable
}
from
'
@nestjs/common
'
;
import
{
MailerService
}
from
'
../mailer/mailer.service
'
;
@
Injectable
()
export
class
ExceptionService
{
constructor
(
private
readonly
mailerService
:
MailerService
)
{}
handleUncaughtException
(
err
:
Error
)
{
console
.
log
(
'
Uncaught Exception :
'
,
err
);
this
.
mailerService
.
send
(
'
eloupias@grandlyon.com
'
,
'
Uncaught Exception in NestJS Application
'
,
`An uncaught exception occurred:
${
JSON
.
stringify
(
err
)}
`
);
}
handleUnhandledRejection
(
reason
:
any
)
{
console
.
log
(
'
Unhandled Rejection:
'
,
reason
);
this
.
mailerService
.
send
(
'
eloupias@grandlyon.com
'
,
'
Unhandled Promise Rejection in NestJS Application
'
,
`An unhandled promise rejection occurred:
${
reason
}
`
);
}
}
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