express-error-handle
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

express-error-handle

A graceful error handle for Express applications. This also patches a DOS exploit where users can manually trigger bad request errors that shut down your app.

Installation

npm i --save express-error-handle

Express express-error-handle

//If the import statement doesn't work, use require module
const {errorLog,errorHandlerNotify} = require("express-error-handle") //use require
const express = require('express');

const app = express()
const port = process.env.PORT || 5000;

app.get('/a-typical-route',async function(req, res, next) {
try{

//your code here

}
catch(error){
next(error)
}

});


app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

//handling error using at the end of last routes
//app.use(errorLog);  //optional
app.use(errorHandlerNotify); //required

Supported environment variables

//mongoose default model error handle
//js example


const mongoose,{Schema} = require('mongoose')
const breakfastSchema = new Schema({
  eggs: {
    type: Number,
    min: [6, 'Too few eggs'],
    max: 12
  },
  bacon: {
    type: Number,
    required: [true, 'Why no bacon?']
  },
  drink: {
    type: String,
    enum: ['Coffee', 'Tea'],
  },
  userId:{
    type: Schema.Types.ObjectId,
    ref: 'User'
}

}, { timestamps: true });
const Breakfast = mongoose.model('Breakfast', breakfastSchema);
export default Breakfast;

//mongoose default model error handle
//ts example

import { Document, Schema, model, Types } from 'mongoose';


export interface IBreakfastSchema extends Document {
    eggs: number,
    bacon: Types.ObjectId,
    drink:string,
    userId?:Types.ObjectId,
    createdAt?: Date,
    updatedAt?: Date,
}
export enum drinkEnum{
    coffee="coffee",
    tea="tea",
}
const breakfastSchema = new Schema<IBreakfastSchema>({
  eggs: {
    type: Number,
    min: [6, 'Too few eggs'],
    max: 12
  },
  bacon: {
    type: Number,
    required: [true, 'Why no bacon?']
  },
  drink: {
    type: String,
    enum: ['coffee', 'tea'],
    enum: Object.values(drinkEnum)
  }
  userId:{
    type: Schema.Types.ObjectId, ref: 'User',
  }
}, { timestamps: true });
const Breakfast =  model<IBreakfastSchema>("Breakfast", breakfastSchema);
export default Breakfast;

Package Sidebar

Install

npm i express-error-handle

Weekly Downloads

3

Version

0.1.8

License

ISC

Unpacked Size

8.24 kB

Total Files

7

Last publish

Collaborators

  • 1emonislam