Exemplo n.º 1
0
        public ApiErrorInfo Build(TEx ex, ApiErroInfoBuilderOptions options)
        {
            var message = options.LoggingLevel == ApiErroInfoBuilderOptions.LoggingLevels.Verbose ? ex.ToString() : ex.Message;
            var result  = new ApiErrorInfo(message);

            return(result);
        }
        public ApiErrorInfo Build(ValidationException ex, ApiErroInfoBuilderOptions options)
        {
            var message = options.LoggingLevel == ApiErroInfoBuilderOptions.LoggingLevels.Verbose ? ex.ToString() : ex.Message;
            var errors  = ex.Errors.Select(err => new ApiError(err.Context, err.Message));

            var result = new ApiErrorInfo(message, errors);

            return(result);
        }
Exemplo n.º 3
0
            public ApiErrorInfo(Exception exception)
            {
                Code = exception.GetType().FullName;

                Message = exception.Message;

                if (exception.InnerException != null)
                {
                    InnerException = new ApiErrorInfo(exception.InnerException);
                }
            }
Exemplo n.º 4
0
        public ApiError(string code, string message, Exception exception)
        {
            Error = new ApiErrorInfo()
            {
                Code = code, Message = message
            };

            if (exception != null)
            {
                Error.InnerException = new ApiErrorInfo(exception);
            }
        }
        /// <summary>
        /// Calls Configured API as per need. This is generic method supports GET POST PUT etc...
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="httpMethod"></param>
        /// <param name="apiBaseUrl"></param>
        /// <param name="uri"></param>
        /// <param name="apiToken"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public async Task <T> ApiCall <T>(HttpMethod httpMethod, string apiBaseUrl, string uri, string apiToken, object body = null)
        {
            try
            {
                HttpResponseMessage response = null;

                var httpclient = _httpFactory.CreateClient();

                using (var request = CreateJsonRequest(httpMethod, apiBaseUrl, uri, apiToken, body))
                {
                    response = await httpclient.SendAsync(request);

                    if (response.IsSuccessStatusCode)
                    {
                        T value = await response.Content.ReadAsAsync <T>();

                        return(value);
                    }
                    else
                    {
                        ApiErrorInfo apiErrorInfo = await response.Content.ReadAsAsync <ApiErrorInfo>();

                        switch (apiErrorInfo.Status)
                        {
                        case "406":
                            throw new BadInputException(apiErrorInfo.Detail);

                        default:
                            throw new ApiException(apiErrorInfo.Detail);
                        }
                    }
                }

                _logger.LogWarning($"ApiCall: StatusCode={response.StatusCode}");

                return(default(T));
            }
            catch (Exception ex)
            {
                _logger.LogError($"ApiCall: Exception={ex}");
                throw ex;
            }
        }
Exemplo n.º 6
0
 public ApiError(Exception exception)
 {
     Error = new ApiErrorInfo(exception);
 }