diff --git a/src/executor.ts b/src/executor.ts index 54d9386..7787c46 100644 --- a/src/executor.ts +++ b/src/executor.ts @@ -331,6 +331,10 @@ export class IdempotentExecutor { type: 'error', error: errorSerializer.serialize(value), }); + } else if (value === undefined) { + await this.cache.set(cacheKey, { + type: 'value', + }); } else { await this.cache.set(cacheKey, { type: 'value', diff --git a/src/serialization.spec.ts b/src/serialization.spec.ts index a878604..fca5f58 100644 --- a/src/serialization.spec.ts +++ b/src/serialization.spec.ts @@ -53,6 +53,12 @@ describe('JSONSerializer', () => { ), ); }); + + it('should throw an error when serialization result is undefined', () => { + expect(() => serializer.serialize(undefined)).toThrow( + new SerializerError('Not JSON serializable'), + ); + }); }); describe('DefaultErrorSerializer', () => { diff --git a/src/serialization.ts b/src/serialization.ts index 1353fed..e5f0f4e 100644 --- a/src/serialization.ts +++ b/src/serialization.ts @@ -64,8 +64,15 @@ export class JSONSerializer implements Serializer { serialize(value: T): string { try { // Assumes the value is already in a JSON serializable format. - return JSON.stringify(value); + const result = JSON.stringify(value); + if (typeof result !== 'string') { + throw new SerializerError('Not JSON serializable'); + } + return result; } catch (err) { + if (err instanceof SerializerError) { + throw err; + } throw new SerializerError('Not JSON serializable', err); } }