TypeORM stack trace not showing origin of error in my code

Viewed 967

In a project I'm working on, once in a while I'll get some random TypeORM error that can be tedious to troubleshoot, because the stack trace from TypeORM doesn't show the origin of error in my code. It will only show the stacktrace from the TypeORM NPM package.

Any recommendation on how to solve this?

Below is an example of such a problem that recently occurred

(node:5096) UnhandledPromiseRejectionWarning: QueryFailedError: Error: Invalid usage of the option NEXT in the FETCH statement.
    at new QueryFailedError (/home/myuser/repos/somefolder/my-project/api/src/error/QueryFailedError.ts:9:9)
    at /home/myuser/repos/somefolder/my-project/api/src/driver/sqlserver/SqlServerQueryRunner.ts:223:37
    at /home/myuser/repos/somefolder/my-project/api/node_modules/mssql/lib/base.js:1293:25
    at Request.userCallback (/home/myuser/repos/somefolder/my-project/api/node_modules/mssql/lib/tedious.js:661:15)
    at Request._this.callback (/home/myuser/repos/somefolder/my-project/api/node_modules/tedious/lib/request.js:61:27)
    at Connection.endOfMessageMarkerReceived (/home/myuser/repos/somefolder/my-project/api/node_modules/tedious/lib/connection.js:1944:20)
    at Connection.dispatchEvent (/home/myuser/repos/somefolder/my-project/api/node_modules/tedious/lib/connection.js:1012:38)
    at Parser.<anonymous> (/home/myuser/repos/somefolder/my-project/api/node_modules/tedious/lib/connection.js:812:18)
    at Parser.emit (events.js:310:20)
    at Parser.EventEmitter.emit (domain.js:482:12)
    at Parser.<anonymous> (/home/myuser/repos/somefolder/my-project/api/node_modules/tedious/lib/token/token-stream-parser.js:54:15)
    at Parser.emit (events.js:310:20)
    at Parser.EventEmitter.emit (domain.js:482:12)
    at addChunk (/home/myuser/repos/somefolder/my-project/api/node_modules/readable-stream/lib/_stream_readable.js:291:12)
    at readableAddChunk (/home/myuser/repos/somefolder/my-project/api/node_modules/readable-stream/lib/_stream_readable.js:278:11)
    at Parser.Readable.push (/home/myuser/repos/somefolder/my-project/api/node_modules/readable-stream/lib/_stream_readable.js:245:10)
2 Answers

You can enable logging of all queries and errors by simply setting logging: true in your connection options.

See this for more information.

Moreover you also can set different types of logging in connection options.

Enabling this functionality will show every SQL command executed by typeorm in your terminal.

Since I don't know what queries you are doing the only reasonable solution I can think about seems to log every query you do (as Carlo Corradini suggested) and when you get an error see list of last queries logged and try to guess what caused problem. Then try to add some error handling code in the place, which you think causes an error, and dig deeper.

Also for debugging purpouses you can try catching unhandled promise rejections by setting up a listener on nodejs process and then try to get more info from error object.

process.on('unhandledRejection', error => {
  // Prints "unhandledRejection woops!"
  console.log('unhandledRejection', error.test);
});

new Promise((_, reject) => reject({ test: 'woops!' }));

Idk tho maybe it's not applicable to your particular situation. Hope that may help.

Related