From 869c8e910a38532e49083acdf145c3bb98842568 Mon Sep 17 00:00:00 2001 From: Lukasz Stafiniak Date: Fri, 9 Aug 2024 12:22:17 +0200 Subject: [PATCH] Fix consistency check when env variable is not set --- CHANGELOG.md | 1 + ppx_minidebug.ml | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eab846f..1e78b11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Fixed - Write the log tree on the error "lexical scope of close_log not matching its dynamic scope", so the entries from the error message can be looked up. +- Uncaught exception in the `[%%global_debug_log_level_from_env_var "..."]` consistency check when the environment variable is not defined. ## [1.6.0] -- 2024-08-08 diff --git a/ppx_minidebug.ml b/ppx_minidebug.ml index 4dea7ba..935c4f1 100644 --- a/ppx_minidebug.ml +++ b/ppx_minidebug.ml @@ -1535,19 +1535,24 @@ let global_log_level_from_env_var ~check_consistency = in A.pstr_eval ~loc [%expr - let runtime_log_level = - Stdlib.String.lowercase_ascii - @@ Stdlib.Sys.getenv - [%e - Ast_helper.Exp.constant ~loc:s_loc - @@ Ast_helper.Const.string ~loc:s_loc env_n] - in - if not (Stdlib.String.equal [%e lifted_log_level] runtime_log_level) - then - failwith - ("ppx_minidebug: compile-time vs. runtime log level mismatch, \ - found '" ^ [%e lifted_log_level] ^ "' at compile time, '" - ^ runtime_log_level ^ "' at runtime")] + try + let runtime_log_level = + Stdlib.String.lowercase_ascii + @@ Stdlib.Sys.getenv + [%e + Ast_helper.Exp.constant ~loc:s_loc + @@ Ast_helper.Const.string ~loc:s_loc env_n] + in + if + (not (Stdlib.String.equal "" runtime_log_level)) + && not + (Stdlib.String.equal [%e lifted_log_level] runtime_log_level) + then + failwith + ("ppx_minidebug: compile-time vs. runtime log level mismatch, \ + found '" ^ [%e lifted_log_level] ^ "' at compile time, '" + ^ runtime_log_level ^ "' at runtime") + with Stdlib.Not_found -> ()] attrs else noop in