private IActionResult HandleError(IExceptionHandlerPathFeature feature) { var ex = feature.Error; AppResult result; if (ex is AppValidationException validEx) { result = validEx.Result; StatusCodeStyle = Resources.BadRequestMessageStyle; MessageTitle = Localizer[Resources.BadRequestMessageTitle]; Code = (int)HttpStatusCode.BadRequest; } else if (ex is AuthorizationException authEx) { if (authEx.IsUnauthorized) { result = AppResult.Unauthorized(_resultLocalizer); StatusCodeStyle = Resources.UnauthorizedMessageStyle; MessageTitle = Localizer[Resources.UnauthorizedMessageTitle]; Code = (int)HttpStatusCode.Unauthorized; } else { result = AppResult.AccessDenied(_resultLocalizer); StatusCodeStyle = Resources.AccessDeniedMessageStyle; MessageTitle = Localizer[Resources.AccessDeniedMessageTitle]; Code = (int)HttpStatusCode.Forbidden; } } else if (ex is AppException appEx) { result = appEx.Result; StatusCodeStyle = Resources.ErrorMessageStyle; MessageTitle = Localizer[Resources.ErrorMessageTitle]; Code = (int)HttpStatusCode.InternalServerError; } else { if (_env.IsDevelopment()) { result = AppResult.Error(_resultLocalizer, data: ex, mess: ex.Message); } else { result = AppResult.Error(_resultLocalizer, Localizer[Resources.ErrorMessage]); } StatusCodeStyle = Resources.ErrorMessageStyle; MessageTitle = Localizer[Resources.ErrorMessageTitle]; Code = (int)HttpStatusCode.InternalServerError; } Message = result.Message; RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; OriginalUrl = feature.Path; return(Page()); }
public async Task <AppResult <User> > Authenticate(User user) { var result = await Task.Run(() => _validator.ValidateObject(user)); if (!result.IsValid) { return(AppResult <User> .Error(result)); } return(AppResult <User> .Succeed(user)); }
public async Task <IActionResult> HandleException() { var context = HttpContext.Features.Get <IExceptionHandlerFeature>(); var ex = context.Error; if (ex == null) { return(BadRequest()); } AppResult response = null; if (ex is AppValidationException validEx) { return(BadRequest(validEx.Result)); } else if (ex is DbUpdateException dbEx) { response = ParseDbUpdateExceptionResult(dbEx); } else if (ex is AuthorizationException authEx) { if (authEx.IsForbidden) { return(Forbid()); } return(Unauthorized()); } else if (ex is AppException appEx) { response = appEx.Result; } if (response == null) { if (_env.IsDevelopment()) { response = AppResult.Error(resultLocalizer, data: ex); } else { response = AppResult.Error(resultLocalizer); } await LogErrorRequestAsync(ex); } return(Error(response)); }
public async Task <IActionResult> HandleException() { var context = HttpContext.Features.Get <IExceptionHandlerFeature>(); var ex = context.Error; if (ex == null) { return(BadRequest()); } AppResult response; if (ex is AppValidationException) { return(BadRequest((ex as AppValidationException).Result)); } else if (ex is AuthorizationException) { var authEx = ex as AuthorizationException; if (authEx.IsForbidden) { return(Forbid()); } return(Unauthorized()); } else if (ex is AppException) { response = (ex as AppException).Result; } else { if (_env.IsDevelopment()) { response = AppResult.Error(data: ex); } else { response = AppResult.Error(); } } await LogErrorRequestAsync(ex); return(Error(response)); }
public IActionResult HandleException() { var context = HttpContext.Features.Get <IExceptionHandlerFeature>(); if (context.Error == null) { return(BadRequest()); } var e = context.Error; _logger.Error(e); #if DEBUG return(Error(AppResult.Error(data: e))); #else return(Error(AppResult.Error())); #endif }
protected async Task <AppResult <List <GameCard> > > GetTargetsByIds(DtoEffectTargets effect, TargetingEffectParameters parameters, Game game) { var targetsCount = Convert.ToInt32(parameters.TargetParameter); if (targetsCount != effect.TargetIds.Count) { return(AppResult <List <GameCard> > .Error(AppActionResultType.RuleError, $"You must choose exactly {targetsCount} targets to destroy.")); } var targets = await dbContext.GameCards.IncludeAll() .Where(_ => effect.TargetIds.Contains(_.Id)) .ToListAsync(); if (targets.Count != effect.TargetIds.Count) { return(VoidAppResult.Error(ErrorPreset.InvalidTargets).GetErrorAppResult <List <GameCard> >()); } return(AppResult <List <GameCard> > .Success(targets)); }
public async Task <AppResult <LoginViewModel> > Login(LoginViewModel login) { AppResult <LoginViewModel> result; var user = _mapper.Map <LoginViewModel, User>(login); CreateHash(user); var retorno = await _accountService.Authenticate(user); if (!retorno.ValidationResult.IsValid) { result = AppResult <LoginViewModel> .Error(retorno.ValidationResult); } else { result = AppResult <LoginViewModel> .Succeed(login); } return(result); }
public IActionResult HandleException() { dynamic context = HttpContext.Features.Get <IExceptionHandlerFeature>(); if (context == null || context.Error == null) { return(LocalRedirect(Constants.Routing.DASHBOARD)); } var exception = context.Error as Exception; if (exception != null) { _logger.Error(exception); } var isApiRequest = context.Path.StartsWith("/api"); if (isApiRequest) { #if DEBUG return(Error(AppResult.Error(data: exception))); #else return(Error(AppResult.Error())); #endif } if (exception == null) { return(LocalRedirect(Constants.Routing.DASHBOARD)); } HttpContext.Items["model"] = this; SetPageInfo(); #if !RELEASE Message = exception.Message; #else Message = "Something's wrong. Please contact admin for more information."; #endif RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; return(this.ErrorView()); }
public async Task <AppResult> CreateAgent(CreateAgentVM model) { try { var phoneNumberUtil = PhoneNumberUtil.GetInstance(); PhoneNumber phoneNumber = null; if (model.MobileNumber.StartsWith('+')) { if (model.MobileNumber.Length < 8) { AppResult.Error(ErrorHandler.GeneralErrorMessage, ResponseMessages.InvalidPhoneNumber); } phoneNumber = phoneNumberUtil.Parse(model.MobileNumber, null); } if (model.MobileNumber.Length < 8) { AppResult.Error(ErrorHandler.GeneralErrorMessage, ResponseMessages.InvalidPhoneNumber); } phoneNumber = phoneNumberUtil.Parse(model.MobileNumber, model.CountryCode); model.MobileNumber = $"{phoneNumber.CountryCode}{phoneNumber.NationalNumber}"; var resp = await _agentRepository.CreateAgentInDb(model); return(ErrorHandler.AgentCreateError.Equals(resp) ? AppResult.Error(resp) : AppResult.Success("Successfully created agent", resp)); } catch (Exception e) { _logger.LogError(e, e.ToString()); return(AppResult.Error("An error occured", e.ToString())); } }
public async Task <AppResult <DtoBoard> > GetBoard(Guid gameId, CancellationToken cancellationToken = default(CancellationToken)) { var game = await this.dbContext.Games.IncludeUserParticipations().IncludeEntities().SingleOrDefaultAsync(_ => _.Id == gameId, cancellationToken); if (game == null) { return(VoidAppResult.Error(ErrorPreset.OnLoadingData).GetErrorAppResult <DtoBoard>()); } if (game.UserParticipations.All(_ => _.Mail != this.loginManager.LoggedInUser.Mail)) { return(AppResult <DtoBoard> .Error(AppActionResultType.UserError, "This user is not part of this game.")); } var opponentParticipation = game.UserParticipations.Single(_ => _.Mail != this.loginManager.LoggedInUser.Mail); var opponent = opponentParticipation.User; var opponentEntity = game.Entities.Select(_ => _ as Player).Single(_ => _ != null && _.User.Mail == opponent.Mail); var loggedInParticipation = game.UserParticipations.Single(_ => _.Mail == this.loginManager.LoggedInUser.Mail); var loggedInEntity = game.Entities.Select(_ => _ as Player).Single(_ => _ != null && _.User.Mail == this.loginManager.LoggedInUser.Mail); var currentTurnPlayerResult = await this.GetCurrentTurnPlayer(game, cancellationToken); if (currentTurnPlayerResult.IsErrorResult) { return(AppResult <DtoBoard> .Error(AppActionResultType.ServerError, "There was an error while loading game info.")); } var gameCards = await this.dbContext.GameCards.IncludeAll().Where(_ => _.Game.Id == gameId).ToListAsync(); var loggedInHandCardsResult = await this.GetDtoCardsFromGameCards(gameCards .Where(_ => _.User.Mail == this.loginManager.LoggedInUser.Mail && _.CardLocation == CardLocation.Hand) .ToList(), gameId); if (loggedInHandCardsResult.IsErrorResult) { return(loggedInHandCardsResult.GetErrorAppResult <DtoBoard>()); } var loggedInBoardCardsResult = await this.GetDtoCardsFromGameCards(gameCards .Where(_ => _.User.Mail == this.loginManager.LoggedInUser.Mail && _.CardLocation == CardLocation.Board) .ToList(), gameId); if (loggedInBoardCardsResult.IsErrorResult) { return(loggedInBoardCardsResult.GetErrorAppResult <DtoBoard>()); } var opponentBoardCardsResult = await this.GetDtoCardsFromGameCards(gameCards .Where(_ => _.User.Mail == opponent.Mail && _.CardLocation == CardLocation.Board) .ToList(), gameId); if (opponentBoardCardsResult.IsErrorResult) { return(opponentBoardCardsResult.GetErrorAppResult <DtoBoard>()); } return(AppResult <DtoBoard> .Success(new DtoBoard { Id = game.Id, CurrentTurnPlayer = currentTurnPlayerResult.SuccessReturnValue.Mail, PlayerHandCards = loggedInHandCardsResult.SuccessReturnValue, OpponentHandCardsCount = gameCards.Where(_ => _.User.Mail == opponent.Mail && _.CardLocation == CardLocation.Hand).Count(), Player = new DtoBoardPlayer { Health = loggedInEntity.Health, Mail = this.loginManager.LoggedInUser.Mail, BoardCards = loggedInBoardCardsResult.SuccessReturnValue, ClassType = loggedInParticipation.ClassType }, Opponent = new DtoBoardPlayer { Health = opponentEntity.Health, Mail = opponent.Mail, BoardCards = opponentBoardCardsResult.SuccessReturnValue, ClassType = opponentParticipation.ClassType } })); }