Esempio n. 1
0
        /// <summary>
        /// Handler all exceptions in controlers and return to client aproriated http status code
        /// </summary>
        /// <param name="context">Http context for status code</param>
        /// <param name="exception">Exception which parse</param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            HttpStatusCode code;

            switch (exception)
            {
            case Exceptions.FieldValueTimeInvalidException _:
            case Exceptions.RelatedEntryNotFoundException _:
                code = HttpStatusCode.BadRequest;
                break;

            case Exceptions.CurrentEntryNotFoundException _:
            case Exceptions.EntryNotFoundException _:
            case NullReferenceException _:
                code = HttpStatusCode.NotFound;
                break;

            case Exceptions.OperationRestrictedRelationException _:
                code = HttpStatusCode.Forbidden;
                break;

            case Exceptions.OperationRestrictedException _:
            case UnauthorizedAccessException _:
                code = HttpStatusCode.Unauthorized;
                break;

            case Exceptions.OperationFailedException _:
            case Exceptions.UserException _:
                code = HttpStatusCode.BadRequest;
                break;

            case SecurityTokenException detailed:
                code = HttpStatusCode.Unauthorized;
                break;

            default:
                logger.LogError($"Catched internal exception. Message: {exception.Message}. Stacktrace: {exception.StackTrace}");
                code = HttpStatusCode.InternalServerError;
                break;
            }

            object data;

            if (IsDevelopment)
            {
                data = new DevelopmentExceptionInfo(exception.Message, exception.StackTrace);
                logger.LogDebug($"Catched {exception.GetType()} exception. Message: {exception.Message}. Stacktrace: {exception.StackTrace}");
            }
            else
            {
                data = new ExceptionInfo(exception.Message);
                logger.LogInformation($"Catched exception. Message: {exception.Message}.");
            }

            var result = JsonConvert.SerializeObject(data, Formatting.Indented);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            return(context.Response.WriteAsync(result));
        }
        /// <summary>
        /// Handler all exceptions in controlers and return to client aproriated http status code
        /// </summary>
        /// <param name="context">Http context for status code</param>
        /// <param name="exception">Exception which parse</param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            HttpStatusCode code;

            switch (exception)
            {
            case Exceptions.CurrentEntryNotFoundException _:
                code = HttpStatusCode.NotFound;
                break;

            case UnauthorizedAccessException _:
                code = HttpStatusCode.Unauthorized;
                break;

            case DataException _:
                code = HttpStatusCode.InternalServerError;
                break;

            default:
                logger.LogError($"Catched internal exception. Message: {exception.Message}. Stacktrace: {exception.StackTrace}");
                code = HttpStatusCode.InternalServerError;
                break;
            }

            object data;

            if (IsDevelopment)
            {
                data = new DevelopmentExceptionInfo(exception.Message, exception.StackTrace);
                logger.LogDebug($"Catched {exception.GetType()} exception. Message: {exception.Message}. Stacktrace: {exception.StackTrace}");
            }
            else
            {
                data = new ExceptionInfo(exception.Message);
                logger.LogInformation($"Catched exception. Message: {exception.Message}.");
            }

            var result = JsonConvert.SerializeObject(data, Formatting.Indented);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            return(context.Response.WriteAsync(result));
        }