- Notifications
You must be signed in to change notification settings - Fork5
Examples demonstrating how the Let it Crash approach works using Node.js
ErickWendel/let-it-crash-js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This application is a demonstration of the "Let It Crash" approach in software development. The "Let It Crash" model is a design philosophy in system development where the system is designed to stop and restart in response to certain types of errors, rather than attempting to account for all possible error conditions.
This is complementary content of my video aboutLet It Crash approach in JavaScript (pt-br)
In this application, I use Node.js'Async LocalStorage to retrieve the response object from a global handle. This allows us to respond to individual customers even in the event of a critical error.
If a database connection is off, the application will prevent receiving new connections, stop the current database connection, and then terminate. This approach allows the system to recover from errors quickly and continue operating, rather than attempting to handle every possible error condition and potentially causing more problems.
Before you can run this application, you must have the following installed:
These are used to spin up the PostgreSQL database that the application interacts with.
To run the application, follow these steps:
Start the PostgreSQL database with Docker Compose:
docker-compose up
Install the necessary dependencies with
npm install
.Start the application with
npm start
.
This application exposes an API endpoint atlocalhost:3000
that accepts JSON payloads. The endpoint interacts with a PostgreSQL database, which is spun up using Docker Compose.
Here are some examples of how to use the API:
You can insert an item into the database by sending a POST request with a JSON payload. Here's an example usingcurl
:
curl \ -H"Content-Type: application/json" \ -d'{"name":"John Wick", "power": "superhuman strength"}' \ localhost:3000
This will insert a new item with the name "John Wick" and the power "superhuman strength" into the database.
You can stop the docker compose instance and trigger the payload below:
curl \ -H"Content-Type: application/json" \ -d'{"name":"John Wick", "power": "superhuman strength"}' \ localhost:3000
which will log:
postgres is runningserver running at 3000req: [387] crashed but will be nicely handled!unhandledRejection received!message: SequelizeConnectionRefusedErrorhttp server closed
This will cause Node.js to throw an unhandled error as it doesn't have any try/catch block. Using the Node.js' AsyncLocalStorage API it will be able to retrieve the context of the user that caused the exception and reply directly to him before stopping the application.
About
Examples demonstrating how the Let it Crash approach works using Node.js
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.