Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to catch and return errors in koa

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to koa error capture and return, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Example:

Const Koa = require ('koa2'); const router = require (' koa-router') (); const app = new Koa (); / * * error capture middleware * / app.use (async (ctx, next) = > {try {ctx.error = (code, message) = > {if (typeof code = 'string') {message = code; code = 500;} ctx.throw (code | 500, message | |' server error') }; await next ();} catch (e) {let status = e.status | | 500; let message = e.message | | 'server error'; ctx.response.body = {status, message};}}); app.use (require ('koa-bodyparser') ()); app.listen (3000); console.log (' start at port 3000..')

This code starts koa and has a middleware that specifically handles koa errors. In the middleware, I add an error method to the ctx object to receive the error number and error content, or not to write the error number, default 500. when the ctx.error method is executed, it will throw an exception, so that in other routing or middleware, when the code executes to ctx.error, it will jump back to my error capture middleware directly, and the code behind ctx.error will not be executed again.

The method is also very easy to use.

Router.get ('/', async (ctx) = > {if (! ctx.request.query.project) {ctx.error ('Project not examples'); / / ctx.error (12345) It is also possible to make it clear that when the error code is 12345, the project field is not found}. / / logical processing. Ctx.response.body = res;})

Is to call the ctx.error method directly. When this method is called, the logical processing that follows the if is no longer performed.

Thank you for reading this article carefully. I hope the article "how to catch and return koa errors" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report