From d8d0eba519386685a25d69057fedc8a9165c9d86 Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Mon, 25 Nov 2019 14:53:17 -0800 Subject: [PATCH] Fix #1950: Nonsensical error message for type errors in launch.json Use fmt() instead of format() for validation error text. --- src/ptvsd/common/json.py | 12 ++++++++++-- src/ptvsd/common/messaging.py | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ptvsd/common/json.py b/src/ptvsd/common/json.py index 2613610b..e4b81186 100644 --- a/src/ptvsd/common/json.py +++ b/src/ptvsd/common/json.py @@ -225,7 +225,7 @@ def array(validate_item=False, vectorize=False, size=None): try: value[i] = validate_item(item) except (TypeError, ValueError) as exc: - raise type(exc)("[{0!j}] {1}".format(i, exc)) + raise type(exc)(fmt("[{0!j}] {1}", i, exc)) return value return validate @@ -259,7 +259,15 @@ def object(validate_value=False): try: value[k] = validate_value(v) except (TypeError, ValueError) as exc: - raise type(exc)("[{0!j}] {1}".format(k, exc)) + raise type(exc)(fmt("[{0!j}] {1}", k, exc)) return value return validate + + +# A helper to resolve the circular dependency between common.fmt and common.json +# on Python 2. +def fmt(*args, **kwargs): + from ptvsd.common import fmt + + return fmt(*args, **kwargs) diff --git a/src/ptvsd/common/messaging.py b/src/ptvsd/common/messaging.py index 2a5bea99..e4647ae5 100644 --- a/src/ptvsd/common/messaging.py +++ b/src/ptvsd/common/messaging.py @@ -413,7 +413,10 @@ class MessageDict(collections.OrderedDict): value = validate(value) except (TypeError, ValueError) as exc: message = Message if self.message is None else self.message - raise message.isnt_valid("{0!r} {1}", key, exc) + err = fmt("{0}", exc) + if not err.startswith("["): + err = " " + err + raise message.isnt_valid("{0!j}{1}", key, err) return value def _invalid_if_no_key(func):