<commit_before>﻿using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace Mindscape.Raygun4Net.WebApi
{
  public class RaygunWebApiExceptionFilter : ExceptionFilterAttribute
  {
    private readonly IRaygunWebApiClientProvider _clientCreator;

    internal RaygunWebApiExceptionFilter(IRaygunWebApiClientProvider clientCreator)
    {
      _clientCreator = clientCreator;
    }

    public override void OnException(HttpActionExecutedContext context)
    {
      _clientCreator.GenerateRaygunWebApiClient().CurrentHttpRequest(context.Request).Send(context.Exception);
    }

    public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
    {
      return Task.Factory.StartNew(() => _clientCreator.GenerateRaygunWebApiClient().CurrentHttpRequest(context.Request).Send(context.Exception), cancellationToken);
    }
  }

  public class RaygunWebApiActionFilter : ActionFilterAttribute
  {
    private readonly IRaygunWebApiClientProvider _clientCreator;

    internal RaygunWebApiActionFilter(IRaygunWebApiClientProvider clientCreator)
    {
      _clientCreator = clientCreator;
    }

    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
      base.OnActionExecuted(context);

      if (context != null && context.Response != null && (int)context.Response.StatusCode >= 400)
      {
        try
        {
          throw new RaygunWebApiHttpException(
            context.Response.StatusCode,
            string.Format("HTTP {0} returned while handling Request {2} {1}", (int)context.Response.StatusCode, context.Request.RequestUri, context.Request.Method));
        }
        catch (Exception e)
        {
          _clientCreator.GenerateRaygunWebApiClient().Send(e);
        }
      }
    }
  }

  public class RaygunWebApiHttpException : Exception
  {
    public HttpStatusCode StatusCode { get; set; }

    public RaygunWebApiHttpException(HttpStatusCode statusCode, string message)
      : base(message)
    {
      StatusCode = statusCode;
    }
  }<commit_msg>Set current HTTP request when sending Status Code exceptions from WebApi<commit_after>﻿using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace Mindscape.Raygun4Net.WebApi
{
  public class RaygunWebApiExceptionFilter : ExceptionFilterAttribute
  {
    private readonly IRaygunWebApiClientProvider _clientCreator;

    internal RaygunWebApiExceptionFilter(IRaygunWebApiClientProvider clientCreator)
    {
      _clientCreator = clientCreator;
    }

    public override void OnException(HttpActionExecutedContext context)
    {
      _clientCreator.GenerateRaygunWebApiClient().CurrentHttpRequest(context.Request).Send(context.Exception);
    }

    public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
    {
      return Task.Factory.StartNew(() => _clientCreator.GenerateRaygunWebApiClient().CurrentHttpRequest(context.Request).Send(context.Exception), cancellationToken);
    }
  }

  public class RaygunWebApiActionFilter : ActionFilterAttribute
  {
    private readonly IRaygunWebApiClientProvider _clientCreator;

    internal RaygunWebApiActionFilter(IRaygunWebApiClientProvider clientCreator)
    {
      _clientCreator = clientCreator;
    }

    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
      base.OnActionExecuted(context);

      if (context != null && context.Response != null && (int)context.Response.StatusCode >= 400)
      {
        try
        {
          throw new RaygunWebApiHttpException(
            context.Response.StatusCode,
            string.Format("HTTP {0} returned while handling Request {2} {1}", (int)context.Response.StatusCode, context.Request.RequestUri, context.Request.Method));
        }
        catch (Exception e)
        {
          _clientCreator.GenerateRaygunWebApiClient().CurrentHttpRequest(context.Request).Send(e);
        }
      }
    }
  }

  public class RaygunWebApiHttpException : Exception
  {
    public HttpStatusCode StatusCode { get; set; }

    public RaygunWebApiHttpException(HttpStatusCode statusCode, string message)
      : base(message)
    {
      StatusCode = statusCode;
    }
  }
{'fraction_non_alphanumeric': 0.07747290753977404, 'fraction_numerical': 0.0032280378141572516, 'mean_word_length': 3.81465038845727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008182', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1152, 'n_words': 220}