error handling #1749

pull/1653/merge
Jamie 7 years ago
parent ebb3e916b6
commit f944ef6a79

@ -1,149 +1,146 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Hangfire; using Hangfire;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Ombi.Core.Notifications; using Ombi.Core.Notifications;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Notifications.Models; using Ombi.Notifications.Models;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Repository; using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests; using Ombi.Store.Repository.Requests;
namespace Ombi.Schedule.Jobs.Plex namespace Ombi.Schedule.Jobs.Plex
{ {
public class PlexAvailabilityChecker : IPlexAvailabilityChecker public class PlexAvailabilityChecker : IPlexAvailabilityChecker
{ {
public PlexAvailabilityChecker(IPlexContentRepository repo, ITvRequestRepository tvRequest, IMovieRequestRepository movies, public PlexAvailabilityChecker(IPlexContentRepository repo, ITvRequestRepository tvRequest, IMovieRequestRepository movies,
INotificationService notification, IBackgroundJobClient background) INotificationService notification, IBackgroundJobClient background)
{ {
_tvRepo = tvRequest; _tvRepo = tvRequest;
_repo = repo; _repo = repo;
_movieRepo = movies; _movieRepo = movies;
_notificationService = notification; _notificationService = notification;
_backgroundJobClient = background; _backgroundJobClient = background;
} }
private readonly ITvRequestRepository _tvRepo; private readonly ITvRequestRepository _tvRepo;
private readonly IMovieRequestRepository _movieRepo; private readonly IMovieRequestRepository _movieRepo;
private readonly IPlexContentRepository _repo; private readonly IPlexContentRepository _repo;
private readonly INotificationService _notificationService; private readonly INotificationService _notificationService;
private readonly IBackgroundJobClient _backgroundJobClient; private readonly IBackgroundJobClient _backgroundJobClient;
public async Task Start() public async Task Start()
{ {
await ProcessMovies(); await ProcessMovies();
await ProcessTv(); await ProcessTv();
} }
private async Task ProcessTv() private async Task ProcessTv()
{ {
var tv = _tvRepo.GetChild().Where(x => !x.Available); var tv = _tvRepo.GetChild().Where(x => !x.Available);
var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series); var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);
foreach (var child in tv) foreach (var child in tv)
{ { var useImdb = false;
var useTvDb = false;
PlexServerContent item = null; if (child.ParentRequest.ImdbId.HasValue())
var useImdb = false; {
var useTvDb = false; useImdb = true;
if (child.ParentRequest.ImdbId.HasValue()) }
{
useImdb = true; if (child.ParentRequest.TvDbId.ToString().HasValue())
} {
useTvDb = true;
if (child.ParentRequest.TvDbId.ToString().HasValue()) }
{
useTvDb = true; var tvDbId = child.ParentRequest.TvDbId;
} var imdbId = child.ParentRequest.ImdbId;
IQueryable<PlexEpisode> seriesEpisodes = null;
var tvDbId = child.ParentRequest.TvDbId; if (useImdb)
var imdbId = child.ParentRequest.ImdbId; {
IQueryable<PlexEpisode> seriesEpisodes = null; seriesEpisodes = plexEpisodes.Where(x => x.Series.ImdbId == imdbId.ToString());
if (useImdb) }
{ if (useTvDb)
seriesEpisodes = plexEpisodes.Where(x => x.Series.ImdbId == imdbId.ToString()); {
} seriesEpisodes = plexEpisodes.Where(x => x.Series.TvDbId == tvDbId.ToString());
if (useTvDb) }
{ foreach (var season in child.SeasonRequests)
seriesEpisodes = plexEpisodes.Where(x => x.Series.TvDbId == tvDbId.ToString()); {
} foreach (var episode in season.Episodes)
foreach (var season in child.SeasonRequests) {
{ var foundEp = await seriesEpisodes.FirstOrDefaultAsync(
foreach (var episode in season.Episodes) x => x.EpisodeNumber == episode.EpisodeNumber &&
{ x.SeasonNumber == episode.Season.SeasonNumber);
var foundEp = await seriesEpisodes.FirstOrDefaultAsync(
x => x.EpisodeNumber == episode.EpisodeNumber && if (foundEp != null)
x.SeasonNumber == episode.Season.SeasonNumber); {
episode.Available = true;
if (foundEp != null) }
{ }
episode.Available = true; }
}
} // Check to see if all of the episodes in all seasons are available for this request
} var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
if (allAvailable)
// Check to see if all of the episodes in all seasons are available for this request {
var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available)); // We have fulfulled this request!
if (allAvailable) child.Available = true;
{ _backgroundJobClient.Enqueue(() => _notificationService.Publish(new NotificationOptions
// We have fulfulled this request! {
child.Available = true; DateTime = DateTime.Now,
_backgroundJobClient.Enqueue(() => _notificationService.Publish(new NotificationOptions NotificationType = NotificationType.RequestAvailable,
{ RequestId = child.ParentRequestId,
DateTime = DateTime.Now, RequestType = RequestType.TvShow,
NotificationType = NotificationType.RequestAvailable, Recipient = child.RequestedUser.Email
RequestId = child.ParentRequestId, }));
RequestType = RequestType.TvShow, }
Recipient = child.RequestedUser.Email }
}));
} await _tvRepo.Save();
} }
await _tvRepo.Save(); private async Task ProcessMovies()
} {
// Get all non available
private async Task ProcessMovies() var movies = _movieRepo.GetAll().Where(x => !x.Available);
{
// Get all non available foreach (var movie in movies)
var movies = _movieRepo.GetAll().Where(x => !x.Available); {
PlexServerContent item = null;
foreach (var movie in movies) if (movie.ImdbId.HasValue())
{ {
PlexServerContent item = null; item = await _repo.Get(movie.ImdbId);
if (movie.ImdbId.HasValue()) }
{ if (item == null)
item = await _repo.Get(movie.ImdbId); {
} if (movie.TheMovieDbId.ToString().HasValue())
if (item == null) {
{ item = await _repo.Get(movie.TheMovieDbId.ToString());
if (movie.TheMovieDbId.ToString().HasValue()) }
{ }
item = await _repo.Get(movie.TheMovieDbId.ToString()); if (item == null)
} {
} // We don't yet have this
if (item == null) continue;
{ }
// We don't yet have this
continue; movie.Available = true;
} if (movie.Available)
{
movie.Available = true; _backgroundJobClient.Enqueue(() => _notificationService.Publish(new NotificationOptions
if (movie.Available) {
{ DateTime = DateTime.Now,
_backgroundJobClient.Enqueue(() => _notificationService.Publish(new NotificationOptions NotificationType = NotificationType.RequestAvailable,
{ RequestId = movie.Id,
DateTime = DateTime.Now, RequestType = RequestType.Movie,
NotificationType = NotificationType.RequestAvailable, Recipient = movie.RequestedUser != null ? movie.RequestedUser.Email : string.Empty
RequestId = movie.Id, }));
RequestType = RequestType.Movie, }
Recipient = movie.RequestedUser != null ? movie.RequestedUser.Email : string.Empty }
}));
} await _movieRepo.Save();
} }
}
await _movieRepo.Save();
}
}
} }

@ -13,54 +13,54 @@ export class IdentityService extends ServiceAuthHelpers {
super(http, "/api/v1/Identity/", platformLocation); super(http, "/api/v1/Identity/", platformLocation);
} }
public createWizardUser(user: ICreateWizardUser): Observable<boolean> { public createWizardUser(user: ICreateWizardUser): Observable<boolean> {
return this.regularHttp.post(`${this.url}Wizard/`, JSON.stringify(user), { headers: this.headers }).map(this.extractData); return this.regularHttp.post(`${this.url}Wizard/`, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public getUser(): Observable<IUser> { public getUser(): Observable<IUser> {
return this.http.get(this.url).map(this.extractData); return this.http.get(this.url).map(this.extractData).catch(this.handleError);
} }
public getUserById(id: string): Observable<IUser> { public getUserById(id: string): Observable<IUser> {
return this.http.get(`${this.url}User/${id}`).map(this.extractData); return this.http.get(`${this.url}User/${id}`).map(this.extractData).catch(this.handleError);
} }
public getUsers(): Observable<IUser[]> { public getUsers(): Observable<IUser[]> {
return this.http.get(`${this.url}Users`).map(this.extractData); return this.http.get(`${this.url}Users`).map(this.extractData).catch(this.handleError);
} }
public getAllAvailableClaims(): Observable<ICheckbox[]> { public getAllAvailableClaims(): Observable<ICheckbox[]> {
return this.http.get(`${this.url}Claims`).map(this.extractData); return this.http.get(`${this.url}Claims`).map(this.extractData).catch(this.handleError);
} }
public createUser(user: IUser): Observable<IIdentityResult> { public createUser(user: IUser): Observable<IIdentityResult> {
return this.http.post(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData); return this.http.post(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public updateUser(user: IUser): Observable<IIdentityResult> { public updateUser(user: IUser): Observable<IIdentityResult> {
return this.http.put(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData); return this.http.put(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public updateLocalUser(user: IUpdateLocalUser): Observable<IIdentityResult> { public updateLocalUser(user: IUpdateLocalUser): Observable<IIdentityResult> {
return this.http.put(this.url + "local", JSON.stringify(user), { headers: this.headers }).map(this.extractData); return this.http.put(this.url + "local", JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public deleteUser(user: IUser): Observable<IIdentityResult> { public deleteUser(user: IUser): Observable<IIdentityResult> {
return this.http.delete(`${this.url}${user.id}`, { headers: this.headers }).map(this.extractData); return this.http.delete(`${this.url}${user.id}`, { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public hasUserRequested(userId: string): Observable<boolean> { public hasUserRequested(userId: string): Observable<boolean> {
return this.http.get(`${this.url}userhasrequest/${userId}`).map(this.extractData); return this.http.get(`${this.url}userhasrequest/${userId}`).map(this.extractData).catch(this.handleError);
} }
public submitResetPassword(email: string): Observable<IIdentityResult> { public submitResetPassword(email: string): Observable<IIdentityResult> {
return this.regularHttp.post(this.url + "reset", JSON.stringify({email}), { headers: this.headers }).map(this.extractData); return this.regularHttp.post(this.url + "reset", JSON.stringify({email}), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public resetPassword(token: IResetPasswordToken): Observable<IIdentityResult> { public resetPassword(token: IResetPasswordToken): Observable<IIdentityResult> {
return this.regularHttp.post(this.url + "resetpassword", JSON.stringify(token), { headers: this.headers }).map(this.extractData); return this.regularHttp.post(this.url + "resetpassword", JSON.stringify(token), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public sendWelcomeEmail(user: IUser): Observable<null> { public sendWelcomeEmail(user: IUser): Observable<null> {
return this.http.post(`${this.url}welcomeEmail`, JSON.stringify(user), { headers: this.headers }).map(this.extractData); return this.http.post(`${this.url}welcomeEmail`, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
} }
public hasRole(role: string): boolean { public hasRole(role: string): boolean {

@ -1,7 +1,6 @@
import { PlatformLocation } from "@angular/common"; import { PlatformLocation } from "@angular/common";
import { Headers, Http, Response } from "@angular/http"; import { Headers, Http, Response } from "@angular/http";
import "rxjs/add/observable/throw"; import "rxjs/add/observable/throw";
import { Observable } from "rxjs/Observable";
import { AuthHttp } from "angular2-jwt"; import { AuthHttp } from "angular2-jwt";
@ -24,12 +23,17 @@ export class ServiceHelpers {
return body; return body;
} }
protected handleError(error: any) { protected handleError(error: Response | any) {
// In a real world app, we might use a remote logging infrastructure let errMsg: string;
// We'd also dig deeper into the error to get a better message if (error instanceof Response) {
const errMsg = (error.message) ? error.message : const body = error.json() || "";
error.status ? `${error.status} - ${error.statusText}` : "Server error"; const err = body.error || JSON.stringify(body);
return Observable.throw(errMsg); errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return errMsg;
} }
} }
@ -63,11 +67,16 @@ export class ServiceAuthHelpers {
} }
} }
protected handleError(error: any) { protected handleError(error: Response | any) {
// In a real world app, we might use a remote logging infrastructure let errMsg: string;
// We'd also dig deeper into the error to get a better message if (error instanceof Response) {
const errMsg = (error.message) ? error.message : const body = error.json() || "";
error.status ? `${error.status} - ${error.statusText}` : "Server error"; const err = body.error || JSON.stringify(body);
return Observable.throw(errMsg); errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
} else {
errMsg = error.Message ? error.message : error.toString();
}
console.error(errMsg);
return errMsg;
} }
} }

@ -0,0 +1,43 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace Ombi
{
public class ErrorHandlingMiddleware
{
private readonly RequestDelegate next;
public ErrorHandlingMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context /* other scoped dependencies */)
{
try
{
await next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
//if (exception is NotFoundException) code = HttpStatusCode.NotFound;
if (exception is UnauthorizedAccessException) code = HttpStatusCode.Unauthorized;
var result = JsonConvert.SerializeObject(new { error = exception.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)code;
return context.Response.WriteAsync(result);
}
}
}

@ -202,6 +202,7 @@ namespace Ombi
c.ShowJsonEditor(); c.ShowJsonEditor();
}); });
app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseMvc(routes => app.UseMvc(routes =>
{ {
routes.MapRoute( routes.MapRoute(

Loading…
Cancel
Save