Example #1
0
        public static void ExpectUCenterError(UCenterErrorCode expectedErrorCode, Action action)
        {
            try
            {
                action();
                Assert.Fail($"Expected {typeof(UCenterException)} Exception, but no expection happened");
            }
            catch (UCenterException ex)
            {
                if (ex.ErrorCode != expectedErrorCode)
                {
                    Assert.Fail($"Expect ErrorCode: {expectedErrorCode} but actual: {ex.ErrorCode}");
                }
            }
            catch (AggregateException ex)
            {
                UCenterException actual = null;
                if (ex.InnerExceptions.Count() == 1)
                {
                    actual = ex.InnerExceptions.Single() as UCenterException;
                }

                if (actual == null)
                {
                    throw;
                }

                if (actual.ErrorCode != expectedErrorCode)
                {
                    Assert.Fail($"Expect ErrorCode: {expectedErrorCode} but actual: {actual.ErrorCode}");
                }
            }
        }
Example #2
0
 public static string GetErrorMessage(UCenterErrorCode errorCode)
 {
     if (errorMessages.ContainsKey(errorCode))
     {
         return(errorMessages[errorCode]);
     }
     return(GeneralErrorMessage);
 }
 public static string GetErrorMessage(UCenterErrorCode errorCode)
 {
     if (errorMessages.ContainsKey(errorCode))
     {
         return errorMessages[errorCode];
     }
     return GeneralErrorMessage;
 }
Example #4
0
        private HttpResponseMessage CreateErrorResponseMessage(UCenterErrorCode errorCode, string errorMessage)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            var content  = new UCenterResponse <UCenterError>
            {
                Status = UCenterResponseStatus.Error,
                Error  = new UCenterError {
                    ErrorCode = errorCode, Message = errorMessage
                }
            };

            response.Content = new ObjectContent <UCenterResponse <UCenterError> >(content, new JsonMediaTypeFormatter());

            return(response);
        }
        private async Task RecordLogin(AccountEntity account, UCenterErrorCode code, string comments = null)
        {
            var record = new LoginRecordEntity
            {
                AccountName = account.AccountName,
                AccountId   = account.Id,
                Code        = code,
                LoginTime   = DateTime.UtcNow,
                UserAgent   = Request.Headers.UserAgent.ToString(),
                ClientIp    = this.GetClientIp(Request),
                Comments    = comments
            };

            await this.DatabaseContext.Bucket.InsertSlimAsync(record, false);
        }
Example #6
0
        public static async Task ExpectUCenterErrorAsync(UCenterErrorCode expectedErrorCode, Func <Task> func)
        {
            try
            {
                await func();

                Assert.Fail("Expected {typeof(UCenterException)}, but no expection happened");
            }
            catch (UCenterException ex)
            {
                if (ex.ErrorCode != expectedErrorCode)
                {
                    Assert.Fail($"Expect ErrorCode: {expectedErrorCode} but actual: {ex.ErrorCode}");
                }
            }
        }
Example #7
0
        private async Task TraceAccountErrorAsync(
            string AccountName,
            UCenterErrorCode code,
            string message          = null,
            CancellationToken token = default(CancellationToken))
        {
            var clientIp = HttpContext.GetClientIpAddress();

            var accountErrorEvent = new AccountErrorEventEntity()
            {
                Id = Guid.NewGuid().ToString(),
                //AccountId = account.Id,
                AccountName = AccountName,
                Code        = code,
                ClientIp    = clientIp,
                LoginArea   = string.Empty,
                Message     = message
            };

            await this.eventTrace.TraceEvent <AccountErrorEventEntity>(accountErrorEvent, token);
        }
Example #8
0
        private async Task RecordLogin(
            AccountEntity account,
            UCenterErrorCode code,
            string comments         = null,
            CancellationToken token = default(CancellationToken))
        {
            var clientIp       = IPHelper.GetClientIpAddress(Request);
            var ipInfoResponse = await IPHelper.GetIPInfoAsync(clientIp, CancellationToken.None);

            string area;

            if (ipInfoResponse != null && ipInfoResponse.Code == IPInfoResponseCode.Success)
            {
                area = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}-{1}",
                    ipInfoResponse.Content.Country,
                    ipInfoResponse.Content.City ?? ipInfoResponse.Content.County);
            }
            else
            {
                area = string.Empty;
            }

            var record = new LoginRecordEntity
            {
                Id          = Guid.NewGuid().ToString(),
                AccountName = account.AccountName,
                AccountId   = account.Id,
                Code        = code,
                LoginTime   = DateTime.UtcNow,
                UserAgent   = Request.Headers.UserAgent.ToString(),
                ClientIp    = clientIp,
                LoginArea   = area,
                Comments    = comments
            };

            await this.Database.LoginRecords.InsertAsync(record, token);
        }
 public UCenterException(UCenterErrorCode errorCode, string message, Exception innerException = null)
     : base(message, innerException)
 {
     this.ErrorCode = errorCode;
 }
 public UCenterException(UCenterErrorCode errorCode, Exception innerException = null)
     : this(errorCode, UCenterResourceManager.GetErrorMessage(errorCode), innerException)
 {
 }
 public UCenterException(UCenterErrorCode errorCode, string message, Exception innerException = null)
     : base(message, innerException)
 {
     this.ErrorCode = errorCode;
 }
 public UCenterException(UCenterErrorCode errorCode, Exception innerException = null)
     : this(errorCode, UCenterResourceManager.GetErrorMessage(errorCode), innerException)
 {
 }