示例#1
0
        /// <summary>
        /// Executed when an underlying API exception occurs.
        /// </summary>
        private void ApiExceptionHandler(object sender, ApiExceptionEventArgs args)
        {
            // Execute our callbacks. We don't handle exceptions since this is client code anyway.
            foreach (var callback in m_exceptionCallbacks)
            {
                callback(args.Message);
            }

            if (!m_config.ThrowExceptionOnApiFailure)
            {
                return;
            }

            if (!(args.Exception is ApiException apiEx))
            {
                throw new EmsApiException(args.Exception.Message, args.Exception);
            }

            // Note: This object is a Dto.V2.Error, but in that class the messageDetail
            // field is marked as required, so it will not deserialize if the details
            // are not there. In many cases the details are empty, so we parse the json
            // manually instead.
            JObject details = null;

            try
            {
                details = JObject.Parse(apiEx.Content);
            }
            catch (Exception) { }

            // We want the details if available.
            string message = null;

            if (details != null)
            {
                message = details.GetValue("messageDetail")?.ToString();
                if (string.IsNullOrEmpty(message))
                {
                    message = details.GetValue("message")?.ToString();
                }
            }

            if (string.IsNullOrEmpty(message))
            {
                message = apiEx.Message;
            }

            System.Diagnostics.Debug.WriteLine("EMS API client encountered Refit.ApiException ({0}): {1}",
                                               args.ApiException.ReasonPhrase, message);

            throw new EmsApiException(message, args.Exception);
        }
示例#2
0
        /// <summary>
        /// Executed when an underlying API exception occurs.
        /// </summary>
        private void ApiExceptionHandler(object sender, ApiExceptionEventArgs args)
        {
            // Execute our callbacks. We don't handle exceptions since this is client code anyway.
            foreach (var callback in m_exceptionCallbacks)
            {
                callback(args.Message);
            }

            if (m_config.ThrowExceptionOnApiFailure)
            {
                throw new EmsApiException("An EMS API access exception occurred, and the ThrowExceptionOnApiFailure setting is true.",
                                          args.Exception);
            }

            if (args.ApiException != null)
            {
                System.Diagnostics.Debug.WriteLine("EMS API client encountered Refit.ApiException ({0}): {1}",
                                                   args.ApiException.ReasonPhrase, args.ApiException.Message);
            }
        }