[英]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 的函数来获取单个资源、列表资源等,
getWidgetByURL
、 listWidgets
、 deleteWidget
。
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.