mirror of
https://github.com/fluxerapp/fluxer.git
synced 2026-11-26 20:26:24 -08:00
21 lines
641 B
TypeScript
21 lines
641 B
TypeScript
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import type {CacheLogger} from '@pkgs/cache/src/CacheProviderTypes';
|
|
|
|
export function safeJsonParse<T>(value: string, logger?: CacheLogger): T | null {
|
|
try {
|
|
return JSON.parse(value);
|
|
} catch (error) {
|
|
if (logger) {
|
|
const truncatedValue = value.length > 200 ? `${value.substring(0, 200)}...` : value;
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
logger.error({errorMessage, value: truncatedValue}, '[CacheProvider] JSON parse error');
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function serializeValue<T>(value: T): string {
|
|
return JSON.stringify(value);
|
|
}
|