Skip to content

Commit fa5c065

Browse files
committed
refactor: improve error handling for encryption and decryption in EncryptedHandler
1 parent 4e5a2be commit fa5c065

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/runtime/src/enhancements/node/encrypted.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ class EncryptedHandler extends DefaultPrismaProxyHandler {
144144
// Don't decrypt null, undefined or empty string values
145145
if (!entityData[field]) continue;
146146

147-
entityData[field] = await this.decrypt(fieldInfo, entityData[field]);
147+
try {
148+
entityData[field] = await this.decrypt(fieldInfo, entityData[field]);
149+
} catch (error) {
150+
console.warn('Decryption failed, keeping original value:', error);
151+
}
148152
}
149153
}
150154
}
@@ -157,7 +161,11 @@ class EncryptedHandler extends DefaultPrismaProxyHandler {
157161

158162
const encAttr = field.attributes?.find((attr) => attr.name === '@encrypted');
159163
if (encAttr && field.type === 'String') {
160-
context.parent[field.name] = await this.encrypt(field, data);
164+
try {
165+
context.parent[field.name] = await this.encrypt(field, data);
166+
} catch (error) {
167+
throw new Error(`Encryption failed for field ${field.name}: ${error}`);
168+
}
161169
}
162170
},
163171
});

tests/integration/tests/enhancements/with-encrypted/with-encrypted.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ describe('Encrypted test', () => {
2323
}`);
2424

2525
const sudoDb = enhance(undefined, { kinds: [] });
26+
const encryptionKey = new Uint8Array(Buffer.from('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=', 'base64'));
27+
2628
const db = enhance(undefined, {
2729
kinds: ['encrypted'],
28-
encryption: { encryptionKey: 'c558Gq0YQK2QcqtkMF9BGXHCQn4dMF8w' },
30+
encryption: { encryptionKey },
2931
});
3032

3133
const create = await db.user.create({

0 commit comments

Comments
 (0)