<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ### Koa2 異常處理 * [ ] 自定義異常 ![](https://box.kancloud.cn/be5655c81c13da9d8db0885d06cb9165_300x294.png) >[danger] 定義一個全局異常處理類 http-exceptions.js ~~~ // 自定義異常 class HttpExceptions extends Error { constructor(msg='服務器異常', errorCode = 0, code = 400) { super() this.msg = msg this.errorCode = errorCode this.code = code } } // 資源未找到異常 class ResourceErr extends HttpExceptions { constructor(msg='資源未找到', errorCode = 1, code = 404) { super(msg, errorCode, code) } } module.exports = { HttpExceptions, ResourceErr } ~~~ ***** * [ ] 定義一個全局捕獲異常中間件 ![](https://box.kancloud.cn/22de4dcf2ee6532f48ca32865028bdd0_268x265.png) ~~~ const { HttpExceptions } = require('../core/http-exceptions') // 全局異常捕捉中間件 const Exception = async (ctx, next) => { try { await next() } catch(error) { const { msg, errorCode, code } = error const url = `${ctx.method} : ${ctx.request.path}` if (error instanceof HttpExceptions) { // 已知異常 ctx.body = { msg, errorCode, url } ctx.status = code } else { // 未知異常 ctx.body = { msg: '服務器內部錯誤~', errorCode: 999, url } ctx.status = 500 } } } module.exports = Exception ~~~ ***** * [ ] 使用 ~~~ const Router = require('koa-router') const router = new Router() const { ResourceErr } = require('../../../core/http-exceptions') router.get('/api/v1/news/:id', (ctx, next) => { // 自定義異常 throw new ResourceErr() }) module.exports = router ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看