Skip to content

Commit 1098eee

Browse files
committed
add logger warning for null/undefined inputs in hashSha256
1 parent 5708ef0 commit 1098eee

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/roktManager.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,21 @@ export default class RoktManager {
294294
* Hashes an attribute using SHA-256
295295
*
296296
* @param {string | number | boolean | undefined | null} attribute - The value to hash
297-
* @returns {Promise<string>} The SHA-256 hex digest of the normalized value
297+
* @returns {Promise<string | undefined | null>} SHA-256 hashed value or undefined/null
298298
*
299299
*/
300-
public async hashSha256(attribute: string | number | boolean | undefined | null): Promise<string> {
301-
if (attribute === undefined || attribute === null) {
302-
throw new Error('Value cannot be null or undefined');
300+
public async hashSha256(attribute: string | number | boolean | undefined | null): Promise<string | undefined | null> {
301+
if (attribute === null || attribute === undefined) {
302+
this.logger.warning(`hashSha256 received ${attribute} as input`);
303+
return attribute as null | undefined;
303304
}
304305

305306
try {
306307
const normalizedValue = String(attribute).trim().toLocaleLowerCase();
307308
return await this.sha256Hex(normalizedValue);
308309
} catch (error) {
309310
const errorMessage = error instanceof Error ? error.message : String(error);
310-
this.logger.error('Failed hashSha256: ' + errorMessage);
311+
this.logger.error('Failed to hash attribute: ' + errorMessage);
311312
throw error;
312313
}
313314
}

test/jest/roktManager.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,25 @@ describe('RoktManager', () => {
255255
expect(emptyStringHash).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
256256
});
257257

258-
it('should reject when value is null', async () => {
259-
await expect(roktManager.hashSha256(null)).rejects.toThrow('Value cannot be null or undefined');
258+
it('should return null and log warning when value is null', async () => {
259+
const result = await roktManager.hashSha256(null);
260+
261+
expect(result).toBeNull();
262+
expect(mockMPInstance.Logger.warning).toHaveBeenCalledWith('hashSha256 received null as input');
260263
});
261264

262-
it('should reject when value is undefined', async () => {
263-
await expect(roktManager.hashSha256(undefined)).rejects.toThrow('Value cannot be null or undefined');
265+
it('should return undefined and log warning when value is undefined', async () => {
266+
const result = await roktManager.hashSha256(undefined);
267+
268+
expect(result).toBeUndefined();
269+
expect(mockMPInstance.Logger.warning).toHaveBeenCalledWith('hashSha256 received undefined as input');
264270
});
265271

266272
it('should log error when hashing fails', async () => {
267273
shaSpy.mockRejectedValue(new Error('Hash failed'));
268274

269275
await expect(roktManager.hashSha256('[email protected]')).rejects.toThrow();
270-
expect(mockMPInstance.Logger.error).toHaveBeenCalledWith(expect.stringContaining('Failed hashSha256'));
276+
expect(mockMPInstance.Logger.error).toHaveBeenCalledWith(expect.stringContaining('Failed to hash attribute'));
271277
});
272278

273279
it('should hash firstName to known SHA-256 value', async () => {

0 commit comments

Comments
 (0)