Api errors now log statuscode too.

This commit is contained in:
Taloth Saldono 2016-07-31 23:40:07 +02:00
parent 6f2dd5d2fa
commit b80d6c74ad
6 changed files with 58 additions and 4 deletions

View File

@ -1,8 +1,10 @@
using System; using System;
using System.Linq;
using System.Threading; using System.Threading;
using Nancy; using Nancy;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using NLog; using NLog;
using NzbDrone.Api.ErrorManagement;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
namespace NzbDrone.Api.Extensions.Pipelines namespace NzbDrone.Api.Extensions.Pipelines
@ -14,12 +16,20 @@ namespace NzbDrone.Api.Extensions.Pipelines
private static int _requestSequenceID; private static int _requestSequenceID;
private readonly NzbDroneErrorPipeline _errorPipeline;
public RequestLoggingPipeline(NzbDroneErrorPipeline errorPipeline)
{
_errorPipeline = errorPipeline;
}
public int Order { get { return 100; } } public int Order { get { return 100; } }
public void Register(IPipelines pipelines) public void Register(IPipelines pipelines)
{ {
pipelines.BeforeRequest.AddItemToStartOfPipeline(LogStart); pipelines.BeforeRequest.AddItemToStartOfPipeline(LogStart);
pipelines.AfterRequest.AddItemToEndOfPipeline(LogEnd); pipelines.AfterRequest.AddItemToEndOfPipeline(LogEnd);
pipelines.OnError.AddItemToEndOfPipeline(LogError);
} }
private Response LogStart(NancyContext context) private Response LogStart(NancyContext context)
@ -54,11 +64,24 @@ namespace NzbDrone.Api.Extensions.Pipelines
} }
} }
private Response LogError(NancyContext context, Exception exception)
{
var response = _errorPipeline.HandleException(context, exception);
context.Response = response;
LogEnd(context);
context.Response = null;
return response;
}
private static string GetRequestPathAndQuery(Request request) private static string GetRequestPathAndQuery(Request request)
{ {
if (request.Url.Query.IsNotNullOrWhiteSpace()) if (request.Url.Query.IsNotNullOrWhiteSpace())
{ {
return string.Concat(request.Url.Path, "?", request.Url.Query); return string.Concat(request.Url.Path, request.Url.Query);
} }
else else
{ {

View File

@ -38,8 +38,6 @@ namespace NzbDrone.Api
container.Resolve<DatabaseTarget>().Register(); container.Resolve<DatabaseTarget>().Register();
container.Resolve<IEventAggregator>().PublishEvent(new ApplicationStartedEvent()); container.Resolve<IEventAggregator>().PublishEvent(new ApplicationStartedEvent());
ApplicationPipelines.OnError.AddItemToEndOfPipeline((Func<NancyContext, Exception, Response>) container.Resolve<NzbDroneErrorPipeline>().HandleException);
} }
private void RegisterPipelines(IPipelines pipelines) private void RegisterPipelines(IPipelines pipelines)

View File

@ -0,0 +1,30 @@
using System.IO;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
namespace NzbDrone.Integration.Test
{
[TestFixture]
public class HttpLogFixture : IntegrationTest
{
[Test]
public void should_log_on_error()
{
var config = HostConfig.Get(1);
config.LogLevel = "Trace";
HostConfig.Put(config);
var logFile = Path.Combine(_runner.AppData, "logs", "sonarr.trace.txt");
var logLines = File.ReadAllLines(logFile);
var result = Series.InvalidPost(new Api.Series.SeriesResource());
logLines = File.ReadAllLines(logFile).Skip(logLines.Length).ToArray();
logLines.Should().Contain(v => v.Contains("|Trace|Http|Req"));
logLines.Should().Contain(v => v.Contains("|Trace|Http|Res"));
logLines.Should().Contain(v => v.Contains("|Debug|Api|"));
}
}
}

View File

@ -9,7 +9,7 @@ namespace NzbDrone.Integration.Test
{ {
public abstract class IntegrationTest : IntegrationTestBase public abstract class IntegrationTest : IntegrationTestBase
{ {
private NzbDroneRunner _runner; protected NzbDroneRunner _runner;
public override string SeriesRootFolder public override string SeriesRootFolder
{ {

View File

@ -44,6 +44,7 @@ namespace NzbDrone.Integration.Test
public DownloadClientClient DownloadClients; public DownloadClientClient DownloadClients;
public EpisodeClient Episodes; public EpisodeClient Episodes;
public ClientBase<HistoryResource> History; public ClientBase<HistoryResource> History;
public ClientBase<HostConfigResource> HostConfig;
public IndexerClient Indexers; public IndexerClient Indexers;
public ClientBase<NamingConfigResource> NamingConfig; public ClientBase<NamingConfigResource> NamingConfig;
public NotificationClient Notifications; public NotificationClient Notifications;
@ -109,6 +110,7 @@ namespace NzbDrone.Integration.Test
DownloadClients = new DownloadClientClient(RestClient, ApiKey); DownloadClients = new DownloadClientClient(RestClient, ApiKey);
Episodes = new EpisodeClient(RestClient, ApiKey); Episodes = new EpisodeClient(RestClient, ApiKey);
History = new ClientBase<HistoryResource>(RestClient, ApiKey); History = new ClientBase<HistoryResource>(RestClient, ApiKey);
HostConfig = new ClientBase<HostConfigResource>(RestClient, ApiKey, "config/host");
Indexers = new IndexerClient(RestClient, ApiKey); Indexers = new IndexerClient(RestClient, ApiKey);
NamingConfig = new ClientBase<NamingConfigResource>(RestClient, ApiKey, "config/naming"); NamingConfig = new ClientBase<NamingConfigResource>(RestClient, ApiKey, "config/naming");
Notifications = new NotificationClient(RestClient, ApiKey); Notifications = new NotificationClient(RestClient, ApiKey);

View File

@ -124,6 +124,7 @@
<Compile Include="ApiTests\EpisodeFixture.cs" /> <Compile Include="ApiTests\EpisodeFixture.cs" />
<Compile Include="ApiTests\HistoryFixture.cs" /> <Compile Include="ApiTests\HistoryFixture.cs" />
<Compile Include="ApiTests\IndexerFixture.cs" /> <Compile Include="ApiTests\IndexerFixture.cs" />
<Compile Include="HttpLogFixture.cs" />
<Compile Include="IndexHtmlFixture.cs" /> <Compile Include="IndexHtmlFixture.cs" />
<Compile Include="IntegrationTest.cs" /> <Compile Include="IntegrationTest.cs" />
<Compile Include="IntegrationTestBase.cs" /> <Compile Include="IntegrationTestBase.cs" />