Beispiel #1
0
        public override async Task OnExceptionAsync(ExceptionContext context)
        {
            var errorMessage = context.Exception.Message;

            var(statusCode, errorCode) = (HttpStatusCode.InternalServerError, ErrorCodes.ServerError);

            (statusCode, errorCode) = context.Exception switch
            {
                ServerException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                CrudException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                ResetPasswordException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                TokenException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                ProfileUpdateException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                ChangePasswordException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                UploadFileException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                DeleteFileException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                AdminActionException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                HubConnectionException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),

                AuthException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),
                InvalidCredentialsException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),
                AccountNotConfirmedException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),

                EntityNotFoundException _ => (HttpStatusCode.NotFound, GetErrorCode(context.Exception)),

                ServiceException _ => (HttpStatusCode.ServiceUnavailable, GetErrorCode(context.Exception)),

                NoPermissionsException _ => (HttpStatusCode.Forbidden, GetErrorCode(context.Exception)),
                AccountBlockedException _ => (HttpStatusCode.Forbidden, GetErrorCode(context.Exception)),

                DuplicateException _ => (HttpStatusCode.Conflict, GetErrorCode(context.Exception)),

                OldPasswordInvalidException _ => (HttpStatusCode.BadRequest, GetErrorCode(context.Exception)),

                _ => (HttpStatusCode.InternalServerError, ErrorCodes.ServerError)
            };

            var jsonResponse = (new BaseResponse(Error.Build(errorCode, errorMessage, statusCode: statusCode))).ToJSON();

            context.HttpContext.Response.ContentType   = "application/json";
            context.HttpContext.Response.StatusCode    = (int)statusCode;
            context.HttpContext.Response.ContentLength = Encoding.UTF8.GetBytes(jsonResponse).Length;

            context.HttpContext.Response.AddApplicationError(errorMessage);

            await context.HttpContext.Response.WriteAsync(jsonResponse);

            var logger = context.HttpContext.RequestServices.GetService <INLogger>();
            logger.Error(errorMessage, context.Exception);

            await base.OnExceptionAsync(context);
        }
        public override async Task OnExceptionAsync(ExceptionContext context)
        {
            var errorMessage = context.Exception.Message;

            var(statusCode, errorCode) = (HttpStatusCode.InternalServerError, ErrorCodes.ServerError);

            (statusCode, errorCode) = context.Exception switch
            {
                ServerException _ or CrudException _ or DatabaseException _ or ResetPasswordException _ or
                TokenExpiredException _ or CannotGenerateTokenException _ or ProfileUpdateException _ or
                ChangePasswordException _ or UploadFileException _ or DeleteFileException _ or
                PremiumOperationException _ => (
                    HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),

                EntityNotFoundException _ => (HttpStatusCode.NotFound, GetErrorCode(context.Exception)),

                AuthException _ or InvalidCredentialsException _ or
                AccountNotConfirmedException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),

                ServiceException _ or CaptchaException _ => (HttpStatusCode.ServiceUnavailable,
                                                             GetErrorCode(context.Exception)),

                NoPermissionsException _ or BlockException _ => (HttpStatusCode.Forbidden,
                                                                 GetErrorCode(context.Exception)),

                DuplicateException _ => (HttpStatusCode.Conflict, GetErrorCode(context.Exception)),

                PaymentException _ => (HttpStatusCode.PaymentRequired, GetErrorCode(context.Exception)),

                OldPasswordInvalidException _ => (HttpStatusCode.BadRequest, GetErrorCode(context.Exception)),

                _ => (HttpStatusCode.InternalServerError, ErrorCodes.ServerError)
            };

            var jsonResponse =
                (new BaseResponse(Error.Build(errorCode, errorMessage, statusCode: statusCode))).ToJSON();

            context.HttpContext.Response.ContentType   = "application/json";
            context.HttpContext.Response.StatusCode    = (int)statusCode;
            context.HttpContext.Response.ContentLength = Encoding.UTF8.GetBytes(jsonResponse).Length;

            context.HttpContext.Response.AddApplicationError(errorMessage);

            await context.HttpContext.Response.WriteAsync(jsonResponse);

            var databaseRestorer = context.HttpContext.RequestServices.GetRequiredService <IDatabaseRestorer>();
            databaseRestorer.EnqueueFromConnectionDatabaseRestorePoints(context.HttpContext.GetConnectionId());

            Log.Error($"{context.Exception.GetType().Name}: {errorMessage} [{errorCode}] [HTTP {(int) statusCode}]");

            await base.OnExceptionAsync(context);
        }