简体   繁体   English

如何在 Firebase 云 Function 可调用堆栈跟踪中显示 TypeScript 源?

[英]How to show TypeScript source in Firebase Cloud Function callable stacktraces?

I have a set of Cloud Functions that perform CRUD-like functions to get an individual resource, list resources and so forth, getWidgetByURL , listWidgets , deleteWidget .我有一组云函数,它们执行类似 CRUD 的函数来获取单个资源、列表资源等, getWidgetByURLlistWidgetsdeleteWidget

For a broader context, these are written in a single src/service.ts file and the src/index.ts exposes a set of callables:对于更广泛的上下文,这些都写在单个src/service.ts文件中,并且src/index.ts公开了一组可调用对象:

import * as functions from 'firebase-functions'
import * as service from './service'

const region = 'europe-west1'

exports.addJob = functions.region(region).https.onCall(async (data, context) => {
  try {
    functions.logger.debug('addJob called with data', data)
    const job = await service.addJob(data.title, data.company,
      data.location, data.applyUrl, data.salary, data.tags)
    return job
  } catch (err) {
    functions.logger.error(err)
    throw new functions.https.HttpsError('internal', 'internal server error', err)
  }
})

During the development cycle I run npm run build locally to compile to JavaScript into the target lib directory.在开发周期中,我运行npm run build以编译到 JavaScript 到目标lib目录中。 Note the *.map files are produced.注意*.map文件已生成。

在此处输入图像描述

In production, if a runtime error occurs, the stacktrace shown within Firebase console logs shows only the.js files callstack.在生产中,如果发生运行时错误,Firebase 控制台日志中显示的堆栈跟踪仅显示 .js 文件调用堆栈。

在此处输入图像描述

The debug process involves having to locate the runtime error in my local lib/service.js file and then by manually find the corresponding line in the corresponding source code lib/service.ts file.调试过程涉及必须在我的本地lib/service.js文件中找到运行时错误,然后在相应的源代码lib/service.ts文件中手动找到相应的行。 Tedious.乏味。

Is it possible for stacktraces to automatically make use of the.map files to produce something more useable?堆栈跟踪是否可以自动使用 .map 文件来生成更有用的东西? If not, what is the best practice/workflow?如果不是,最佳实践/工作流程是什么?

You can use the module source-map-support .您可以使用模块source-map-support Just install the module with npm, then put one line of code at the top of your index.js.只需安装 npm 的模块,然后在 index.js 的顶部放一行代码。

require('source-map-support').install();

We use node-source-map-support for this.我们为此使用node-source-map-support I tried this just a few minutes ago and it worked just fine.几分钟前我试过这个,效果很好。

Assuming you're using NPM, run npm install source-map-support .假设您使用的是 NPM,请运行npm install source-map-support (Not --save-dev of course, you'll need it at runtime). (当然不是--save-dev ,你在运行时需要它)。

In index.ts, import source-map-support/register early, preferrably first thing, like so:在 index.ts 中,尽早导入source-map-support/register ,最好是第一件事,如下所示:

// index.ts
import "source-map-support/register";

It doesn't much matter how this compiles.它如何编译并不重要。 As far as I can tell, the call really just needs to happen in the main module there, and it compiles out early enough to make the magic happen.据我所知,调用真的只需要在那里的主模块中发生,并且它编译得足够早以使魔术发生。

Now, to test!现在,来测试! As per this Firebase doc , as long as the logger.error call includes an Error object, we get the stack printout (handy for testing).根据此 Firebase 文档,只要logger.error调用包含Error object,我们就会得到堆栈打印输出(便于测试)。 Logging an error like so, then:像这样记录错误,然后:

// foo.ts
import * as functions from "firebase-functions";
const { logger } = functions;

...

const error = new Error("This should log.");
logger.error(error);

This compiles from line 83 in foo.ts to line 54 in foo.js.这从 foo.ts 中的第83行编译到 foo.js 中的第54行。

And once triggered we get this in the Firebase functions console:一旦触发,我们就会在 Firebase 函数控制台中得到这个:

在此处输入图像描述

As you can see, the filename and line number point correctly to line 83 the .ts source file!如您所见,文件名和行号正确指向.ts源文件的第83行!

Since this has to do with logging, I should note that because we use Firebase Cloud Functions on the Node.js 10 runtime, the old console.log syntax is kinda spoopy.由于这与日志记录有关,我应该注意,因为我们在 Node.js 10 运行时使用 Firebase 云函数,所以旧的console.log语法有点诡异。 So in our tsconfig.json file, we get to have lib: ["es2018"], target: "es2018" , but we also need to use functions.logger.* to get the same behavior as we got with console.* .所以在我们的 tsconfig.json 文件中,我们得到了lib: ["es2018"], target: "es2018" ,但我们还需要使用functions.logger.*来获得与使用console.*相同的行为。 As usual, throwing any old Error object will do as well.像往常一样,抛出任何旧的Error object 也可以。

I found this article handy for the Node.js 10 migration.我发现这篇文章对于 Node.js 10 迁移很方便。 I'll include it here in case that ends up being relevant for you with this.我会将它包含在此处,以防最终与您相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 TypeScript 中的可调用 Firebase Cloud Function 向我的 Unity 应用程序发送自定义 object? - How can I send custom object from my callable Firebase Cloud Function in TypeScript to my Unity app? Flutter firebase 云 function typescript - Flutter firebase cloud function typescript Firebase:如何在打字稿/云功能中创建或访问地图 - Firebase: How to create or access map in typescript / cloud function How to parse json object in firebase cloud function typescript - How to parse json object in firebase cloud function typescript 如何使用打字稿等待for循环firebase云函数 - how to await in for loop firebase cloud function using typescript 在 firebase 云 ZC1C425268E68785D1AB4ZZ114F 中调用 TypeScript function 时出错 - Error calling a TypeScript function in firebase cloud function 在某些情况下,Firebase 可调用云函数在没有 Promise 的情况下返回 void - Firebase Callable Cloud Function is returning void without a Promise in some cases 在 TypeScript 中为 Firebase Cloud Function 配置 mailgun - Configure mailgun in TypeScript for Firebase Cloud Function Firebase UID output 到文档 typescript 云 ZC1C425268E683854F1AB5074C1ZA7 - Firebase UID output to document with typescript cloud function Firebase 云 Function CORS Z558B544CF685F39D34E4903E39C38B6 中的问题 - Firebase Cloud Function CORS issue in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM