Пример #1
0
        public ApiException(ApiExceptionResponse error, params object?[] args) : base(string.Format(error.ResponseMessage, args))
        {
            Error = new ApiExceptionResponse(error);

            this.Error.ResponseMessage = string.Format(Error.ResponseMessage, args);
            this.Error.ResponseDetails = string.IsNullOrEmpty(Error.ResponseDetails) ? null : string.Format(Error.ResponseDetails, args);
        }
Пример #2
0
        public void OnException(ExceptionContext context)
        {
            //handled error
            if (context.Exception is URLShortner.ApiException)
            {
                var ex = context.Exception as ApiException;
                context.Exception = null;
                var ApiError = new ApiExceptionResponse(ex.Message);

                HttpResponse response = context.HttpContext.Response;
                response.StatusCode  = ex.StatusCode;
                response.ContentType = "application/json";

                context.Result = new JsonResult(ApiError);
            }
            //unhandled error
            else
            {
                var ex = context.Exception as ApiException;
                context.Exception = null;
                var ApiError = new ApiExceptionResponse("Unhandled Exception");

                HttpResponse response = context.HttpContext.Response;
                response.StatusCode  = ex.StatusCode;
                response.ContentType = "application/json";

                context.Result = new JsonResult(ApiError);
                //handle logging here
            }
        }
Пример #3
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (OperationCanceledException)
            {
                _logger.LogWarning("Request was cancelled");

                context.Response.Clear();
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
            catch (Exception exception)
            {
                const string message = "Unhandled exception has occurred";
                _logger.LogError(exception, message);

                context.Response.Clear();
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                if (_webHostEnvironment.IsDevelopment() || _webHostEnvironment.IsStaging())
                {
                    var content = new ApiExceptionResponse(message, exception);

                    var outputFormatterSelector = context.RequestServices.GetService <OutputFormatterSelector>();
                    var writersFactory          = context.RequestServices.GetService <IHttpResponseStreamWriterFactory>();
                    if (outputFormatterSelector != null && writersFactory != null)
                    {
                        var formatterContext = new OutputFormatterWriteContext(
                            context,
                            writersFactory.CreateWriter,
                            content.GetType(),
                            content
                            );
                        var selectedFormatter = outputFormatterSelector.SelectFormatter(
                            formatterContext,
                            new List <IOutputFormatter>(),
                            new MediaTypeCollection()
                            );

                        if (selectedFormatter != null)
                        {
                            await selectedFormatter.WriteAsync(formatterContext);
                        }
                    }
                    else
                    {
                        context.Response.ContentType = "application/json; charset=utf-8";
                        await context.Response.WriteAsync(
                            JsonConvert.SerializeObject(content, _jsonSerializerSettings)
                            );
                    }
                }
            }
        }
        private static void HandleException(ExceptionContext context)
        {
            Exception exception = context.Exception;

            string message = exception.Message;

#if DEBUG
            ApiExceptionResponse response = new ApiExceptionResponse(message, exception.StackTrace);
#else
            ApiExceptionResponse response = new ApiExceptionResponse(message);
#endif
            context.Result = new JsonResult(response)
            {
                StatusCode = (int)HttpStatusCode.InternalServerError
            };
        }
Пример #5
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await next(context);
            }
            catch (ApiException e)
            {
                log.LogWarning(e, $"Handled API exception: {e.Message}");
                context.Response.ContentType = "application/json";

                if (e is NotFoundException)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                }
                else if (e is BadRequestException)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                }
                else if (e is UnauthorizedException)
                {
                    //Note: 401 Unauthorized actually means "Not Authenticated", so we want 403 "Forbidden" instead
                    context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }

                ApiExceptionResponse errObj = new ApiExceptionResponse()
                {
                    Type    = "ApiException",
                    Message = e.Message
                };

                await context.Response.WriteAsync(JsonConvert.SerializeObject(errObj, new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }));
            }
            catch (Exception)
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                throw;
            }
        }
Пример #6
0
 public ApiException(ApiExceptionResponse error, HttpStatusCode statusCode) : base(error.ResponseMessage)
 {
     this.Error      = error;
     this.StatusCode = statusCode;
 }
Пример #7
0
 public ApiException(ApiExceptionResponse error) : base(error.ResponseMessage)
 {
     this.Error = error;
 }