public static JsonErrorInfo ValidateJson(string text, JSON.SyntaxValidator.CommentInfos comments, bool relaxMode)
        {
            var r = new JsonErrorInfo();

            try
            {
                var o = new JSON.SyntaxValidator.Compiler().Validate(text, supportStartComment: true, relaxMode: relaxMode, commentInfos: comments);
            }
            catch (JSON.SyntaxValidator.ParserException ex)
            {
                r.ex           = ex;
                r.LineNumber   = ex.Line;
                r.LinePosition = ex.Col;
                r.Message      = ex.Message;
                r.AbsPosition  = ex.AbsolutePosition;
                System.Diagnostics.Debug.WriteLine("PARSING_ERR:" + r.ToString());
            }
            catch (System.Exception ex)
            {
                r.ex           = ex;
                r.LineNumber   = -1;
                r.LinePosition = -1;
                r.Message      = ex.Message;
                r.AbsPosition  = -1;
                System.Diagnostics.Debug.WriteLine("PARSING_ERR:" + r.ToString());
            }
            return(r);
        }
Beispiel #2
0
        public static string AutoSaveFileParseMessage(JsonErrorInfo jsonErrorInfo)
        {
            string paramDisplayString = StringUtilities.ToDefaultParameterListDisplayString(
                jsonErrorInfo.Parameters.Select(x => JsonErrorInfoParameterDisplayHelper.GetLocalizedDisplayValue(x, Localizer.Default)));

            return($"{jsonErrorInfo.ErrorCode}{paramDisplayString} at position {jsonErrorInfo.Start}, length {jsonErrorInfo.Length}");
        }
        public void UnchangedParametersInError(JsonErrorCode errorCode, JsonErrorLevel errorLevel, int start, int length, string[] parameters)
        {
            JsonErrorInfoParameter[] errorInfoParameters = parameters?.Select(x => new JsonErrorInfoParameter <string>(x))?.ToArrayEx();

            var errorInfo = new JsonErrorInfo(errorCode, errorLevel, start, length, errorInfoParameters);

            Assert.Equal(errorCode, errorInfo.ErrorCode);
            Assert.Equal(errorLevel, errorInfo.ErrorLevel);
            Assert.Equal(start, errorInfo.Start);
            Assert.Equal(length, errorInfo.Length);

            AssertErrorInfoParameters(errorInfo, errorInfoParameters);
        }
 internal static void AssertErrorInfoParameters(JsonErrorInfo actualErrorInfo, params JsonErrorInfoParameter[] expectedParameters)
 {
     if (expectedParameters == null || !expectedParameters.Any())
     {
         Assert.Empty(actualErrorInfo.Parameters);
     }
     else
     {
         Assert.Collection(actualErrorInfo.Parameters, expectedParameters.Select(expected => new Action <JsonErrorInfoParameter>(actual =>
         {
             Assert.IsType(expected.GetType(), actual);
             Assert.Equal(expected.UntypedValue, actual.UntypedValue);
         })).ToArrayEx());
     }
 }
        /// <summary>
        /// Gets the formatted and localized error message of a <see cref="JsonErrorInfo"/>.
        /// </summary>
        public static string Message(this JsonErrorInfo jsonErrorInfo, Localizer localizer)
        {
            const string UnspecifiedMessage = "Unspecified error";

            if (jsonErrorInfo is PTypeError typeError)
            {
                return(typeError.GetLocalizedMessage(localizer));
            }

            if (Enum.IsDefined(typeof(JsonErrorCode), jsonErrorInfo.ErrorCode))
            {
                return(localizer.Localize(
                           GetLocalizedStringKey(jsonErrorInfo.ErrorCode),
                           jsonErrorInfo.Parameters.Select(
                               x => JsonErrorInfoParameterDisplayHelper.GetLocalizedDisplayValue(x, localizer)).ToArrayEx()));
            }

            return(UnspecifiedMessage);
        }
Beispiel #6
0
 public SettingsParseException(JsonErrorInfo jsonErrorInfo)
     : base(AutoSaveFileParseMessage(jsonErrorInfo))
 {
 }