• Stars
    star
    274
  • Rank 147,230 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP Error collection to use in your next REST API.

throw.js

An HTTP Error collection to use in your next REST API.

Installation

npm install --save throw.js

or

yarn add throw.js

Example

const express = require("express");
const app = express();
const { NotFound } = require("throw.js");
const logger = require("winston");

app.get("/", (req, res, next) => {
    next(new NotFound());
});

app.use((err, req, res, next) => {
    logger.error(err);

    if (req.app.get("env") !== "development" && req.app.get("env") !== "test") {
        delete err.stack;
    }

    res.status(err.statusCode || 500).json(err);
});

app.listen(3000);

Custom Errors

const { CustomError } = require("throw.js");

throw new CustomError(message, statusCode?, errorCode?, originalError?);

Parameters:

  • message[optional]: Detailed message of this error
  • statusCode[optional]: The HTTP Status number to return
  • errorCode[optional]: An internal unique code identifier of this error

Stacking Errors

const { CustomError } = require("throw.js");

try {
    // throws an error
} catch (e) {
    // Pass the error as a parameter to keep its original stack trace
    throw new CustomError(message, statusCode, errorCode, e);
}

Errors

All of the classes below have all parameters set up by default, based on RFC7231. But you can override the message and the errorCode to fit your for personal needs.

400 Bad Request

BadRequest(message?, errorCode?, originalError?);

401 Unauthorized

Unauthorized(message?, errorCode?, originalError?);

402 Payment Required

PaymentRequired(message?, errorCode?, originalError?);

403 Forbidden

Forbidden(message?, errorCode?, originalError?);

404 Not Found

NotFound(message?, errorCode?, originalError?);

405 Method Not Allowed

MethodNotAllowed(message?, errorCode?, originalError?);

406 Not Acceptable

NotAcceptable(message?, errorCode?, originalError?);

407 Proxy Authentication Required

ProxyAuthenticationRequired(message?, errorCode?, originalError?);

408 Request Timeout

RequestTimeout(message?, errorCode?, originalError?);

409 Conflict

Conflict(message?, errorCode?, originalError?);

410 Gone

Gone(message?, errorCode?, originalError?);

422 Unprocessable Entity

UnprocessableEntity(message?, errorCode?, originalError?);

424 Failed Dependency

FailedDependency(message?, errorCode?, originalError?);

500 Internal Server Error

InternalServerError(message?, errorCode?, originalError?);

501 Not Implemented

NotImplemented(message?, errorCode?, originalError?);

502 Bad Gateway

BadGateway(message?, errorCode?, originalError?);

503 Service Unavailable

ServiceUnavailable(message?, errorCode?, originalError?);

504 Gateway Timeout

GatewayTimeout(message?, errorCode?, originalError?);

505 HTTP Version Not Supported

HttpVersionNotSupported(message?, errorCode?, originalError?);

511 Network Authentication Required

NetworkAuthenticationRequired(message?, errorCode?, originalError?);

TODO

  • Implement more Error Classes

More Repositories

1

templates

A set of standard document templates.
2,010
star
2

feedly-api

PHP wrapper around Feedly's REST API
PHP
122
star
3

documer

Bayes algorithm implementation with PHP
PHP
77
star
4

ansible-nodejs-digitalocean

Complete playbook to create and deploy a Node.js application on Digital Ocean.
Jinja
75
star
5

kostasbariotis.com

My Gatsby.js powered personal homepage
JavaScript
65
star
6

yeelight.js

Control your Yeelight devices with Node.js
JavaScript
29
star
7

node-cron-expression

Declarative functional cron expression builder
TypeScript
26
star
8

presentador

πŸ“½ An opinionated presentation framework. Just write what you want to present and it will do the rest.
JavaScript
25
star
9

can-i

Authorization service for a Microservices Architecture
JavaScript
23
star
10

gityeller

Stay in the loop of your favorite Github repositories.
JavaScript
20
star
11

webpack-patterns

A list of common webpack patterns
JavaScript
14
star
12

meanimum

Meanimum is my Mongo/Express/Angular/Node Boilerplate
JavaScript
13
star
13

gatsby-tutorial

Source code for the tutorial
JavaScript
12
star
14

simple-static-site-generator

Just render some HTML already!
JavaScript
9
star
15

sqs-lambda-consumer

Create a sequential pipeline of processing messages with AWS SQS and Lambda.
JavaScript
8
star
16

typescript-graphql-server

A simple Graphql server with TypeScript
TypeScript
8
star
17

gatsby-plugin-paginate

Plug and play pagination plugin for Gatsby
JavaScript
8
star
18

quoteme.js_Unmaintained_

Medium-like, Share to Twitter popup button on text selection
JavaScript
8
star
19

go-discover

Discover what your peers are doing in GitHub
Go
7
star
20

parcel-bootstrap-pug

Playing around with Parcel and Bootstrap
JavaScript
6
star
21

KBariotis_NBP

National Bank of Greece as a Payment Gateway for Magento
PHP
5
star
22

Dijkstrer

Dijkstrer is a C application which calculate the shor path of a graph using Dijkstra's Algorithm. Dijkstrer uses MPI library to implement cluster functionality where every member of the cluster calculates each of a node's childs. Dijkstrer will use each process to calculate each child of every node. See README file for more info.
C
5
star
23

wsmanager

An easy to use WebSocket account manager
JavaScript
5
star
24

kostasbariotis.com__ghost-theme

This is my Ghost's theme for http://kostasbariotis.com
CSS
4
star
25

devastation-podcast

Devastation Podcast - Talking about dev in Greek
HTML
4
star
26

chrome-guides

Chrome Extension for Photoshop-like Guides
JavaScript
3
star
27

async-jobs-ui

UI to monitor async jobs and processes.
JavaScript
3
star
28

nodejs-mongo-geospatial-queries

MongoDB Geospatial Queries PoC with Node.js
JavaScript
3
star
29

aws-static-site

🐣 Deploy a static site to AWS, made easy
JavaScript
2
star
30

static-woocommerce-frontend

CSS
2
star
31

gridorous

CSS grid exposed as a function to be used in CSS Modules
JavaScript
2
star
32

acdev

JavaScript
1
star
33

bestbeachesin.gr

HTML
1
star
34

covid-19-stats

1
star
35

KBariotis_ProductFeeds

Add Product Feeds to your store for http://skroutz.gr, http://bestprice.gr, http://snif.gr
PHP
1
star
36

dotfiles

Shell
1
star
37

example

JavaScript
1
star
38

slackzones.com

Generate timezones heat maps from your Slack channels
JavaScript
1
star
39

git-workshop

HTML
1
star
40

mailgunee

Preview your emails sent through mailgun
JavaScript
1
star
41

phpless

PHP Less Assets Management
PHP
1
star
42

HTTPHeaderElucidator

HTTP Headers behavior for humans
JavaScript
1
star