示例#1
0
 internal static void HandleException(ApiException exception, ApiExceptionHandlerConfig config)
 {
     if (IsSessionExpired(exception))
     {
         HandleSessionExpiry(exception, config);
     }
     else if (HasNoNetworkConnectivity(exception, config))
     {
         HandleNoNetworkConnectivity(exception, config);
     }
     else if (!exception.HasContent)
     {
         LogException(exception, "", config.Logger);
     }
 }
示例#2
0
        /// <summary>
        /// Waits for an API request to complete and return a response
        /// </summary>
        /// <param name="task">A <see cref="Task"/> representing the API request to be awaited</param>
        /// <param name="config">A <see cref="ApiExceptionHandlerConfig"/> specifying any error handlers and logger to use</param>
        /// <param name="continueOnCapturedContext">Specifies whether the resonse should return on the same thread as the request.
        /// Setting this value to <c>false</c> can improve performance by avoiding thread context switches.</param>
        /// <returns>A <see cref="ServiceResult"/> instance representing the status of the completed request</returns>
        public static async Task <ServiceResult> WaitForResponse(this Task task, ApiExceptionHandlerConfig config, bool continueOnCapturedContext = true)
        {
            try
            {
                await task.ConfigureAwait(continueOnCapturedContext);

                return(ServiceResult.AsSuccess());
            }
            catch (ApiException ex)
            {
                HandleException(ex, config);

                return(ServiceResult.AsFailure(new ApiHandledException(ex), statusCode: (int)ex.StatusCode));
            }
            catch (Exception ex)
            {
                return(ServiceResult.AsFailure(new ApiHandledException(ex)));
            }
        }
示例#3
0
        internal static bool HasNoNetworkConnectivity(ApiException exception, ApiExceptionHandlerConfig config)
        {
            if (exception.HasContent && exception.StatusCode == HttpStatusCode.RequestTimeout)
            {
                try
                {
                    var result = exception.GetErrorResult(ApiConfiguration.DefaultJsonSerializationSettingsFactory());
                    if (String.Equals(result.ErrorMessages(), ErrorMessages.NoNetWorkError, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
                catch (Exception internalException)
                {
                    LogException(internalException, "No network connectivity");
                }
            }

            return(false);
        }
示例#4
0
        internal static void HandleNoNetworkConnectivity(ApiException exception, ApiExceptionHandlerConfig config)
        {
            var r = exception.GetErrorResult(ApiConfiguration.DefaultJsonSerializationSettingsFactory());

            config.OnNoNetworkConnectivity?.Invoke(ServiceResult.AsFailure(r.ErrorMessages(), ErrorMessages.NoNetworkErrorCode));
        }
示例#5
0
 internal static void HandleSessionExpiry(ApiException exception, ApiExceptionHandlerConfig config)
 {
     config.OnSessionExpired?.Invoke(ServiceResult.AsFailure(ErrorMessages.SessionTimedout, (int)exception.StatusCode));
 }