strongloop loopback自定义错误处理

问题描述:

我目前使用的是loopback 3.2.1我正面临的问题是在访问令牌过期时未记录在日志文件中的未处理错误。做谷歌搜索我碰到这Unhandled error在这里提到,我们可以有自定义错误记录中间件。我遵循了那里提到的指示,并且也参考了文档。不过,我收到以下错误:strongloop loopback自定义错误处理

错误:无法应用.../server/middleware.staging.json:阶段“final:after”中的中间件“./middleware/error-logger”未定义主要配置。

当前middleware.staging.json:

... 
"final": { 
    "loopback#urlNotFound": {} 
    }, 
    "final:after": { 
    "./middleware/error-logger": {}, 
    "strong-error-handler": { 
     "params": { 
     "debug": false, 
     "includeStack": false, 
     "log": false 
     } 
    } 
    } 

服务器/中间件/错误logger.js:

module.exports = function createErrorLogger(options) { 
    return function logError(err, req, res, next) { 
    // your custom error-logging logic goes here 

    const status = err.status || err.statusCode; 
    if (status >= 500) { 
     // log only Internal Server errors 
     console.log('Unhandled error for request %s %s: %s', 
     req.method, req.url, err.stack || err); 
    } 

    // Let the next error handler middleware 
    // produce the HTTP response 
    next(err); 
    }; 
} 

缺少什么我在这里?

最后在查看加载loopback配置文件的代码后发现它。 Loopback正在读取所有的默认配置文件,然后将它们与env特定文件合并。因此,必须在默认文件中进行任何需要进行的配置更改,并在env特定文件中设置值,以使更改按要求运行。例如在我的情况下,我试图将中间件添加到middleware.live.json,但未将其添加到默认的middleware.json文件中。因此,在执行mergePhaseConfig() env特定文件中存在的值,并在默认文件中搜索不存在的值。 在默认文件中添加中间件条目解决了问题。这已经在文件中提到,但今天它跳过了我的脑海。