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 about Let 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 at localhost: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 using curl
:
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 running
server running at 3000
req: [387] crashed but will be nicely handled!
unhandledRejection received!
message: SequelizeConnectionRefusedError
http 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.