Localize error messages

This commit is contained in:
Florian Dupret 2021-10-22 13:52:46 +02:00
commit 2165c20f80
23 changed files with 126 additions and 42 deletions

View file

@ -120,11 +120,13 @@ namespace Ombi.Core.Engine
?.FirstOrDefault(x => x.Type == ReleaseDateType.Digital)?.ReleaseDate;
var ruleResults = (await RunRequestRules(requestModel)).ToList();
if (ruleResults.Any(x => !x.Success))
var ruleResultInError = ruleResults.Find(x => !x.Success);
if (ruleResultInError != null)
{
return new RequestEngineResult
{
ErrorMessage = ruleResults.FirstOrDefault(x => x.Message.HasValue()).Message
ErrorMessage = ruleResultInError.Message,
ErrorCode = ruleResultInError.ErrorCode
};
}

View file

@ -1,4 +1,7 @@
namespace Ombi.Core.Engine
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Ombi.Core.Engine
{
public class RequestEngineResult
{
@ -6,6 +9,23 @@
public string Message { get; set; }
public bool IsError => !string.IsNullOrEmpty(ErrorMessage);
public string ErrorMessage { get; set; }
public ErrorCode ErrorCode { get; set; }
public int RequestId { get; set; }
}
[JsonConverter(typeof(StringEnumConverter))]
public enum ErrorCode {
AlreadyRequested,
EpisodesAlreadyRequested,
NoPermissionsOnBehalf,
NoPermissions,
RequestDoesNotExist,
ChildRequestDoesNotExist,
NoPermissionsRequestMovie,
NoPermissionsRequestTV,
NoPermissionsRequestAlbum,
MovieRequestQuotaExceeded,
TvRequestQuotaExceeded,
AlbumRequestQuotaExceeded,
}
}

View file

@ -144,6 +144,7 @@ namespace Ombi.Core.Engine
return new RequestEngineResult
{
Result = false,
ErrorCode = ErrorCode.AlreadyRequested,
ErrorMessage = "This has already been requested"
};
}
@ -166,6 +167,7 @@ namespace Ombi.Core.Engine
return new RequestEngineResult
{
Result = false,
ErrorCode = ErrorCode.NoPermissionsOnBehalf,
Message = "You do not have the correct permissions to request on behalf of users!",
ErrorMessage = $"You do not have the correct permissions to request on behalf of users!"
};
@ -176,6 +178,7 @@ namespace Ombi.Core.Engine
return new RequestEngineResult
{
Result = false,
ErrorCode = ErrorCode.NoPermissions,
Message = "You do not have the correct permissions!",
ErrorMessage = $"You do not have the correct permissions!"
};
@ -250,6 +253,7 @@ namespace Ombi.Core.Engine
return new RequestEngineResult
{
Result = false,
ErrorCode = ErrorCode.AlreadyRequested,
ErrorMessage = "This has already been requested"
};
}
@ -685,6 +689,7 @@ namespace Ombi.Core.Engine
{
return new RequestEngineResult
{
ErrorCode = ErrorCode.ChildRequestDoesNotExist,
ErrorMessage = "Child Request does not exist"
};
}
@ -722,6 +727,7 @@ namespace Ombi.Core.Engine
{
return new RequestEngineResult
{
ErrorCode = ErrorCode.ChildRequestDoesNotExist,
ErrorMessage = "Child Request does not exist"
};
}
@ -781,6 +787,7 @@ namespace Ombi.Core.Engine
{
return new RequestEngineResult
{
ErrorCode = ErrorCode.ChildRequestDoesNotExist,
ErrorMessage = "Child Request does not exist"
};
}
@ -808,6 +815,7 @@ namespace Ombi.Core.Engine
{
return new RequestEngineResult
{
ErrorCode = ErrorCode.ChildRequestDoesNotExist,
ErrorMessage = "Child Request does not exist"
};
}
@ -905,6 +913,7 @@ namespace Ombi.Core.Engine
return new RequestEngineResult
{
Result = false,
ErrorCode = ErrorCode.RequestDoesNotExist,
ErrorMessage = "Request does not exist"
};
}
@ -965,6 +974,7 @@ namespace Ombi.Core.Engine
return new RequestEngineResult
{
Result = false,
ErrorCode = ErrorCode.RequestDoesNotExist,
ErrorMessage = "Request does not exist"
};
}

View file

@ -1,4 +1,5 @@
namespace Ombi.Core.Rule
using Ombi.Core.Engine;
namespace Ombi.Core.Rule
{
public abstract class BaseRequestRule
{
@ -7,9 +8,9 @@
return new RuleResult {Success = true};
}
public RuleResult Fail(string message)
public RuleResult Fail(ErrorCode errorCode, string message = "")
{
return new RuleResult { Message = message };
return new RuleResult { ErrorCode = errorCode, Message = message };
}
}
}

View file

@ -1,8 +1,10 @@
namespace Ombi.Core.Rule
using Ombi.Core.Engine;
namespace Ombi.Core.Rule
{
public class RuleResult
{
public bool Success { get; set; }
public string Message { get; set; }
public ErrorCode ErrorCode { get; set; }
}
}

View file

@ -6,6 +6,7 @@ using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication;
using Ombi.Core.Engine;
using Ombi.Core.Rule.Interfaces;
using Ombi.Helpers;
using Ombi.Store.Entities.Requests;
@ -34,7 +35,7 @@ namespace Ombi.Core.Rule.Rules.Request
{
if (await _manager.IsInRoleAsync(user, OmbiRoles.RequestMovie) || await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMovie))
return Success();
return Fail("You do not have permissions to Request a Movie");
return Fail(ErrorCode.NoPermissionsRequestMovie, "You do not have permissions to Request a Movie");
}
if (obj.RequestType == RequestType.TvShow)
@ -44,7 +45,7 @@ namespace Ombi.Core.Rule.Rules.Request
return Success();
}
return Fail("You do not have permissions to Request a TV Show");
return Fail(ErrorCode.NoPermissionsRequestTV, "You do not have permissions to Request a TV Show");
}
if (obj.RequestType == RequestType.Album)
@ -54,7 +55,7 @@ namespace Ombi.Core.Rule.Rules.Request
return Success();
}
return Fail("You do not have permissions to Request an Album");
return Fail(ErrorCode.NoPermissionsRequestAlbum, "You do not have permissions to Request an Album");
}
throw new InvalidDataException("Permission check failed: unknown RequestType");

View file

@ -6,6 +6,7 @@ using Ombi.Helpers;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
using Ombi.Core.Engine;
using Ombi.Store.Repository.Requests;
namespace Ombi.Core.Rule.Rules.Request
@ -49,7 +50,7 @@ namespace Ombi.Core.Rule.Rules.Request
}
if(found)
{
return Fail($"\"{obj.Title}\" has already been requested");
return Fail(ErrorCode.AlreadyRequested, $"\"{obj.Title}\" has already been requested");
}
}
return Success();

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Engine;
using Ombi.Core.Rule.Interfaces;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
@ -87,7 +88,7 @@ namespace Ombi.Core.Rule.Rules.Request
if (!anyEpisodes)
{
return Fail($"We already have episodes requested from series {child.Title}");
return Fail(ErrorCode.EpisodesAlreadyRequested, $"We already have episodes requested from series {child.Title}");
}
return Success();

View file

@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Rule.Interfaces;
using Ombi.Core.Engine;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository.Requests;
@ -63,7 +64,7 @@ namespace Ombi.Core.Rule.Rules.Request
if (!anyEpisodes)
{
return Fail($"We already have episodes requested from series {tv.Title}");
return Fail(ErrorCode.EpisodesAlreadyRequested, $"We already have episodes requested from series {tv.Title}");
}
}

View file

@ -54,7 +54,7 @@ namespace Ombi.Core.Rule.Rules.Request
if (remainingLimitsModel.Remaining < 1)
{
return Fail("You have exceeded your Movie request quota!");
return Fail(Engine.ErrorCode.MovieRequestQuotaExceeded, "You have exceeded your Movie request quota!");
}
}
if (obj.RequestType == RequestType.TvShow)
@ -75,7 +75,7 @@ namespace Ombi.Core.Rule.Rules.Request
if ((remainingLimitsModel.Remaining - requestCount) < 0)
{
return Fail("You have exceeded your Episode request quota!");
return Fail(Engine.ErrorCode.TvRequestQuotaExceeded, "You have exceeded your Episode request quota!");
}
}
if (obj.RequestType == RequestType.Album)
@ -88,7 +88,7 @@ namespace Ombi.Core.Rule.Rules.Request
if (remainingLimitsModel.Remaining < 1)
{
return Fail("You have exceeded your Album request quota!");
return Fail(Engine.ErrorCode.AlbumRequestQuotaExceeded, "You have exceeded your Album request quota!");
}
}
return Success();

View file

@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Engine;
using Ombi.Core.Models.Search;
using Ombi.Helpers;
using Ombi.Store.Context;
@ -56,7 +57,7 @@ namespace Ombi.Core.Rule.Rules
if (!anyEpisodes)
{
return new RuleResult { Message = $"We already have episodes requested from series {vm.Title}" };
return new RuleResult { ErrorCode = ErrorCode.EpisodesAlreadyRequested, Message = $"We already have episodes requested from series {vm.Title}" };
}
}
}

View file

@ -1,4 +1,5 @@
using Ombi.Core.Rule.Interfaces;
using Ombi.Core.Engine;
using Ombi.Core.Rule.Interfaces;
namespace Ombi.Core.Rule
{
@ -9,9 +10,9 @@ namespace Ombi.Core.Rule
return new RuleResult { Success = true };
}
public RuleResult Fail(string message)
public RuleResult Fail(ErrorCode errorCode, string message = "")
{
return new RuleResult { Message = message };
return new RuleResult { ErrorCode = errorCode, Message = message };
}
public abstract SpecificRules Rule { get; }

View file

@ -132,7 +132,7 @@ export class DiscoverCardComponent implements OnInit {
this.result.requested = true;
this.messageService.send(this.translate.instant("Requests.RequestAddedSuccessfully", { title: this.result.title }), "Ok");
} else {
this.messageService.send(x.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(x);
}
});
}
@ -143,7 +143,7 @@ export class DiscoverCardComponent implements OnInit {
this.result.requested = true;
this.messageService.send(this.translate.instant("Requests.RequestAddedSuccessfully", { title: this.result.title }), "Ok");
} else {
this.messageService.send(x.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(x);
}
this.loading = false;
});

View file

@ -46,7 +46,7 @@ export class DiscoverCollectionsComponent implements OnInit {
if (result.result) {
this.messageService.send(this.translate.instant("Requests.CollectionSuccesfullyAdded", { name: this.collection.name }));
} else {
this.messageService.send(result.errorMessage);
this.messageService.sendRequestEngineResultError(result);
}
this.finishLoading();
});

View file

@ -2,5 +2,21 @@
result: boolean;
message: string;
errorMessage: string;
errorCode: ErrorCode;
requestId: number | undefined;
}
export enum ErrorCode {
AlreadyRequested,
EpisodesAlreadyRequested,
NoPermissionsOnBehalf,
NoPermissions,
RequestDoesNotExist,
ChildRequestDoesNotExist,
NoPermissionsRequestMovie,
NoPermissionsRequestTV,
NoPermissionsRequestAlbum,
MovieRequestQuotaExceeded,
TvRequestQuotaExceeded,
AlbumRequestQuotaExceeded,
}

View file

@ -94,14 +94,14 @@ export class ArtistDetailsComponent {
a.monitored = true;
this.messageService.send(r.message);
} else {
this.messageService.send(r.errorMessage);
this.messageService.sendRequestEngineResultError(r);
}
a.selected = false;
})
.catch(r => {
console.log(r);
this.messageService.send("Error when requesting album");
this.messageService.sendRequestEngineResultError(r);
});
});
} else {
@ -117,14 +117,14 @@ export class ArtistDetailsComponent {
a.monitored = true;
this.messageService.send(r.message);
} else {
this.messageService.send(r.errorMessage);
this.messageService.sendRequestEngineResultError(r);
}
a.selected = false;
})
.catch(r => {
console.log(r);
this.messageService.send("Error when requesting album");
this.messageService.sendRequestEngineResultError(r);
});
})
}

View file

@ -102,7 +102,7 @@ export class MovieDetailsComponent {
this.messageService.send(this.translate.instant("Requests.RequestAddedSuccessfully", { title: this.movie.title }), "Ok");
this.movieRequest = await this.requestService.getMovieRequest(this.movie.requestId);
} else {
this.messageService.send(requestResult.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(requestResult);
}
}
});
@ -114,7 +114,7 @@ export class MovieDetailsComponent {
this.movieRequest = await this.requestService.getMovieRequest(this.movie.requestId);
this.messageService.send(this.translate.instant("Requests.RequestAddedSuccessfully", { title: this.movie.title }), "Ok");
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
}
}
@ -156,7 +156,7 @@ export class MovieDetailsComponent {
this.messageService.send(this.translate.instant("Requests.SuccessfullyApproved"), "Ok");
} else {
this.movie.approved = false;
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
}
@ -166,7 +166,7 @@ export class MovieDetailsComponent {
this.movie.available = true;
this.messageService.send(this.translate.instant("Requests.NowAvailable"), "Ok");
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
}
@ -177,7 +177,7 @@ export class MovieDetailsComponent {
this.movie.available = false;
this.messageService.send(this.translate.instant("Requests.NowUnavailable"), "Ok");
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
}
@ -208,7 +208,7 @@ export class MovieDetailsComponent {
if (result.result) {
this.messageService.send(result.message ? result.message : this.translate.instant("Requests.SuccessfullyReprocessed"), "Ok");
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
});
}

View file

@ -35,7 +35,7 @@ export class DenyDialogComponent {
this.messageService.send(this.translate.instant("Requests.DeniedRequest"), "Ok");
this.data.denied = true;
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
this.data.denied = false;
}

View file

@ -4,6 +4,7 @@ import { RequestService } from "../../../../../services/request.service";
import { MessageService } from "../../../../../services";
import { DenyDialogComponent } from "../../../shared/deny-dialog/deny-dialog.component";
import { ISearchTvResultV2 } from "../../../../../interfaces/ISearchTvResultV2";
import { TranslateService } from "@ngx-translate/core";
import { MatDialog } from "@angular/material/dialog";
import { SelectionModel } from "@angular/cdk/collections";
import { RequestServiceV2 } from "../../../../../services/requestV2.service";
@ -27,7 +28,7 @@ export class TvRequestGridComponent {
public displayedColumns: string[] = ['select', 'number', 'title', 'airDate', 'status'];
constructor(private requestService: RequestService, private requestServiceV2: RequestServiceV2, private notificationService: MessageService,
private dialog: MatDialog) {
private dialog: MatDialog, private translate: TranslateService) {
}
@ -236,7 +237,7 @@ export class TvRequestGridComponent {
private postRequest(requestResult: IRequestEngineResult) {
if (requestResult.result) {
this.notificationService.send(
`Request for ${this.tv.title} has been added successfully`);
this.translate.instant("Requests.RequestAddedSuccessfully", { title:this.tv.title }));
this.selection.clear();
@ -262,7 +263,7 @@ export class TvRequestGridComponent {
}
} else {
this.notificationService.send(requestResult.errorMessage ? requestResult.errorMessage : requestResult.message);
this.notificationService.sendRequestEngineResultError(requestResult);
}
}
}

View file

@ -41,7 +41,7 @@ export class TvRequestsPanelComponent {
});
this.messageService.send("Request has been approved", "Ok");
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
}
@ -104,7 +104,7 @@ export class TvRequestsPanelComponent {
if (result.result) {
this.messageService.send(result.message ? result.message : "Successfully Re-processed the request", "Ok");
} else {
this.messageService.send(result.errorMessage, "Ok");
this.messageService.sendRequestEngineResultError(result);
}
});
}

View file

@ -1,9 +1,11 @@
import { Injectable } from "@angular/core";
import { MatSnackBar, MatSnackBarConfig } from "@angular/material/snack-bar";
import { TranslateService } from "@ngx-translate/core";
import { IRequestEngineResult } from "../interfaces/IRequestEngineResult";
@Injectable()
export class MessageService {
constructor(private snackBar: MatSnackBar) {
constructor(private snackBar: MatSnackBar, private translate: TranslateService) {
this.config = {
duration: 4000,
}
@ -18,4 +20,14 @@ export class MessageService {
this.snackBar.open(message, "OK", this.config)
}
}
public sendRequestEngineResultError(result: IRequestEngineResult, action: string = "Ok") {
console.log(result.errorCode);
const textKey = 'Requests.ErrorCodes.' + result.errorCode;
const text = this.translate.instant(textKey);
if (text !== textKey) {
this.send(text, action);
} else {
this.send(result.errorMessage ? result.errorMessage : result.message, action);
}
}
}

View file

@ -134,7 +134,7 @@ export class EpisodeRequestComponent {
});
} else {
this.notificationService.send(requestResult.errorMessage ? requestResult.errorMessage : requestResult.message);
this.notificationService.sendRequestEngineResultError(requestResult);
}
}
}

View file

@ -204,7 +204,21 @@
"RequestCollection": "Request Collection",
"CollectionSuccesfullyAdded": "The collection {{name}} has been successfully added!",
"NeedToSelectEpisodes": "You need to select some episodes!",
"RequestAddedSuccessfully": "Request for {{title}} has been added successfully"
"RequestAddedSuccessfully": "Request for {{title}} has been added successfully",
"ErrorCodes": {
"AlreadyRequested": "This has already been requested",
"EpisodesAlreadyRequested": "We already have episodes requested from this series",
"NoPermissionsOnBehalf":"You do not have the correct permissions to request on behalf of users!",
"NoPermissions":"You do not have the correct permissions!",
"RequestDoesNotExist":"Request does not exist",
"ChildRequestDoesNotExist":"Child Request does not exist",
"NoPermissionsRequestMovie":"You do not have permissions to Request a Movie",
"NoPermissionsRequestTV":"You do not have permissions to Request a TV Show",
"NoPermissionsRequestAlbum":"You do not have permissions to Request an Album",
"MovieRequestQuotaExceeded":"You have exceeded your Movie request quota!",
"TvRequestQuotaExceeded":"You have exceeded your Episode request quota!",
"AlbumRequestQuotaExceeded":"You have exceeded your Album request quota!"
}
},
"Issues": {
"Title": "Issues",