From 1e78c6c96e6d5c6367ba4f7a6ab73d6e9a389cde Mon Sep 17 00:00:00 2001 From: "Cornelius A. Ludmann" Date: Wed, 7 Jul 2021 13:25:42 +0000 Subject: [PATCH] [typeorm] Catch JSON error in SIMPLE_JSON transformer --- components/gitpod-db/src/typeorm/transformer.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/gitpod-db/src/typeorm/transformer.ts b/components/gitpod-db/src/typeorm/transformer.ts index 68a74d3cb98b63..8d1f78e62d72a1 100644 --- a/components/gitpod-db/src/typeorm/transformer.ts +++ b/components/gitpod-db/src/typeorm/transformer.ts @@ -6,6 +6,7 @@ import { ValueTransformer } from "typeorm/decorator/options/ValueTransformer"; import { EncryptionService } from "@gitpod/gitpod-protocol/lib/encryption/encryption-service"; +import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; export namespace Transformer { @@ -45,7 +46,12 @@ export namespace Transformer { return JSON.stringify(value || defaultValue); }, from(value: any): any { - return JSON.parse(value); + try { + return typeof value === 'object' ? value : JSON.parse(value); + } catch (e) { + log.error(`Cannot parse JSON during TypeORM transformation. Returning default value '${JSON.stringify(defaultValue)}' instead. Value: ${JSON.stringify(value)}`, e); + return defaultValue; + } } }; }