Fix the exception in #440

This commit is contained in:
tidusjar 2016-07-26 20:01:29 +01:00
parent f03392691c
commit 85baa67f29
3 changed files with 120 additions and 113 deletions

View file

@ -1,87 +1,88 @@
#region Copyright #region Copyright
// /************************************************************************ // /************************************************************************
// Copyright (c) 2016 Jamie Rees // Copyright (c) 2016 Jamie Rees
// File: ValidationHelper.cs // File: ValidationHelper.cs
// Created By: Jamie Rees // Created By: Jamie Rees
// //
// Permission is hereby granted, free of charge, to any person obtaining // Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the // a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including // "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish, // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to // distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to // permit persons to whom the Software is furnished to do so, subject to
// the following conditions: // the following conditions:
// //
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Nancy.Validation; using Nancy.Validation;
using PlexRequests.UI.Models; using PlexRequests.UI.Models;
namespace PlexRequests.UI.Helpers namespace PlexRequests.UI.Helpers
{ {
public static class ValidationHelper public static class ValidationHelper
{ {
/// <summary> /// <summary>
/// This will send the first error as a JsonResponseModel /// This will send the first error as a JsonResponseModel
/// </summary> /// </summary>
/// <param name="result">The result.</param> /// <param name="result">The result.</param>
/// <returns></returns> /// <returns></returns>
public static JsonResponseModel SendJsonError(this ModelValidationResult result) public static JsonResponseModel SendJsonError(this ModelValidationResult result)
{ {
var errors = result.Errors; var errors = result.Errors;
return errors return errors
.Select(e => e.Value.FirstOrDefault()) .Select(e => e.Value.FirstOrDefault())
.Where(modelValidationError => modelValidationError != null) .Where(modelValidationError => modelValidationError != null)
.Select(modelValidationError => .Select(modelValidationError =>
new JsonResponseModel new JsonResponseModel
{ {
Result = false, Result = false,
Message = modelValidationError.ErrorMessage Message = modelValidationError.ErrorMessage
}) })
.FirstOrDefault(); .FirstOrDefault();
} }
public static JsonResponseModel SendSonarrError(List<string> result) public static JsonResponseModel SendSonarrError(List<string> result)
{ {
var model = new JsonResponseModel {Result = false};
if (!result.Any()) var model = new JsonResponseModel {Result = false};
{ if (!result.Any())
return model; {
} return model;
var sb = new StringBuilder(); }
sb.AppendLine("Errors from Sonarr: "); var sb = new StringBuilder();
for (var i = 0; i < result.Count; i++) sb.AppendLine("Errors from Sonarr: ");
{ for (var i = 0; i < result.Count; i++)
if (i != result.Count - 1) {
{ if (i != result.Count - 1)
sb.AppendLine(result[i] + ","); {
} sb.AppendLine(result[i] + ",");
else }
{ else
sb.AppendLine(result[i]); {
} sb.AppendLine(result[i]);
} }
}
model.Message = sb.ToString();
model.Message = sb.ToString();
return model;
} return model;
} }
}
} }

View file

@ -637,8 +637,8 @@ namespace PlexRequests.UI.Modules
{ {
return await AddRequest(model, settings, $"{fullShowName} {Resources.UI.Search_SuccessfullyAdded}"); return await AddRequest(model, settings, $"{fullShowName} {Resources.UI.Search_SuccessfullyAdded}");
} }
Log.Debug("Error with sending to sonarr.");
return Response.AsJson(ValidationHelper.SendSonarrError(result?.ErrorMessages)); return Response.AsJson(ValidationHelper.SendSonarrError(result?.ErrorMessages ?? new List<string>()));
} }
var srSettings = SickRageService.GetSettings(); var srSettings = SickRageService.GetSettings();

View file

@ -157,9 +157,15 @@
</div> </div>
<div class="col-sm-5 "> <div class="col-sm-5 ">
<div> <div>
{{#if_eq type "movie"}}
<a href="https://www.themoviedb.org/movie/{{id}}/" target="_blank">
<h4>{{title}} ({{year}})</h4>
</a>
{{else}}
<a href="http://www.imdb.com/title/{{imdb}}/" target="_blank"> <a href="http://www.imdb.com/title/{{imdb}}/" target="_blank">
<h4>{{title}} ({{year}})</h4> <h4>{{title}} ({{year}})</h4>
</a> </a>
{{/if_eq}}
</div> </div>
<p>{{overview}}</p> <p>{{overview}}</p>
</div> </div>
@ -167,30 +173,30 @@
<form method="POST" action="@url/search/request/{{type}}" id="form{{id}}"> <form method="POST" action="@url/search/request/{{type}}" id="form{{id}}">
<input name="{{type}}Id" type="text" value="{{id}}" hidden="hidden" /> <input name="{{type}}Id" type="text" value="{{id}}" hidden="hidden" />
{{#if_eq type "movie"}} {{#if_eq type "movie"}}
{{#if_eq available true}} {{#if_eq available true}}
<button style="text-align: right" class="btn btn-success-outline disabled" disabled><i class="fa fa-check"></i> @UI.Search_Available</button> <button style="text-align: right" class="btn btn-success-outline disabled" disabled><i class="fa fa-check"></i> @UI.Search_Available</button>
{{else}} {{else}}
{{#if_eq requested true}} {{#if_eq requested true}}
<button style="text-align: right" class="btn btn-primary-outline disabled" disabled><i class="fa fa-check"></i> @UI.Search_Requested</button> <button style="text-align: right" class="btn btn-primary-outline disabled" disabled><i class="fa fa-check"></i> @UI.Search_Requested</button>
{{else}} {{else}}
<button id="{{id}}" style="text-align: right" class="btn btn-primary-outline requestMovie" type="submit"><i class="fa fa-plus"></i> @UI.Search_Request</button> <button id="{{id}}" style="text-align: right" class="btn btn-primary-outline requestMovie" type="submit"><i class="fa fa-plus"></i> @UI.Search_Request</button>
{{/if_eq}} {{/if_eq}}
{{/if_eq}} {{/if_eq}}
{{/if_eq}} {{/if_eq}}
{{#if_eq type "tv"}} {{#if_eq type "tv"}}
<div class="dropdown"> <div class="dropdown">
<button id="{{id}}" class="btn btn-primary-outline dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> <button id="{{id}}" class="btn btn-primary-outline dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<i class="fa fa-plus"></i> @UI.Search_Request <i class="fa fa-plus"></i> @UI.Search_Request
<span class="caret"></span> <span class="caret"></span>
</button> </button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><a id="{{id}}" season-select="0" class="dropdownTv " href="#">@UI.Search_AllSeasons</a></li> <li><a id="{{id}}" season-select="0" class="dropdownTv " href="#">@UI.Search_AllSeasons</a></li>
<li><a id="{{id}}" season-select="1" class="dropdownTv" href="#">@UI.Search_FirstSeason</a></li> <li><a id="{{id}}" season-select="1" class="dropdownTv" href="#">@UI.Search_FirstSeason</a></li>
<li><a id="{{id}}" season-select="2" class="dropdownTv" href="#">@UI.Search_LatestSeason</a></li> <li><a id="{{id}}" season-select="2" class="dropdownTv" href="#">@UI.Search_LatestSeason</a></li>
<li><a id="SeasonSelect" data-identifier="{{id}}" data-toggle="modal" data-target="#seasonsModal" href="#">@UI.Search_SelectSeason...</a></li> <li><a id="SeasonSelect" data-identifier="{{id}}" data-toggle="modal" data-target="#seasonsModal" href="#">@UI.Search_SelectSeason...</a></li>
<li><a id="EpisodeSelect" data-identifier="{{id}}" data-toggle="modal" data-target="#episodesModal" href="#">@UI.Search_SelectEpisode...</a></li> <li><a id="EpisodeSelect" data-identifier="{{id}}" data-toggle="modal" data-target="#episodesModal" href="#">@UI.Search_SelectEpisode...</a></li>
</ul> </ul>
</div> </div>
{{/if_eq}} {{/if_eq}}
@ -341,9 +347,9 @@
</script> </script>
<script id="seasonNumber-template" type="text/x-handlebars-template"> <script id="seasonNumber-template" type="text/x-handlebars-template">
<br /> <br />
<br /> <br />
<br /> <br />
<div id="seasonNumber{{seasonNumber}}"> <div id="seasonNumber{{seasonNumber}}">
<strong>@UI.Search_Season {{seasonNumber}}</strong> <strong>@UI.Search_Season {{seasonNumber}}</strong>
</div> </div>