Coder Social home page Coder Social logo

uk-gov-mirror / dfe-digital.login.dfe.express-error-handling Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dfe-digital/login.dfe.express-error-handling

0.0 0.0 0.0 382 KB

Error handling middleware for express applications

License: MIT License

JavaScript 58.88% EJS 41.12%

dfe-digital.login.dfe.express-error-handling's Introduction

login.dfe.express-error-handling

Build Status tested with jest

Error handling middleware for express applications

Usage

Error Handler middleware

The error handler middleware provides a standard express error handler middleware function that will log the error and return a 500 result. It takes an object that can have a logger and errorPageRenderer.

logger should be an object that has an error function on it, with a signature error(message, details); where message is a string nad details is an object.

errorPageRenderer is optional, but if passed, should be a function that can take an error and return an object with properties content and contentType. These will be used as the respective details in the response.

const { getErrorHandler } = require('login.dfe.express-error-handling');
const errorPageRenderer = (error) => {
  render('500', {error});
}

app.use(getErrorHandler(logger, errorPageRenderer))

asyncWrapper

The asyncWrapper can be used to wrap async actions for express, so that errors are handled and passed through the standard express error chain.

const { asyncWrapper } = require('login.dfe.express-error-handling');

app.use('/my-route', asyncWrapper(async (req, res) => {
  await someAction();
  
  res.status(204).send();
}));

EJS Error Page Renderer

The package includes an EJS error page renderer:

const { getErrorHandler, ejsErrorPages } = require('login.dfe.express-error-handling');

const showErrorDetailsOnPage = false; // You can include error details on the page in appropriate environments

// Links to common locations, such as help or static assets
const urls = { 
  help: 'http://url.to/help'
  assets: 'http://cdn.with.assets',
  assetsVersion: 'v1'
}; 
//assetsVersion is a string that will be added when requesting static assets
// http://cdn.with.assets/assetName?version=v1

const errorPageRenderer = ejsErrorPages.getErrorPageRenderer(urls, showErrorDetailsOnPage);

app.use(getErrorHandler({
  logger,
  errorPageRenderer,
}));

dfe-digital.login.dfe.express-error-handling's People

Contributors

jamesmking avatar simonbu11 avatar thedapperdev avatar dashton82 avatar joseluisgraa avatar floydprice avatar jovin-joy avatar darkynt 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.