<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ### egg resfulAPI 全局異常處理 ### 統一錯誤處理 * [ ] 文檔:[https://eggjs.org/zh-cn/tutorials/restful.html](https://eggjs.org/zh-cn/tutorials/restful.html) ***** 1. 自定義一個異常基類 app / exceptions / http_exceptions.js ``` class HttpExceptions extends Error { constructor(msg='服務器異常', code=1, httpCode=400) { super() this.code = code; this.msg = msg; this.httpCode = httpCode; } } module.exports = { HttpExceptions }; ``` ***** 2. 定義全局異常處理中間件 app / middleware / error_handler.js ``` const { HttpExceptions } = require('../exceptions/http_exceptions'); module.exports = () => { return async function errorHandler(ctx, next) { try { await next(); } catch (err) { // 所有的異常都在 app 上觸發一個 error 事件,框架會記錄一條錯誤日志 ctx.app.emit('error', err, ctx); let status = err.status || 500; let error = {}; if (err instanceof HttpExceptions) { status = err.httpCode error.requestUrl = `${ctx.method} : ${ctx.path}`; error.msg = err.msg; error.code = err.code; error.httpCode = err.httpCode; } else { // 未知異常,系統異常,線上不顯示堆棧信息 // 生產環境時 500 錯誤的詳細錯誤內容不返回給客戶端,因為可能包含敏感信息 error.errsInfo = status === 500 && ctx.app.config.env === 'prod' ? 'Internal Server Error' : err.message; } // 從 error 對象上讀出各個屬性,設置到響應中 ctx.body = error; if (status === 422) { ctx.body.detail = err.errors; } ctx.status = status; } }; }; ``` *****
                  <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>

                              哎呀哎呀视频在线观看