Example #1
0
        //public async Task DoStandartSomethingWithOutErrorResponse(Func<Task> action, HttpResponse response, ILogger logger)
        //{

        //}

        public virtual async Task DoStandartSomething(Func <Task> action, HttpResponse response, ILogger logger)
        {
            try
            {
                await action();

                return;
            }
            catch (SomeCustomException e)
            {
                ErrorFromCustomException(e);
            }
            catch (StopException)
            {
            }
            catch (NotAuthException)
            {
                var error = _errorContainer.TryGetError(ErrorConsts.NotAuthorized);
                if (error != null)
                {
                    _errorService.AddError(error);
                }

                await WriteResponseAsync(response, new ErrorObjectReturnFactory().GetObjectReturn(_errorService.GetErrorsObject()), 401);//TODO 401

                return;
            }
            catch (Exception e)
            {
                _errorService.AddError(_errorContainer.TryGetError(ErrorConsts.SomeError));
                logger?.LogError(e, ErrorConsts.SomeError);
            }

            await WriteResponseAsync(response, new ErrorObjectReturnFactory().GetObjectReturn(_errorService.GetErrorsObject()));
        }
Example #2
0
        public async Task Login([FromForm] LoginModelApi loginModel)
        {
            await _apiHealper.DoStandartSomething(
                async() =>
            {
                //throw new Exception("test exc");

                //throw new System.Exception();
                if (_apiHealper.ErrorsFromModelState(ModelState))
                {
                    await _apiHealper.WriteResponseAsync(Response, _errRetrunFactory.GetObjectReturn((_errorService.GetErrorsObject())));
                    return;
                }

                var tokens = await _authSrvice.Login(loginModel.GetModel());
                if (tokens == null)
                {
                    throw new SomeCustomException(ErrorConsts.SomeError);
                }

                _apiHealper.SetUserTokens(Response, tokens);
                await _apiHealper.WriteResponseAsync(Response, _tokensReturnFactory.GetObjectReturn(tokens));
            }, Response, _logger);
        }