Esempio n. 1
0
        public static LndError2 AsLNDError(this SwaggerException swagger)
        {
            var error = JsonConvert.DeserializeObject <LndError2>(swagger.Response);

            if (error.Error == null)
            {
                return(null);
            }
            return(error);
        }
        public static void Log(SwaggerException ex)
        {
            var additionalProperties = new Dictionary <string, string>
            {
                { "HTTP status code", ex.StatusCode.ToString() },
                { "HTTP response", ex.Response }
            };

            TelemetryClient.TrackException(ex, additionalProperties);
        }
 public void ThenTheResponseShouldHaveAStatusOf(int expectedStatusCode)
 {
     if (expectedStatusCode >= 300)
     {
         SwaggerException ex = this.scenarioContext.GetLastApiException();
         Assert.AreEqual(expectedStatusCode, ex.StatusCode);
     }
     else
     {
         SwaggerResponse response = this.scenarioContext.GetLastApiResponse();
         Assert.AreEqual(expectedStatusCode, response.StatusCode);
     }
 }
Esempio n. 4
0
 private static Dictionary <string, string[]> ParseResponse(SwaggerException ex)
 {
     if (ex.Headers.TryGetValue("Content-Type", out var contentType) && contentType.Contains("application/json"))
     {
         return(JsonConvert.DeserializeObject <Dictionary <string, string[]> >(ex.Response));
     }
     else
     {
         return(new Dictionary <string, string[]>()
         {
             { "", new[] { ex.Response } }
         });
     }
 }
Esempio n. 5
0
        private TRes Wrap <TRes>(Func <TRes> func)
        {
            const int        retryCount       = 3;
            Exception        exception        = null;
            SwaggerException swaggerException = null;

            for (var x = 0; !_cancellationToken.IsCancellationRequested && x < retryCount; x++)
            {
#if DEBUGAPI
                var timer = new Stopwatch();
                timer.Start();
#endif
                try
                {
                    return(func());
                }
                catch (Exception e)
                {
                    exception        = e;
                    swaggerException = e as SwaggerException;
#if DEBUGAPI
                    if (swaggerException != null)
                    {
                        _logger.Warn($"{swaggerException.StatusCode} : {swaggerException.Response}", ThreadId);
                    }

                    string member = new StackFrame(2).GetMethod().Name;
                    _logger.Exception(e, ThreadId, member);
#endif
                }
#if DEBUGAPI
                finally
                {
                    _logger.Debug($"API call took {timer.ElapsedMilliseconds} ms", ThreadId);
                    timer.Stop();
                }
#endif
                var wt = Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken);
                wt.WaitAndUnwrapException(_cancellationToken);
            }
            if (swaggerException != null)
            {
                _logger.Warn($"{swaggerException.StatusCode} : {swaggerException.Response}", ThreadId);
            }

            string caller = new StackFrame(2).GetMethod().Name;
            _logger.Exception(exception ?? new Exception(nameof(ApiWrapper)), ThreadId, caller);
            return(default(TRes));
        }
Esempio n. 6
0
        private static void HandleValidation(IDotvvmRequestContext context, SwaggerException ex)
        {
            var invalidProperties = ParseResponse(ex);

            foreach (var property in invalidProperties)
            {
                foreach (var error in property.Value)
                {
                    context.ModelState.Errors.Add(new ViewModelValidationError()
                    {
                        PropertyPath = ConvertPropertyName(property.Key), ErrorMessage = error
                    });
                }
            }

            context.FailOnInvalidModelState();
        }
Esempio n. 7
0
 public static ApiResult ToResult(this SwaggerException swaggerException)
 {
     return(Newtonsoft.Json.JsonConvert.DeserializeObject <ApiResult>(swaggerException.Response));
 }
Esempio n. 8
0
 /// <summary>
 /// Stores the given exception in the context.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="ex">The exception to store.</param>
 public static void StoreLastApiException(this ScenarioContext context, SwaggerException ex)
 {
     context.Set(ex, LastApiExceptionKey);
 }