Giter Site home page Giter Site logo

erickwendel / let-it-crash-js Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 3.0 13 KB

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

Home Page: https://youtu.be/OO33Sr4lQDU

JavaScript 100.00%
asynclocalstorage context-tracking error-handling let-it-crash nodejs postgres sequelize vanilla-javascript advanced-javascript advanced-nodejs

let-it-crash-js's Introduction

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.

let-it-crash-js's People

Contributors

erickwendel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.