最近我正在研究 Node.js 中全局错误处理中间件的实现。然后,我遇到了这个Error.captureStackTrace(this,this.constructor)。
我检查了 Node 文档并发现 - 在 targetObject 上创建一个 .stack 属性,该属性在访问时返回一个字符串,表示调用 Error.captureStackTrace() 的代码中的位置。
MDN 文档 - 维护正确的堆栈跟踪以查找抛出错误的位置
appError.js 文件
class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
// Error.captureStackTrace(this, this.constructor);
}}
Run Code Online (Sandbox Code Playgroud)
app.js 文件
const AppError = require('./appError');
const express = require('express');
const app = express();
app.all('*', (req,res,next) => {
const custErr = new AppError('Mentioned Route is not available on server','404');
next();
})
Run Code Online (Sandbox Code Playgroud)
当我尝试调试代码时我的观察:
node.js ×1