From 1d903601c3b3349603cde2df1910bbafc7a388a4 Mon Sep 17 00:00:00 2001 From: Gerrit Plehn Date: Thu, 14 Dec 2023 19:14:42 +0100 Subject: [PATCH] don't crash on schema validation fail --- src/crawl.ts | 2 +- src/influx/inverter3P.ts | 8 +++++++- src/influx/inverterCommon.ts | 8 +++++++- src/influx/inverterCumulation.ts | 8 +++++++- src/influx/meter.ts | 8 +++++++- src/influx/powerFlow.ts | 8 +++++++- src/influx/storage.ts | 8 +++++++- 7 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/crawl.ts b/src/crawl.ts index 336cf11..8d7de5d 100644 --- a/src/crawl.ts +++ b/src/crawl.ts @@ -81,7 +81,7 @@ export const crawlFronius = async () => { try { await crawlEndpoint(endpoint); } catch (error) { - console.log(error); + console.error("Error while fetching endpoint", error); } } }; diff --git a/src/influx/inverter3P.ts b/src/influx/inverter3P.ts index cc44e9d..3ceeecf 100644 --- a/src/influx/inverter3P.ts +++ b/src/influx/inverter3P.ts @@ -3,7 +3,13 @@ import { writeApi } from "./index.ts"; import { inverter3PSchema } from "../types/Inverter3P.ts"; export const writeInverter3P = async (rawData: unknown) => { - const data = inverter3PSchema.parse(rawData); + const input = inverter3PSchema.safeParse(rawData); + + if (!input.success) { + console.warn("got unexpected data", input.error); + return; + } + const { data } = input; const inverter3p = data.Body.Data; const measurementTime = data.Head.Timestamp; diff --git a/src/influx/inverterCommon.ts b/src/influx/inverterCommon.ts index 13ab3ef..92d6bcc 100644 --- a/src/influx/inverterCommon.ts +++ b/src/influx/inverterCommon.ts @@ -3,7 +3,13 @@ import { writeApi } from "./index.ts"; import { inverterCommonSchema } from "../types/InverterCommon.ts"; export const writeInverterCommon = async (rawData: unknown) => { - const data = inverterCommonSchema.parse(rawData); + const input = inverterCommonSchema.safeParse(rawData); + + if (!input.success) { + console.warn("got unexpected data", input.error); + return; + } + const { data } = input; const invertercommon = data.Body.Data; const measurementTime = data.Head.Timestamp; diff --git a/src/influx/inverterCumulation.ts b/src/influx/inverterCumulation.ts index 7fec898..1149beb 100644 --- a/src/influx/inverterCumulation.ts +++ b/src/influx/inverterCumulation.ts @@ -3,7 +3,13 @@ import { writeApi } from "./index.ts"; import { inverterCumulationSchema } from "../types/InverterCumulation.ts"; export const writeInverterCumulation = async (rawData: unknown) => { - const data = inverterCumulationSchema.parse(rawData); + const input = inverterCumulationSchema.safeParse(rawData); + + if (!input.success) { + console.warn("got unexpected data", input.error); + return; + } + const { data } = input; const invertercumulation = data.Body.Data; const measurementTime = data.Head.Timestamp; diff --git a/src/influx/meter.ts b/src/influx/meter.ts index 0890943..a19656e 100644 --- a/src/influx/meter.ts +++ b/src/influx/meter.ts @@ -3,7 +3,13 @@ import { writeApi } from "./index.ts"; import { meterSchema } from "../types/Meter.ts"; export const writeMeter = async (rawData: unknown) => { - const data = meterSchema.parse(rawData); + const input = meterSchema.safeParse(rawData); + + if (!input.success) { + console.warn("got unexpected data", input.error); + return; + } + const { data } = input; const meter = data.Body.Data; const measurementTime = data.Head.Timestamp; diff --git a/src/influx/powerFlow.ts b/src/influx/powerFlow.ts index a7d225d..03865de 100644 --- a/src/influx/powerFlow.ts +++ b/src/influx/powerFlow.ts @@ -3,7 +3,13 @@ import { writeApi } from "./index.ts"; import { powerFlowSchema } from "../types/PowerFlow"; export const writePowerFlow = async (rawData: unknown) => { - const data = powerFlowSchema.parse(rawData); + const input = powerFlowSchema.safeParse(rawData); + + if (!input.success) { + console.warn("got unexpected data", input.error); + return; + } + const { data } = input; const inverter = data.Body.Data.Inverters[1]; const site = data.Body.Data.Site; diff --git a/src/influx/storage.ts b/src/influx/storage.ts index 3ef0e9c..da6ec0e 100644 --- a/src/influx/storage.ts +++ b/src/influx/storage.ts @@ -3,7 +3,13 @@ import { writeApi } from "./index.ts"; import { storageSchema } from "../types/Storage.ts"; export const writeStorage = async (rawData: unknown) => { - const data = storageSchema.parse(rawData); + const input = storageSchema.safeParse(rawData); + + if (!input.success) { + console.warn("got unexpected data", input.error); + return; + } + const { data } = input; const storage = data.Body.Data.Controller; if (!storage) {