Skip to content

Commit

Permalink
Merge pull request #22 from LykkeCity/client-exceptions
Browse files Browse the repository at this point in the history
changed ValidationApiException to contain ErrorResponse
  • Loading branch information
samodovdi committed Jul 2, 2018
2 parents 3c355b6 + 612adcf commit 2e891e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Lykke.Common.ApiLibrary/Exceptions/ValidationApiException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net;
using JetBrains.Annotations;
using Lykke.Common.Api.Contract.Responses;

namespace Lykke.Common.ApiLibrary.Exceptions
{
Expand All @@ -9,14 +10,27 @@ namespace Lykke.Common.ApiLibrary.Exceptions
public class ValidationApiException : Exception
{
public HttpStatusCode StatusCode { get; set; }
public ErrorResponse ErrorResponse { get; set; }

public ValidationApiException(ErrorResponse response) : this(HttpStatusCode.BadRequest, response)
{
}

public ValidationApiException(HttpStatusCode code, ErrorResponse response) : base(response.ErrorMessage)
{
StatusCode = code;
ErrorResponse = response;
}

public ValidationApiException(string message) : this(HttpStatusCode.BadRequest, message)
{
ErrorResponse = ErrorResponse.Create(message);
}

public ValidationApiException(HttpStatusCode code, string message) : base(message)
{
StatusCode = code;
ErrorResponse = ErrorResponse.Create(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public async Task Invoke(HttpContext context)
}
catch (ValidationApiException exception)
{
await CreateErrorResponse(context, exception.Message, exception.StatusCode);
await CreateErrorResponse(context, exception.ErrorResponse, exception.StatusCode);
}
}

private static async Task CreateErrorResponse(HttpContext ctx, string message, HttpStatusCode status)
private static async Task CreateErrorResponse(HttpContext ctx, ErrorResponse response, HttpStatusCode status)
{
ctx.Response.Clear();
ctx.Response.ContentType = "application/json";
ctx.Response.StatusCode = (int)status;

var json = JsonConvert.SerializeObject(ErrorResponse.Create(message));
var json = JsonConvert.SerializeObject(response);

await ctx.Response.WriteAsync(json);
}
Expand Down

0 comments on commit 2e891e7

Please sign in to comment.