oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.12k stars 2.77k forks source link

Bun Errors from libraries are not able to extend Error.stack (TypeError: Attempted to assign to readonly property) #3443

Closed vrn-dev closed 1 year ago

vrn-dev commented 1 year ago

What version of Bun is running?

0.6.10

What platform is your computer?

Linux 6.2.6-76060206-generic x86_64 x86_64

What steps can reproduce the bug?

I'm running a Hono server with Kysely ORM with node-postgres driver.

I had a Foreign Key error with the following Kysely function:

const result = await db
    .insertInto('users')
    .values({
        email: body.email,
        firstName: body.firstName,
        lastName: body.lastName,
        extension: body.extension,
        role: Role[body.role],
        password: body.password || '',
        isEnabled: body.isEnabled,
        isRecordingEnabled: body.isRecordingEnabled,
        ldapUserId: body.ldapUserId,
        workgroupId: body.workgroupId,
        routeId: body.routeId,
    }).executeTakeFirst();
return c.json({ id: result.insertId });

With bun I get the following error:

2 | import { isObject, isString } from './object-utils.js';
3 | export function extendStackTrace(err, stackError) {
4 |     if (isStackHolder(err) && stackError.stack) {
5 |         // Remove the first line that just says `Error`.
6 |         const stackExtension = stackError.stack.split('\n').slice(1).join('\n');
7 |         err.stack += `\n${stackExtension}`;
            ^
TypeError: Attempted to assign to readonly property.
      at extendStackTrace (..../node_modules/kysely/dist/esm/util/stack-trace-utils.js:7:8)
      at ..../node_modules/kysely/dist/esm/dialect/postgres/postgres-driver.js:88:18

Which seems to be the Postgres library adding on extra information on the Error stack prop.

The following is the actual Postgres error which I got with Node 18

error: insert or update on table "users" violates foreign key constraint "users_workgroupId_fkey"
at <anonymous> (..../node_modules/.pnpm/pg@8.11.1/node_modules/pg/lib/client.js:526:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at PostgresConnection.executeQuery (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/dialect/postgres/postgres-driver.js:72:28)
at <anonymous> (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28)
at DefaultConnectionProvider.provideConnection (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at DefaultQueryExecutor.executeQuery (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at InsertQueryBuilder.execute (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:534:24)
at InsertQueryBuilder.executeTakeFirst (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:549:26)
at <anonymous> (..../src/routers/usersRouter.ts:104:17)
at <anonymous> (..../node_modules/.pnpm/hono@3.2.6/node_modules/hono/dist/cjs/middleware/cors/index.js:62:7)
at PostgresConnection.executeQuery (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/dialect/postgres/postgres-driver.js:91:69)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at <anonymous> (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:37:28)
at DefaultConnectionProvider.provideConnection (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
at DefaultQueryExecutor.executeQuery (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:16)
at InsertQueryBuilder.execute (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:534:24)
at InsertQueryBuilder.executeTakeFirst (..../node_modules/.pnpm/kysely@0.25.0/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:549:26)
at <anonymous> (..../src/routers/usersRouter.ts:104:17)
at <anonymous> (..../node_modules/.pnpm/hono@3.2.6/node_modules/hono/dist/cjs/middleware/cors/index.js:62:7)
at <anonymous> (..../node_modules/.pnpm/hono@3.2.6/node_modules/hono/dist/cjs/middleware/logger/index.js:63:5) {
    length: 294,
        severity: 'ERROR',
        code: '23503',
        detail: 'Key (workgroupId)=(983713cd-dad6-47ff-8eb8-3347f644782f) is not present in table "workgroups".',
        hint: undefined,
        position: undefined,
        internalPosition: undefined,
        internalQuery: undefined,
        where: undefined,
        schema: 'public',
        table: 'users',
        column: undefined,
        dataType: undefined,
        constraint: 'users_workgroupId_fkey',
        file: 'ri_triggers.c',
        line: '2528',
        routine: 'ri_ReportViolation'
}

Is this something that can be resolved with some method I have not thought about?

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Jarred-Sumner commented 1 year ago

This is a bug, stack shouldn’t be set to read-only