K
Kysely12mo ago
ohmi

Figuring out where in codebase an exception originated from

Hi all, This might be a stupid question, but what is the best way to figure out which .execute() call threw a SQL exception? I know ideally I should be doing error handling on each call, but I have a large codebase and I'm not sure where this error is coming from. Is there any easy way of figuring this out? Maybe a way to have a wrapper that prints out the query if malformed, or to print out the stack trace where the original call was executed?
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1. Trace: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
at Packet.asError (/app/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/app/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/app/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/app/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/app/node_modules/mysql2/lib/connection.js:102:25)
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1. Trace: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
at Packet.asError (/app/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/app/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/app/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/app/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/app/node_modules/mysql2/lib/connection.js:102:25)
14 Replies
Igal
Igal12mo ago
Hey 👋 We support logging queries, errors and custom, so spotting the query log that's closest to the runtime exception is a way. You could try and parse the stack trace at iirc Error.stack and look for the .execute (or a variant of it).
koskimas
koskimas12mo ago
Hmm.. Didn't we have a workaround for showing the whole stack trace in errors? Yep, we use extendStackTrace. It's failing in this case
ohmi
ohmi12mo ago
extendStackTrace should in theory be including the stack trace of where the .execute was called?
koskimas
koskimas12mo ago
Yep But it's not for some reason
ohmi
ohmi12mo ago
heres the full stack trace, truncated it a bit earlier
2023-06-21T04:20:13.395Z [ERROR] - unhandledRejection | Cluster Unhandled Rejection | Name: Error. Reason: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1. Trace: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
at Packet.asError (/app/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/app/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/app/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/app/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/app/node_modules/mysql2/lib/connection.js:102:25)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at MysqlConnection.executeQuery (/app/node_modules/kysely/dist/cjs/dialect/mysql/mysql-driver.js:115:69)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /app/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28
at async DefaultConnectionProvider.provideConnection (/app/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at async DefaultQueryExecutor.executeQuery (/app/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at async SelectQueryBuilder.execute (/app/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1202:24)
at async SelectQueryBuilder.executeTakeFirst (/app/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1210:26)
2023-06-21T04:20:13.395Z [ERROR] - unhandledRejection | Cluster Unhandled Rejection | Name: Error. Reason: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1. Trace: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
at Packet.asError (/app/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/app/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/app/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/app/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/app/node_modules/mysql2/lib/connection.js:102:25)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at MysqlConnection.executeQuery (/app/node_modules/kysely/dist/cjs/dialect/mysql/mysql-driver.js:115:69)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /app/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28
at async DefaultConnectionProvider.provideConnection (/app/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at async DefaultQueryExecutor.executeQuery (/app/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at async SelectQueryBuilder.execute (/app/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1202:24)
at async SelectQueryBuilder.executeTakeFirst (/app/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1210:26)
koskimas
koskimas12mo ago
Oh crap. We've created a new stack-cutter in executeTakeFirst Wot, I don't see how it would happen here
async executeTakeFirst(): Promise<SimplifySingleResult<O>> {
const [result] = await this.execute()
return result as SimplifySingleResult<O>
}
async executeTakeFirst(): Promise<SimplifySingleResult<O>> {
const [result] = await this.execute()
return result as SimplifySingleResult<O>
}
are you perhaps compiling into ES5 or something and your compiled code doesn't use async/await? Async stack traces only work with async/await. If your build phase transpiles async/await into something else, you lose the ability to have full stack traces.
ohmi
ohmi12mo ago
i should be compiling to es6.. let me try to get a minimal repro some time tomorrow
koskimas
koskimas12mo ago
Could just be a case of missing await in some query. Or that query is a part of Promise.all (not sure if stack traces are handled in case of Promise.all)
ohmi
ohmi12mo ago
oh, its possible it might be in a Promise.all? but yeah ill try to dig deeper a bit later todayu
import { Kysely, MysqlDialect } from "kysely";
import { createPool } from "mysql2";
import { KmqDB } from "./typings/kmq_db";

const connection = new Kysely<KmqDB>({
dialect: new MysqlDialect({
pool: createPool({
host: "127.0.0.1",
user: "",
password: "",
database: "",
connectionLimit: 10,
charset: "utf8mb4",
port: 3306,
decimalNumbers: true,
multipleStatements: true,
}),
}),
});

(async () => {
console.log(
await connection
.selectFrom("available_songs")
.where("artist_aliases", "in", [])
.execute()
);
})();
import { Kysely, MysqlDialect } from "kysely";
import { createPool } from "mysql2";
import { KmqDB } from "./typings/kmq_db";

const connection = new Kysely<KmqDB>({
dialect: new MysqlDialect({
pool: createPool({
host: "127.0.0.1",
user: "",
password: "",
database: "",
connectionLimit: 10,
charset: "utf8mb4",
port: 3306,
decimalNumbers: true,
multipleStatements: true,
}),
}),
});

(async () => {
console.log(
await connection
.selectFrom("available_songs")
.where("artist_aliases", "in", [])
.execute()
);
})();
brian@personal-vm:~/KMQ_Discord$ ts-node src/test.ts

/home/brian/KMQ_Discord/node_modules/mysql2/lib/packets/packet.js:728
const err = new Error(message);
^
Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `available_songs` where `artist_aliases` in ()' at line 1
at Packet.asError (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/home/brian/KMQ_Discord/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:102:25)
at Socket.emit (node:events:520:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at MysqlConnection.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/dialect/mysql/mysql-driver.js:115:69)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at /home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28
at DefaultConnectionProvider.provideConnection (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at DefaultQueryExecutor.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at SelectQueryBuilder.execute (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1202:24)
brian@personal-vm:~/KMQ_Discord$ ts-node src/test.ts

/home/brian/KMQ_Discord/node_modules/mysql2/lib/packets/packet.js:728
const err = new Error(message);
^
Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `available_songs` where `artist_aliases` in ()' at line 1
at Packet.asError (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/home/brian/KMQ_Discord/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:102:25)
at Socket.emit (node:events:520:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at MysqlConnection.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/dialect/mysql/mysql-driver.js:115:69)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at /home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28
at DefaultConnectionProvider.provideConnection (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at DefaultQueryExecutor.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at SelectQueryBuilder.execute (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1202:24)
{
"compilerOptions": {
"incremental": true,
"outDir": "./build",
"allowJs": true,
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": ".",
"rootDir": "src",
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictPropertyInitialization": true,
"lib": ["es2022"],
"resolveJsonModule": true,
"esModuleInterop": true,
"sourceMap": true,
"skipLibCheck": true
},
"include": [
"./src/**/*"
]
}
{
"compilerOptions": {
"incremental": true,
"outDir": "./build",
"allowJs": true,
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": ".",
"rootDir": "src",
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictPropertyInitialization": true,
"lib": ["es2022"],
"resolveJsonModule": true,
"esModuleInterop": true,
"sourceMap": true,
"skipLibCheck": true
},
"include": [
"./src/**/*"
]
}
above was with ts-node, but same when transpiling
brian@personal-vm:~/KMQ_Discord$ npx tsc
brian@personal-vm:~/KMQ_Discord$ node build/test.js
node:internal/process/promises:265
triggerUncaughtException(err, true /* fromPromise */);
^

Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `available_songs` where `artist_aliases` in ()' at line 1
at Packet.asError (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/home/brian/KMQ_Discord/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:102:25)
at Socket.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at MysqlConnection.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/dialect/mysql/mysql-driver.js:115:69)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28
at async DefaultConnectionProvider.provideConnection (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at async DefaultQueryExecutor.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at async SelectQueryBuilder.execute (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1202:24)
at async SelectQueryBuilder.executeTakeFirst (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1210:26) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `available_songs` where `artist_aliases` in ()' at line 1",
sql: 'select from `available_songs` where `artist_aliases` in ()'
}
brian@personal-vm:~/KMQ_Discord$ npx tsc
brian@personal-vm:~/KMQ_Discord$ node build/test.js
node:internal/process/promises:265
triggerUncaughtException(err, true /* fromPromise */);
^

Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `available_songs` where `artist_aliases` in ()' at line 1
at Packet.asError (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packets/packet.js:728:17)
at Query.execute (/home/brian/KMQ_Discord/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:490:32)
at PacketParser.onPacket (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:95:12)
at PacketParser.executeStart (/home/brian/KMQ_Discord/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/brian/KMQ_Discord/node_modules/mysql2/lib/connection.js:102:25)
at Socket.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at MysqlConnection.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/dialect/mysql/mysql-driver.js:115:69)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28
at async DefaultConnectionProvider.provideConnection (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at async DefaultQueryExecutor.executeQuery (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at async SelectQueryBuilder.execute (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1202:24)
at async SelectQueryBuilder.executeTakeFirst (/home/brian/KMQ_Discord/node_modules/kysely/dist/cjs/query-builder/select-query-builder.js:1210:26) {
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `available_songs` where `artist_aliases` in ()' at line 1",
sql: 'select from `available_songs` where `artist_aliases` in ()'
}
koskimas
koskimas12mo ago
Did you check async/await is used in the compiled code?
ohmi
ohmi12mo ago
(() => __awaiter(void 0, void 0, void 0, function* () {
console.log(yield connection
.selectFrom("available_songs")
.where("artist_aliases", "in", [])
.executeTakeFirst());
}))();
(() => __awaiter(void 0, void 0, void 0, function* () {
console.log(yield connection
.selectFrom("available_songs")
.where("artist_aliases", "in", [])
.executeTakeFirst());
}))();
i assume thats a no?
koskimas
koskimas12mo ago
That's a no The target probably need to be much newer
ohmi
ohmi12mo ago
yep that did it changed target to es2022, thanks! the earliest target that compiles to async/await appears to be es2017
koskimas
koskimas12mo ago
the earliest target that compiles to async/await appears to be es2017
That's good to know! I'm guessing a looooot of people have a lower target than that and are suffering from missing stack traces.