Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text analytics error code and warning code #11889

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DocumentSentiment DocumentSentiment
if (HasError)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.Code}: {Error.Message}");
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.ErrorCode}: {Error.Message}");
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
return _documentSentiment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public DetectedLanguage PrimaryLanguage
if (HasError)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.Code}: {Error.Message}");
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.ErrorCode}: {Error.Message}");
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
return _primaryLanguage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public KeyPhraseCollection KeyPhrases
if (HasError)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.Code}: {Error.Message}");
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.ErrorCode}: {Error.Message}");
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
return _keyPhrases;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CategorizedEntityCollection Entities
if (HasError)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.Code}: {Error.Message}");
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.ErrorCode}: {Error.Message}");
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
return _entities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public LinkedEntityCollection Entities
if (HasError)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.Code}: {Error.Message}");
throw new InvalidOperationException($"Cannot access result for document {Id}, due to error {Error.ErrorCode}: {Error.Message}");
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
return _linkedEntities;
Expand Down
20 changes: 10 additions & 10 deletions sdk/textanalytics/Azure.AI.TextAnalytics/src/TextAnalyticsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public virtual async Task<Response<DetectedLanguage>> DetectLanguageAsync(string
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = result[0].Error;
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.Code, CreateAdditionalInformation(error)).ConfigureAwait(false);
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error)).ConfigureAwait(false);
}
return Response.FromValue(result[0].PrimaryLanguage, response);
default:
Expand Down Expand Up @@ -210,7 +210,7 @@ public virtual Response<DetectedLanguage> DetectLanguage(string document, string
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = result[0].Error;
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.Code, CreateAdditionalInformation(error));
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
}
return Response.FromValue(result[0].PrimaryLanguage, response);
default:
Expand Down Expand Up @@ -439,7 +439,7 @@ public virtual async Task<Response<CategorizedEntityCollection>> RecognizeEntiti
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.Code, CreateAdditionalInformation(error)).ConfigureAwait(false);
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error)).ConfigureAwait(false);
}
return Response.FromValue((CategorizedEntityCollection)results[0].Entities, response);
default:
Expand Down Expand Up @@ -500,7 +500,7 @@ public virtual Response<CategorizedEntityCollection> RecognizeEntities(string do
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.Code, CreateAdditionalInformation(error));
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
}
return Response.FromValue((CategorizedEntityCollection)results[0].Entities, response);
default:
Expand Down Expand Up @@ -736,7 +736,7 @@ public virtual async Task<Response<DocumentSentiment>> AnalyzeSentimentAsync(str
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.Code, CreateAdditionalInformation(error)).ConfigureAwait(false);
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error)).ConfigureAwait(false);
}
return Response.FromValue(results[0].DocumentSentiment, response);
default:
Expand Down Expand Up @@ -794,7 +794,7 @@ public virtual Response<DocumentSentiment> AnalyzeSentiment(string document, str
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.Code, CreateAdditionalInformation(error));
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
}
return Response.FromValue(results[0].DocumentSentiment, response);
default:
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public virtual async Task<Response<KeyPhraseCollection>> ExtractKeyPhrasesAsync(
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.Code, CreateAdditionalInformation(error)).ConfigureAwait(false);
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error)).ConfigureAwait(false);
}
return Response.FromValue((KeyPhraseCollection) results[0].KeyPhrases, response);
default:
Expand Down Expand Up @@ -1080,7 +1080,7 @@ public virtual Response<KeyPhraseCollection> ExtractKeyPhrases(string document,
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.Code, CreateAdditionalInformation(error));
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
}
return Response.FromValue((KeyPhraseCollection)results[0].KeyPhrases, response);
default:
Expand Down Expand Up @@ -1313,7 +1313,7 @@ public virtual async Task<Response<LinkedEntityCollection>> RecognizeLinkedEntit
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.Code, CreateAdditionalInformation(error)).ConfigureAwait(false);
throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error)).ConfigureAwait(false);
}
return Response.FromValue((LinkedEntityCollection)results[0].Entities, response);
default:
Expand Down Expand Up @@ -1372,7 +1372,7 @@ public virtual Response<LinkedEntityCollection> RecognizeLinkedEntities(string d
{
// only one document, so we can ignore the id and grab the first error message.
TextAnalyticsError error = results[0].Error;
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.Code, CreateAdditionalInformation(error));
throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
}
return Response.FromValue((LinkedEntityCollection)results[0].Entities, response);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public struct TextAnalyticsError
{
internal TextAnalyticsError(string code, string message, string target = null)
{
Code = code;
ErrorCode = code;
Message = message;
Target = target;
}

/// <summary>
/// Error code that serves as an indicator of the HTTP error code.
/// </summary>
public string Code { get; }
public TextAnalyticsErrorCode ErrorCode { get; }

/// <summary>
/// Message that contains more information about the reason of the error.
Expand Down
146 changes: 146 additions & 0 deletions sdk/textanalytics/Azure.AI.TextAnalytics/src/TextAnalyticsErrorCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using Azure.Core;

namespace Azure.AI.TextAnalytics
{
/// <summary>
/// Text Analytics error code values.
/// </summary>
public readonly struct TextAnalyticsErrorCode : IEquatable<TextAnalyticsErrorCode>
{
private readonly string _value;

/// <summary>
/// Specifies that the error code is an invalid request.
/// </summary>
public static readonly string InvalidRequest = "invalidRequest";

/// <summary>
/// Specifies that the error code is an invalid argument.
/// </summary>
public static readonly string InvalidArgument = "invalidArgument";

/// <summary>
/// Specifies that the error code is an internal server error.
/// </summary>
public static readonly string InternalServerError = "internalServerError";

/// <summary>
/// Specifies that the error code is service unavailable.
/// </summary>
public static readonly string ServiceUnavailable = "serviceUnavailable";

/// <summary>
/// Specifies that the error code is an invalid parameter value.
/// </summary>
public static readonly string InvalidParameterValue = "invalidParameterValue";

/// <summary>
/// Specifies that the error code is an invalid request body format.
/// </summary>
public static readonly string InvalidRequestBodyFormat = "invalidRequestBodyFormat";

/// <summary>
/// Specifies that the error code is an empty request.
/// </summary>
public static readonly string EmptyRequest = "emptyRequest";

/// <summary>
/// Specifies that the error code is a missing input records.
/// </summary>
public static readonly string MissingInputRecords = "missingInputRecords";

/// <summary>
/// Specifies that the error code is an invalid document.
/// </summary>
public static readonly string InvalidDocument = "invalidDocument";

/// <summary>
/// Specifies that the error code is model version incorrect.
/// </summary>
public static readonly string ModelVersionIncorrect = "modelVersionIncorrect";

/// <summary>
/// Specifies that the error code is an invalid document batch.
/// </summary>
public static readonly string InvalidDocumentBatch = "invalidDocumentBatch";

/// <summary>
/// Specifies that the error code is an unsupported language code.
/// </summary>
public static readonly string UnsupportedLanguageCode = "unsupportedLanguageCode";

/// <summary>
/// Specifies that the error code is an invalid country hint.
/// </summary>
public static readonly string InvalidCountryHint = "invalidCountryHint";

private TextAnalyticsErrorCode(string errorCode)
{
Argument.AssertNotNullOrEmpty(errorCode, nameof(errorCode));
_value = errorCode;
}

/// <summary>
/// Defines implicit conversion from string to TextAnalyticsErrorCode.
/// </summary>
/// <param name="errorCode">string to convert.</param>
/// <returns>The string as an TextAnalyticsErrorCode.</returns>
public static implicit operator TextAnalyticsErrorCode(string errorCode) => new TextAnalyticsErrorCode(errorCode);

/// <summary>
/// Defines explicit conversion from TextAnalyticsErrorCode to string.
/// </summary>
/// <param name="errorCode">TextAnalyticsErrorCode to convert.</param>
/// <returns>The TextAnalyticsErrorCode as a string.</returns>
public static explicit operator string(TextAnalyticsErrorCode errorCode) => errorCode._value;

/// <summary>
/// Compares two TextAnalyticsErrorCode values for equality.
/// </summary>
/// <param name="left">The first TextAnalyticsErrorCode to compare.</param>
/// <param name="right">The second TextAnalyticsErrorCode to compare.</param>
/// <returns>true if the TextAnalyticsErrorCode objects are equal or are both null; false otherwise.</returns>
public static bool operator ==(TextAnalyticsErrorCode left, TextAnalyticsErrorCode right) => Equals(left, right);

/// <summary>
/// Compares two TextAnalyticsErrorCode values for inequality.
/// </summary>
/// <param name="left">The first TextAnalyticsErrorCode to compare.</param>
/// <param name="right">The second TextAnalyticsErrorCode to compare.</param>
/// <returns>true if the TextAnalyticsErrorCode objects are not equal; false otherwise.</returns>
public static bool operator !=(TextAnalyticsErrorCode left, TextAnalyticsErrorCode right) => !Equals(left, right);

/// <summary>
/// Compares the TextAnalyticsErrorCode for equality with another TextAnalyticsErrorCode.
/// </summary>
/// <param name="other">The TextAnalyticsErrorCode with which to compare.</param>
/// <returns><c>true</c> if the TextAnalyticsErrorCode objects are equal; otherwise, <c>false</c>.</returns>
public bool Equals(TextAnalyticsErrorCode other) => _value == other._value;

/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns><c>true</c> if the specified object is equal to the current object; otherwise, <c>false</c>.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => obj is TextAnalyticsErrorCode errorCode && Equals(errorCode);

/// <summary>
/// Serves as the default hash function.
/// </summary>
/// <returns>A hash code for the current object.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => _value.GetHashCode();

/// <summary>
/// Returns a string representation of the TextAnalyticsErrorCode.
/// </summary>
/// <returns>The TextAnalyticsErrorCode as a string.</returns>
public override string ToString() => _value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static TextDocumentBatchStatistics TextDocumentBatchStatistics(int docume
/// <summary>
/// Initializes a new instance of <see cref="TextAnalytics.TextAnalyticsError"/> for mocking purposes.
/// </summary>
/// <param name="code">Sets the <see cref="TextAnalyticsError.Code"/> property.</param>
/// <param name="code">Sets the <see cref="TextAnalyticsError.ErrorCode"/> property.</param>
/// <param name="message">Sets the <see cref="TextAnalyticsError.Message"/> property.</param>
/// <param name="target">Sets the <see cref="TextAnalyticsError.Target"/> property.</param>
/// <returns>A new instance of <see cref="TextAnalytics.TextAnalyticsError"/> for mocking purposes.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ internal TextAnalyticsResult(string id, TextAnalyticsError error)
/// <summary>
/// Indicates that the document was not successfully processed and an error was returned for this document.
/// </summary>
public bool HasError => Error.Code != default;
public bool HasError => Error.ErrorCode != default;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static TextAnalyticsError ReadTextAnalyticsError(JsonElement element)
}

// Return the innermost error, which should be only one level down.
return innerError.Code == default ? new TextAnalyticsError(errorCode, message, target) : innerError;
return innerError.ErrorCode == default ? new TextAnalyticsError(errorCode, message, target) : innerError;
}

private static List<TextAnalyticsWarning> ReadDocumentWarnings(JsonElement documentElement)
Expand Down
Loading