Skip to content

Commit

Permalink
Add Message to DocumentStatus, improve DocumentTranslationException m…
Browse files Browse the repository at this point in the history
…essage
  • Loading branch information
daniel-jones-dev committed Apr 12, 2022
1 parent 7260c71 commit 6b08a57
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]
### Added
* Add `ErrorMessage` property to `DocumentStatus` that contains a short
description of document translation error, if available.
### Changed
### Deprecated
### Removed
### Fixed
### Security


## [1.0.5] - 2022-04-12
### Added
* Add support for `TextTranslateOptions.TagHandling = "html"`. No code changes
Expand Down Expand Up @@ -50,6 +61,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Initial release.


[Unreleased]: https://github.com/DeepLcom/deepl-dotnet/compare/v1.0.5..HEAD
[1.0.5]: https://github.com/DeepLcom/deepl-dotnet/compare/v1.0.4..v1.0.5
[1.0.4]: https://github.com/DeepLcom/deepl-dotnet/compare/v1.0.3..v1.0.4
[1.0.3]: https://github.com/DeepLcom/deepl-dotnet/compare/v1.0.2..v1.0.3
Expand Down
10 changes: 7 additions & 3 deletions DeepL/Model/DocumentStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public enum StatusCode {
/// Number of characters billed for the translation of this document, only included
/// after document translation is finished and the state is done.
/// </param>
/// <param name="errorMessage">Short description of the error, if available.</param>
/// <remarks>
/// The constructor for this class (and all other Model classes) should not be used by library users. Ideally it
/// would be marked <see langword="internal" />, but needs to be <see langword="public" /> for JSON deserialization.
/// In future this function may have backwards-incompatible changes.
/// </remarks>
[JsonConstructor]
public DocumentStatus(string documentId, StatusCode status, int? secondsRemaining, int? billedCharacters) {
(DocumentId, Status, SecondsRemaining, BilledCharacters) =
(documentId, status, secondsRemaining, billedCharacters);
public DocumentStatus(string documentId, StatusCode status, int? secondsRemaining, int? billedCharacters, string? errorMessage) {
(DocumentId, Status, SecondsRemaining, BilledCharacters, ErrorMessage) =
(documentId, status, secondsRemaining, billedCharacters, errorMessage);
}

/// <summary>Document ID of the associated document.</summary>
Expand All @@ -64,6 +65,9 @@ public DocumentStatus(string documentId, StatusCode status, int? secondsRemainin
/// </summary>
public int? BilledCharacters { get; }

/// <summary>Short description of the error, if available.</summary>
public string? ErrorMessage { get; }

/// <summary><c>true</c> if no error has occurred during document translation, otherwise <c>false</c>.</summary>
public bool Ok => Status != StatusCode.Error;

Expand Down
2 changes: 1 addition & 1 deletion DeepL/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public async Task TranslateDocumentWaitUntilDoneAsync(
}

if (!status.Ok) {
throw new DeepLException("Document translation resulted in an error");
throw new DeepLException(status.ErrorMessage ?? "Unknown error");
}
}

Expand Down
21 changes: 12 additions & 9 deletions DeepLTests/TranslateDocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ await translator.TranslateDocumentAsync(
}

[MockServerOnlyFact]
public async Task TestTranslateDocument() {
public async Task TestTranslateLargeDocument() {
var translator = CreateTestTranslator();
using var outputStream = new MemoryStream();
var inputFile = new FileInfo(ExampleLargeDocumentPath());
Expand Down Expand Up @@ -136,19 +136,22 @@ await translator.TranslateDocumentAsync(
}
}

[MockServerOnlyFact]
public async Task TestDocumentFailure() {
var translator = CreateTestTranslatorWithMockSession(
nameof(TestDocumentFailure),
new SessionOptions { DocumentFailure = 1 });
[Fact]
public async Task TestDocumentFailureDuringTranslation() {
var translator = CreateTestTranslator();
var outputDocumentPath = OutputDocumentPath();

await Assert.ThrowsAsync<DocumentTranslationException>(
// Translating text from DE to DE will trigger error
var inputDocumentPath = ExampleDocumentPath(ExampleText(LanguageCode.German));

var exception = await Assert.ThrowsAsync<DocumentTranslationException>(
async () => await translator.TranslateDocumentAsync(
new FileInfo(ExampleDocumentPath()),
new FileInfo(inputDocumentPath),
new FileInfo(outputDocumentPath),
"EN",
null,
"DE"));

Assert.Contains("Source and target language", exception.Message);
}

[Fact]
Expand Down

0 comments on commit 6b08a57

Please sign in to comment.