Skip to content

Commit 7ce1614

Browse files
authored
fix(prisma): silence the "Record to delete does not exist" error (#186)
1 parent defe3e9 commit 7ce1614

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test, expect, describe, beforeEach } from 'vitest';
2+
import { session } from 'grammy';
3+
import { PrismaAdapter } from '../src';
4+
import { createBot, createMessage } from '@grammyjs/storage-utils';
5+
import prisma from './helpers/prisma';
6+
7+
beforeEach(async () => {
8+
await prisma.session.deleteMany({});
9+
});
10+
11+
describe('Delete test', () => {
12+
test('A not yet stored record should be nullable without throwing', async () => {
13+
const bot = createBot();
14+
15+
bot.use(
16+
session({
17+
initial() {
18+
return { pizzaCount: 0 };
19+
},
20+
storage: new PrismaAdapter(prisma.session),
21+
})
22+
);
23+
24+
bot.hears('first', (ctx) => {
25+
ctx.session = null;
26+
});
27+
28+
bot.hears('second', (ctx) => {
29+
expect(ctx.session).toHaveProperty('pizzaCount');
30+
});
31+
32+
const firstMessage = createMessage(bot, 'first');
33+
const secondMessage = createMessage(bot, 'second');
34+
35+
await bot.handleUpdate(firstMessage.update);
36+
await bot.handleUpdate(secondMessage.update);
37+
});
38+
});

packages/prisma/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export class PrismaAdapter<T> implements StorageAdapter<T> {
2525
}
2626

2727
async delete(key: string) {
28-
await this.sessionDelegate.delete({ where: { key } });
28+
await this.sessionDelegate.delete({ where: { key } }).catch((err) => {
29+
// Record does not exist in database
30+
if (err?.code === 'P2025') return;
31+
return Promise.reject(err);
32+
});
2933
}
3034
}

0 commit comments

Comments
 (0)