From 138e7c06d66c730414b82e9ff8479430bec9d22d Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Mon, 5 Dec 2022 22:59:16 +0700 Subject: [PATCH] add BUILD_METADATA fallback when parsing (#2503) ### Changes This PR adds a fallback to empty build metadata when BUILD_METADATA contains invalid JSON. Example `warning` log for `BUILD_METADATA={...}`: ``` 20:57:57.872 [warning] failed to parse $BUILD_METADATA, reason: ** (Jason.DecodeError) unexpected byte at position 1: 0x2E (".") ``` Fixes https://github.com/plausible/analytics/issues/2491 ### Tests - [x] This PR does not require tests ### Changelog - [ ] Entry has been added to changelog ### Documentation - [x] This change does not need a documentation update ### Dark mode - [x] This PR does not change the UI --- config/runtime.exs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/config/runtime.exs b/config/runtime.exs index 579c53258..cfb89a941 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -1,5 +1,6 @@ import Config import Plausible.ConfigHelpers +require Logger if config_env() in [:dev, :test] do Envy.load(["config/.env.#{config_env()}"]) @@ -92,10 +93,26 @@ ch_db_url = ### Mandatory params End +build_metadata_raw = get_var_from_path_or_env(config_dir, "BUILD_METADATA", "{}") + build_metadata = - config_dir - |> get_var_from_path_or_env("BUILD_METADATA", "{}") - |> Jason.decode!() + case Jason.decode(build_metadata_raw) do + {:ok, build_metadata} -> + build_metadata + + {:error, error} -> + error = Exception.format(:error, error) + + Logger.warn(""" + failed to parse $BUILD_METADATA: #{error} + + $BUILD_METADATA is set to #{build_metadata_raw}\ + """) + + Logger.warn("falling back to empty build metadata, as if $BUILD_METADATA was set to {}") + + _fallback = %{} + end runtime_metadata = [ version: get_in(build_metadata, ["labels", "org.opencontainers.image.version"]),