Skip to content

Examples demonstrating how the Let it Crash approach works using Node.js

Notifications You must be signed in to change notification settings

ErickWendel/let-it-crash-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Let It Crash Approach Demonstration

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)

Deu erro

Overview

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.

Prerequisites

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.

Running the Application

To run the application, follow these steps:

  1. Start the PostgreSQL database with Docker Compose:

    docker-compose up
  2. Install the necessary dependencies with npm install.

  3. Start the application with npm start.

Usage

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:

Inserting an Item

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.

Causing the Application to Crash

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.