forked from kysely-org/kysely
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: query and error logging both occur on error. (kysely-org#796)
* deny logging query and error onerror. also deny catching custom log err. * move caughtError declaration so it doesn't take up "execution time". * fix defaultLogger error handling when not Error. * test logging.
- Loading branch information
1 parent
5f5b8b8
commit ed112af
Showing
4 changed files
with
200 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import { SinonSandbox, SinonSpy, createSandbox } from 'sinon' | ||
import { Database, expect } from './test-setup' | ||
import { | ||
DatabaseConnection, | ||
Driver, | ||
DummyDriver, | ||
Kysely, | ||
LogConfig, | ||
PostgresDialect, | ||
} from '../../..' | ||
|
||
describe('logging', () => { | ||
let sandbox: SinonSandbox | ||
let errorSpy: SinonSpy | ||
let logSpy: SinonSpy | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox() | ||
errorSpy = sandbox.stub(console, 'error') | ||
logSpy = sandbox.stub(console, 'log') | ||
}) | ||
|
||
afterEach(() => { | ||
sandbox.restore() | ||
}) | ||
|
||
describe('when query execution succeeds', () => { | ||
describe('when query logging is disabled', () => { | ||
describe('when error logging is disabled', () => { | ||
const db = getKysely([]) | ||
|
||
it('should not log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.false | ||
}) | ||
|
||
it('should not log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.false | ||
}) | ||
}) | ||
|
||
describe('when error logging is enabled', () => { | ||
const db = getKysely(['error']) | ||
|
||
it('should not log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.false | ||
}) | ||
|
||
it('should not log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.false | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when query logging is enabled', () => { | ||
describe('when error logging is disabled', () => { | ||
const db = getKysely(['query']) | ||
|
||
it('should log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.true | ||
}) | ||
|
||
it('should not log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.false | ||
}) | ||
}) | ||
|
||
describe('when error logging is enabled', () => { | ||
const db = getKysely(['query', 'error']) | ||
|
||
it('should log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.true | ||
}) | ||
|
||
it('should not log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.false | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when query execution fails', () => { | ||
const executeQuery = () => Promise.reject('oops') | ||
|
||
describe('when query logging is disabled', () => { | ||
describe('when error logging is disabled', () => { | ||
const db = getKysely([], executeQuery) | ||
|
||
it('should not log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.false | ||
}) | ||
|
||
it('should not log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.false | ||
}) | ||
}) | ||
|
||
describe('when error logging is enabled', () => { | ||
const db = getKysely(['error'], executeQuery) | ||
|
||
it('should not log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.false | ||
}) | ||
|
||
it('should log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.true | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when query logging is enabled', () => { | ||
describe('when error logging is disabled', () => { | ||
const db = getKysely(['query'], executeQuery) | ||
|
||
it('should not log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.false | ||
}) | ||
|
||
it('should not log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.false | ||
}) | ||
}) | ||
|
||
describe('when error logging is enabled', () => { | ||
const db = getKysely(['query', 'error'], executeQuery) | ||
|
||
it('should not log query', async () => { | ||
await run(db) | ||
expect(logSpy.called).to.be.false | ||
}) | ||
|
||
it('should log error', async () => { | ||
await run(db) | ||
expect(errorSpy.called).to.be.true | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function getKysely( | ||
log: LogConfig, | ||
executeQuery: DatabaseConnection['executeQuery'] = () => | ||
Promise.resolve({ rows: [] }) | ||
): Kysely<Database> { | ||
return new Kysely({ | ||
dialect: new (class extends PostgresDialect { | ||
constructor() { | ||
super({ pool: {} as any }) | ||
} | ||
createDriver(): Driver { | ||
return new (class extends DummyDriver { | ||
acquireConnection(): Promise<DatabaseConnection> { | ||
return Promise.resolve({ | ||
executeQuery, | ||
streamQuery: (async () => {}) as any, | ||
}) | ||
} | ||
})() | ||
} | ||
})(), | ||
log, | ||
}) | ||
} | ||
|
||
async function run(db: Kysely<Database>) { | ||
try { | ||
await db.selectFrom('person').selectAll().execute() | ||
} catch (err) {} | ||
} |