Skip to content

Commit

Permalink
Localize runtime error messages (#2602)
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-joyle authored Aug 30, 2024
1 parent 077ede8 commit 923f501
Show file tree
Hide file tree
Showing 39 changed files with 1,202 additions and 420 deletions.
230 changes: 0 additions & 230 deletions src/libraries/Microsoft.PowerFx.Core/Functions/CommonErrors.cs

This file was deleted.

53 changes: 45 additions & 8 deletions src/libraries/Microsoft.PowerFx.Core/Public/ExpressionError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public string Message
{
if (_message == null && this.MessageKey != null)
{
(var shortMessage, var _) = ErrorUtils.GetLocalizedErrorContent(new ErrorResourceKey(this.MessageKey, this.ResourceManager), _messageLocale, out _);

var msg = ErrorUtils.FormatMessage(shortMessage, _messageLocale, _messageArgs);

_message = msg;
_message = GetFormattedMessage(_messageLocale);
}

return _message;
Expand Down Expand Up @@ -111,19 +107,60 @@ public ExpressionError GetInLocale(CultureInfo culture)
return this;
}

public override string ToString()
private string GetFormattedMessage(CultureInfo locale)
{
if (this.ResourceManager != null)
{
(var shortMessage, var _) = ErrorUtils.GetLocalizedErrorContent(new ErrorResourceKey(this.MessageKey, this.ResourceManager), locale, out _);
return ErrorUtils.FormatMessage(shortMessage, _messageLocale, _messageArgs);
}
else
{
return _message;
}
}

/// <summary>
/// Get error message in the given locale.
/// </summary>
/// <param name="culture">CultureInfo object.</param>
/// <param name="includeSpanDetails">If true, get error message with span details.</param>
/// <returns></returns>
public string GetMessageInLocale(CultureInfo culture, bool includeSpanDetails = false)
{
if (includeSpanDetails)
{
return IncludeSpanDetails(GetFormattedMessage(culture));
}
else
{
return GetFormattedMessage(culture);
}
}

/// <summary>
/// Format error message with span details.
/// </summary>
/// <param name="message">Message to get formatted.</param>
/// <returns></returns>
private string IncludeSpanDetails(string message)
{
var prefix = IsWarning ? "Warning" : "Error";
if (Span != null)
{
return $"{prefix} {Span.Min}-{Span.Lim}: {Message}";
return $"{prefix} {Span.Min}-{Span.Lim}: {message}";
}
else
{
return $"{prefix}: {Message}";
return $"{prefix}: {message}";
}
}

public override string ToString()
{
return IncludeSpanDetails(Message);
}

// Build the public object from an internal error object.
internal static ExpressionError New(IDocumentError error)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Functions;

namespace Microsoft.PowerFx.Types
{
Expand Down Expand Up @@ -220,7 +219,8 @@ public override async Task<DValue<BooleanValue>> RemoveAsync(IEnumerable<Formula

if (!found)
{
errors.Add(CommonErrors.RecordNotFound());
// https://github.com/microsoft/Power-Fx/issues/2618
errors.Add(new ExpressionError() { Message = "The specified record was not found.", Kind = ErrorKind.NotFound });
}
}

Expand Down Expand Up @@ -250,7 +250,8 @@ protected override async Task<DValue<RecordValue>> PatchCoreAsync(RecordValue ba
}
else
{
return DValue<RecordValue>.Of(FormulaValue.NewError(CommonErrors.RecordNotFound()));
// https://github.com/microsoft/Power-Fx/issues/2618
return DValue<RecordValue>.Of(FormulaValue.NewError(new ExpressionError() { Message = "The specified record was not found.", Kind = ErrorKind.NotFound }));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.Types;
using Microsoft.PowerFx.Core.Utils;
using static Microsoft.PowerFx.Syntax.PrettyPrintVisitor;

namespace Microsoft.PowerFx.Types
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private static bool TryConvertToDouble(object value, out double result)

internal static ErrorValue OverflowError(IRContext irContext)
{
// https://github.com/microsoft/Power-Fx/issues/2618
return new ErrorValue(irContext, new ExpressionError()
{
Message = "Overflow",
Expand Down
13 changes: 10 additions & 3 deletions src/libraries/Microsoft.PowerFx.Core/Public/Values/TableValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.Localization;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Functions;

namespace Microsoft.PowerFx.Types
{
Expand Down Expand Up @@ -253,8 +252,16 @@ protected virtual async Task<DValue<RecordValue>> PatchCoreAsync(RecordValue bas
/// </summary>
/// <returns></returns>
protected virtual async Task<DValue<RecordValue>> PatchSingleRecordCoreAsync(RecordValue recordValue, CancellationToken cancellationToken)
{
return DValue<RecordValue>.Of(CommonErrors.NotYetImplementedError(IRContext, "Patch single record is invalid for tables/records with no primary key."));
{
// https://github.com/microsoft/Power-Fx/issues/2618
return DValue<RecordValue>.Of(new ErrorValue(
IRContext,
new ExpressionError()
{
Message = $"Not implemented: Patch single record is invalid for tables/records with no primary key.",
Span = IRContext.SourceContext,
Kind = ErrorKind.NotSupported
}));
}

/// <summary>
Expand Down
Loading

0 comments on commit 923f501

Please sign in to comment.