欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用node怎么實(shí)現(xiàn)錯誤處理-創(chuàng)新互聯(lián)

本篇文章為大家展示了使用node怎么實(shí)現(xiàn)錯誤處理,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新新互聯(lián),憑借十多年的網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計(jì)網(wǎng)站有上千家案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)。

node項(xiàng)目中的錯誤處理

node中Error對象的使用

使用captureStackTrace方法加入自帶的錯誤信息

// Error對象自帶的屬性
Error.captureStackTrace

// 如何使用captureStackTrace
var obj = {
  message: 'something is wrong'
}

Error.captureStackTrace(obj)

throw obj  // 此時會拋出obj對象的message內(nèi)信息

使用try catch捕獲錯誤

直接把代碼寫在try catch中即可捕獲錯誤信息

try{
  throw new Error('oh no')
}catch(e){
  console.log(e)
}

在異步代碼中,直接try catch是無法捕獲錯誤信息的,可以使用如下方法

function foo(params, cb){
  const error = new Error('something is wrong')
  if(error) cb(error)
}

以上使用callback方式來做錯誤處理比較容易麻煩,容易出錯,現(xiàn)在node已經(jīng)支持async await所以盡量使用它們準(zhǔn)沒錯

async function foo(){
  try{
    await bar()
  }catch(e){
    console.log(e)
  }
}

async function bar(){
  throw new Error('async function got wrong)
}

foo()

基本錯誤類型

在項(xiàng)目會有多個地方對錯誤信息進(jìn)行處理,所以先寫一個基本錯誤類型,方便使用

// 基本錯誤類型
class HttpBaseError extends Error {
 constructor(httpStatusCode, httpMsg, errCode, msg) {
  super(`HTTP ERROR: ${msg}`);
  this.httpStatusCode = httpStatusCode;
  this.httpMsg = httpMsg;
  this.errCode = errCode;
 }
}

try {
// 直接拋出定義好的錯誤即可
 throw new HttpBaseError(404, '資源不存在', 10000, 'resouse is not found');
} catch (e) {
 console.log(e.message);
 console.log(e.httpStatusCode);
 console.log(e.httpMsg);
 console.log(e.errCode);
}

特定錯誤類型

除了基本類型,不同情況下會有不同錯誤信息,需要用一個特定的錯誤類型來處理特定的錯誤信息

// 一個參數(shù)錯誤類型
const ERROR_CODE = 40000  // 錯誤碼
class HttpRequestParamError extends HttpBaseError {
  constructor(paramName, desc, msg) {
    super(200, desc, ERROR_CODE, `${paramName} wrong: ${msg}`)
  }
}

這樣,在參數(shù)錯誤的地方就能非常方便的調(diào)用這個錯誤類型來返回錯誤

拋錯的邏輯

錯誤處理中,model,controller中的錯誤,有些是不能直接返回給用戶的,應(yīng)該只返回給model或controller的調(diào)用者。

使用錯誤處理

正常接口,controller,model的錯誤,使用設(shè)定好的錯誤類型進(jìn)行處理,例如前面寫的HttpRequestParamError,在所有所有路由的最后,需要使用一個error handler來對所有的錯誤進(jìn)行集中處理

// error handler
function handler(options) {
  return function (err, req, res, next) {
    if (err instanceof HttpRequestParamError) {  // 這里對不同的錯誤做不同的處理
      console.log('http request error')
      res.statusCode = err.httpStatusCode
      res.json({
        code: err.errCode,
        msg: err.httpMsg
      })
    } else {
      // 設(shè)定之外的錯誤,把管理權(quán)向外移交
      next(err)
    }
  }
}

除了可預(yù)知的錯誤,還有未知的類型的錯誤,此時需要一個unknow error handler進(jìn)行剩余錯誤的處理

function unKnowErrorHandler(options) {
  return function (err, req, res, next) {
    console.log(err)
    res.json({
      code: 99999,
      msg: 'unKnow error'
    })
  }
}

node中的日志

平時使用console來debug是沒有問題的,但是在線上環(huán)境,我們并不能有效的看到console,使用日志系統(tǒng)可以更好的方便線上的debug,記錄信息等

winston的使用

winston是node中常用的日志插件

const winston = require('winston')

const logger = winston.createLogger({
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({
      name: 'info_logger',  // log名稱
      filename: 'logs/info.log',  // 日志記錄文件地址
      level: 'info' // 設(shè)置log的類型
    }),
    // 第二個logger,記錄error級別的log
    new winston.transports.File({
      name: 'error_logger',
      filename: 'logs/error.log',
      level: 'error'
    })
  ]
});

// error級別比info要高,error.log文件只會記錄error日志
logger.error('first error log with winston')
// info文件內(nèi)會記錄info級別的log和比info級別高的log,比如error
logger.info('first info log with winston')

日志滾動(log rotation)

在產(chǎn)生大量數(shù)據(jù)的應(yīng)用當(dāng)中,日志的輸出是大量的,這是就需要對日志進(jìn)行拆分處理,例如按照每天的頻率來分別記錄日志。

winston并不自帶log rotation,需要引入winston-daily-rotate-file庫

const {
  createLogger,
  format,
  transports
} = require('winston');
const {
  combine,
  timestamp,
  label,
  prettyPrint
} = format;
require('winston-daily-rotate-file')


var transport = new(transports.DailyRotateFile)({
  filename: './logs/app-%DATE%.log',
  datePattern: 'YYYY-MM-DD-HH',
  maxSize: '20m',
  maxFiles: '14d',
  format: combine(
    label({
      label: 'right meow!'
    }),
    timestamp(),
    prettyPrint()
  ),
});
transport.on('rotate', function (oldFilename, newFilename) {});

var logger = createLogger({
  transports: [
    transport
  ]
});

logger.info('Hello World!');

上述內(nèi)容就是使用node怎么實(shí)現(xiàn)錯誤處理,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文名稱:使用node怎么實(shí)現(xiàn)錯誤處理-創(chuàng)新互聯(lián)
分享鏈接:http://chinadenli.net/article22/idscc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)微信小程序、搜索引擎優(yōu)化云服務(wù)器ChatGPT、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營