未加星标

An elegant solution for handling errors in Express

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Express is amicroframework that according to 2018 Node.js User Survey Report is used by 4 in 5 back-end and full-stack node.js developers. Thanks to its simplicity, the always-growing range of available middleware, and active community the express userbase is still growing.

Arguably, the simplicity of Express is its most significant advantage but comes with acost of bare-bones API for handling requests and leaves the rest to the developer. In general, it’s fantastic! We’re developers, and we love to roll out the solutions that we tailor to meet our requirements.

The two everyday tasks for each RESTful application is handling errors and payload validation. Ilike to keep my controllers lean, so for me, middleware is the way to deal with framework-ish features like these two.

Code is available on GitHub: MichalZalecki/express-errors-handling .

Payload Validation

Hapi.js is another framework for building web services in Node.js. Hapi.js extracts input validation out of controllers into the intermediate layer between router and route handlers. Hapi is very modular and its schema validation component is aseparate library called Joi. We are going to use Joi with our Express application through the Celebrate, an Express middleware for Joi.

import express from "express"; import bodyParser from "body-parser"; import { Joi, celebrate, errors } from "celebrate"; const PORT = process.env.PORT || 3000; const app = express(); app.use(bodyParser.json()); type CreateSessionBody = { email: string, password: string }; const createSessionBodySchema = Joi.object({ email: Joi.string().email().required(), password: Joi.string().required(), }).required(); app.post("/session", celebrate({ body: createSessionBodySchema }), (req, res) => { const { email, password } = req.body; const { token } = login(email, password); res.status(201).send({ token }); }); app.use(errors()); app.listen(PORT, () => { console.log(`Server listens on port: ${PORT}`); });

Using Celebrate with Joi is aquick and easy way to validate your schema and respond with awell-formatted error message. It also helps to keep your controllers lean, so you can assume that data from the request payload is well formatted.

That’s how the sample error response may look like:

{ "statusCode": 400, "error": "Bad Request", "message": "child \"email\" fails because [\"email\" is required]", "validation": { "source": "body", "keys": [ "email" ] } } Runtime Errors

Some errors are accidental like database constraint violation, lost connection, third-party service timeout, and some are expected under precisely defined conditions like token expiration, incorrect input data beyond their format (trying to register with the same email twice), etc.

I aim to unify errors handling and make it possible to early return errors to avoid deeply nested code and minimize the effort to handle exceptions. Ilike to wrap my route handlers with enhanceHandler which is ahigher order function that formats the output and sets the correct status based on the returned value from the actual route handler.

If you use TypeScript then start with defining the type for the route handler which allows specifying the type for params and body of the request.

// types.d.ts import express from "express"; import Boom from "boom"; interface Request<TParams, TBody> extends express.Request { params: TParams; body: TBody; } declare global { type RouteHandler<TParams, TBody> = (req: Request<TParams, TBody>, res: express.Response) => | void | express.Response | Boom<any> | Promise<void | express.Response | Boom<any>>; }

Depending on the middleware you use it’s possible to extend Express request and add additional properties.

The essential part of the enhanceHandler is utilizing Boom library. Boom is aset of factory functions for errors that correspond to HTTP errors. Boom, like Joi, is library made to use with Hapi.js but doesn’t depend on it.

// enhanceHandler.ts import express from "express"; import Boom from "boom"; import isNil from "lodash/isNil" function formatBoomPayload(error: Boom<any>) { return { ...error.output.payload, ...(isNil(error.data) ? {} : { data: error.data }), } } export function enhanceHandler<T, P>(handler: RouteHandler<T, P>) { return async (req: express.Request, res: express.Response, next: express.NextFunction) => { try { const result = await handler(req, res); if (result instanceof Error && Boom.isBoom(result)) { res.status(result.output.statusCode).send(formatBoomPayload(result)); } } catch (error) { if (process.env.NODE_ENV !== "production" && (error.stack || error.message)) { res.status(500).send(error.stack || error.message); } else { res.status(500).send(Boom.internal().output.payload); } } next(); }; }

To better understand each case that is handled by enhanceHandler , read the tests on GitHub .

To use enhanceHandler just pass an actual route handler as aparameter and you can now return Boom errors from your controllers.

type CreateSessionBody = { email: string, password: string }; const createSession: RouteHandler<{}, CreateSessionBody> = (req, res) => { const { email, password } = req.body; if (password !== "password") { return Boom.unauthorized("Invalid password"); } res.status(201).send({ email, password }); }; app.post("/session", enhanceHandler(createSession)); Wrap Up

Whether you like this approach or not and consider it elegant as Ido is the matter of preference. The most important is flexibility, so you adjust it to your liking. Hope you at least enjoy this poem:

Express is awesome, but Hapi is too.

Do not add more code out of the blue.

Combine the best tools and code just the glue.

You can buy me a coffee, I like a cold brew.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:An elegant solution for handling errors in Express
本站链接:https://www.codesec.net/view/621249.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(24)