Skip to content

Commit

Permalink
91 extend exception for keeping internal info from http response (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleh-Yanushkevich authored Oct 22, 2022
1 parent 47da8c3 commit efc1072
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 31 deletions.
20 changes: 0 additions & 20 deletions src/ReportPortal.Client/ReportPortalException.cs

This file was deleted.

10 changes: 8 additions & 2 deletions src/ReportPortal.Client/Resources/ServiceBaseResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ private void CheckSuccessStatusCode(HttpResponseMessage response, Stream stream)
{
using (var reader = new StreamReader(stream))
{
string body = reader.ReadToEnd();
throw new ReportPortalException($"Response status code does not indicate success: {response.StatusCode} ({(int)response.StatusCode}) {response.RequestMessage.Method} {response.RequestMessage.RequestUri}", new HttpRequestException($"Response message: {body}"));
string responseBody = reader.ReadToEnd();

throw new ServiceException(
"Response status code does not indicate success.",
response.StatusCode,
response.RequestMessage.RequestUri,
response.RequestMessage.Method,
responseBody);
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions src/ReportPortal.Client/ServiceException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Net;
using System.Net.Http;

namespace ReportPortal.Client
{
/// <summary>
/// Occurs when server cannot process a request.
/// </summary>
public class ServiceException : Exception
{
/// <summary>
/// Initializes a new instance of <see cref="ServiceException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="httpStatusCode">Response HTTP status code.</param>
/// <param name="requestUri">Request Uri.</param>
/// <param name="httpMethod">HTTP method.</param>
/// <param name="responseBody">Response body.</param>
public ServiceException(string message, HttpStatusCode httpStatusCode, Uri requestUri, HttpMethod httpMethod, string responseBody)
{
HttpStatusCode = httpStatusCode;
RequestUri = requestUri;
HttpMethod = httpMethod;
ResponseBody = responseBody;

_message = $"{message}\n {httpStatusCode} ({(int)httpStatusCode}) {httpMethod} {requestUri}\n {responseBody}";
}

private readonly string _message;

/// <summary>
/// Gets HTTP status code.
/// </summary>
public HttpStatusCode HttpStatusCode { get; }

/// <summary>
/// Gets request uri.
/// </summary>
public Uri RequestUri { get; }

/// <summary>
/// Gets HTTP method.
/// </summary>
public HttpMethod HttpMethod { get; }

/// <summary>
/// Gets response body.
/// </summary>
public string ResponseBody { get; }

/// <inheritdoc/>
public override string Message => _message;
}
}
2 changes: 1 addition & 1 deletion test/ReportPortal.Client.IntegrationTests/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BaseFixture
{
protected static readonly string Username = "default";
protected static readonly string ProjectName = "default_personal";
protected readonly Service Service = new Service(new Uri("https://demo.reportportal.io/api/v1"), ProjectName, "aeb09a3b-18ef-4e9e-b647-4645c0e9a74e");
protected readonly Service Service = new Service(new Uri("https://demo.reportportal.io/api/v1"), ProjectName, "0e80c23b-9676-4b6b-8255-18ef1dece8b5");
//protected static readonly string Username = "default";
//protected static readonly string ProjectName = "default_personal";
//protected readonly Service Service = new Service(new Uri("http://localhost:8080/api/v1"), ProjectName, "d562c898-7705-49a4-be6d-17a8121715fa");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GetLaunchesItemFixture : BaseFixture, IClassFixture<LaunchesFixture
[Fact]
public async Task GetInvalidLaunch()
{
await Assert.ThrowsAsync<ReportPortalException>(async () => await Service.Launch.GetAsync("invalid_id"));
await Assert.ThrowsAsync<ServiceException>(async () => await Service.Launch.GetAsync("invalid_id"));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public async Task RerunLaunch()
await Service.Launch.FinishAsync(launch1Response.Uuid, new FinishLaunchRequest { EndTime = DateTime.UtcNow });

// api doesn't allow to finish launch twice?! So when using rerun, we can start launch, but it seems we should not finish launch
await Assert.ThrowsAnyAsync<ReportPortalException>(async () => await Service.Launch.FinishAsync(launch2Response.Uuid, new FinishLaunchRequest { EndTime = DateTime.UtcNow }));
await Assert.ThrowsAnyAsync<ServiceException>(async () => await Service.Launch.FinishAsync(launch2Response.Uuid, new FinishLaunchRequest { EndTime = DateTime.UtcNow }));

var gotLaunch = await Service.Launch.GetAsync(launch1Response.Uuid);
await Service.Launch.DeleteAsync(gotLaunch.Id);
Expand Down Expand Up @@ -369,7 +369,7 @@ public async Task RerunToSpecificLaunch()
await Service.Launch.FinishAsync(launch1Response.Uuid, new FinishLaunchRequest { EndTime = DateTime.UtcNow });

// api doesn't allow to finish launch twice?! So when using rerun, we can start launch, but it seems we should not finish launch
await Assert.ThrowsAnyAsync<ReportPortalException>(async () => await Service.Launch.FinishAsync(launch2Response.Uuid, new FinishLaunchRequest { EndTime = DateTime.UtcNow }));
await Assert.ThrowsAnyAsync<ServiceException>(async () => await Service.Launch.FinishAsync(launch2Response.Uuid, new FinishLaunchRequest { EndTime = DateTime.UtcNow }));

var gotLaunch = await Service.Launch.GetAsync(launch1Response.Uuid);
await Service.Launch.DeleteAsync(gotLaunch.Id);
Expand All @@ -386,7 +386,7 @@ public async Task RerunNonExistingLaunch()
IsRerun = true
};

await Assert.ThrowsAnyAsync<ReportPortalException>(async () => await Service.Launch.StartAsync(request));
await Assert.ThrowsAnyAsync<ServiceException>(async () => await Service.Launch.StartAsync(request));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public async Task IncorrectHost()
public async Task IncorrectUrlInCorrectHost()
{
var service = new Service(new Uri("https://rp.epam.com/api/blabla/"), "p", "p");
await Assert.ThrowsAsync<ReportPortalException>(async () => await service.Launch.StartAsync(new StartLaunchRequest { Name = "abc" }));
await Assert.ThrowsAsync<ServiceException>(async () => await service.Launch.StartAsync(new StartLaunchRequest { Name = "abc" }));
}

[Fact]
public async Task IncorrectUuid()
{
var service = new Service(new Uri("https://rp.epam.com/api/v1/"), "default_project", "incorrect_uuid");
await Assert.ThrowsAsync<ReportPortalException>(async () => await service.Launch.GetAsync());
await Assert.ThrowsAsync<ServiceException>(async () => await service.Launch.GetAsync());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public async Task UpdateOfInProgressIsNotAllowed()

Func<Task> act = async () => await Service.TestItem.UpdateAsync(tempTest.Id, updateRequest);

var exp = await act.Should().ThrowExactlyAsync<ReportPortalException>();
exp.WithInnerExceptionExactly<HttpRequestException>().WithMessage("*Unable to update status*");
var exp = await act.Should().ThrowExactlyAsync<ServiceException>();
exp.Which.ResponseBody.Should().Contain("Unable to update status");

await Service.TestItem.FinishAsync(test.Uuid, new FinishTestItemRequest
{
Expand Down

0 comments on commit efc1072

Please sign in to comment.