Git Product home page Git Product logo

calorie-counter's Introduction

Error handling

Custom errors

I define custom errors in appErrors.ts.

I create an AppError class and use it as super generic error object for my application. Why don't I use just Error? Cause I want the generic error to hold status codes.

Then I use AppError to create more specific error classes, if I am sure that I will use them again and again. For example, I use EmptyFieldError every time when I check objects for properties.

Throwing errors

Check recordsAPI.ts to see how I throw these errors.

If I use trycatch, in a catch statement I always put an error inside next(). That way it is always going to be caught by handleError middleware.

catch (error) {
    next(error);
}

Handle errors with middleware

handleErrors.ts โ€“ is a typical express error handler middleware, since it takes an error as a first argument. It's going to catch any error passed to the next() as an argument.

โ†’ Check express guide: Writing error handlers for more information about express error handler middleware

handleErrors is mounted the last in app.ts, so that it would be able to catch errors after controllers throw them.

Inside this middleware you can check caught Error for error name or status code and decide what to do with it depending on these values.

Life circle of an Error

Let's go to the life circle of an error.

  1. Throw an error some where

    if (typeof dateStr !== "string")
          throw new WrongTypeError({ date: dateStr }, "string");
  2. Catch-statement catches this error and sends it to the next()

    catch (error) {
        next(error);
    }
  3. handleError (error handler middleware) gets this error and decides what to do with it depending in its name or/and status code: logs it or/and sends appropriate response.

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.