public static string GetMessage(this ApplicationErrorCodes enumData) { switch (enumData) { case ApplicationErrorCodes.EmptyTransactionId: return("Specify Valid Transaction Id"); default: throw new ArgumentOutOfRangeException(nameof(enumData), enumData, null); } }
public override void OnException(ExceptionContext context) { var exceptionType = context.Exception.GetType(); string errorCode, message, field; HttpStatusCode statusCode; if (exceptionType == typeof(UnauthorizedAccessException)) { message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.Unauthorized); errorCode = ApplicationErrorCodes.Unauthorized; field = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.Unauthorized); statusCode = HttpStatusCode.Unauthorized; } else if (exceptionType == typeof(DivideByZeroException)) { message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InternalSeverError); errorCode = ApplicationErrorCodes.InternalSeverError; field = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InternalSeverError); statusCode = HttpStatusCode.InternalServerError; } else if (exceptionType == typeof(MongoDB.Driver.MongoConfigurationException)) { message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.DatabaseError); errorCode = ApplicationErrorCodes.DatabaseError; field = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.DatabaseError); statusCode = HttpStatusCode.BadGateway; } else { message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.NotFound); errorCode = ApplicationErrorCodes.NotFound; field = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.NotFound); statusCode = HttpStatusCode.NotFound; } var error = new Common.Exceptions.ApplicationException { ErrorCode = errorCode, Data = new ErrorData(field, message) }; context.HttpContext.Response.StatusCode = (int)statusCode; context.Result = new JsonResult(error); }
public void CompartmentStatusInvalidLockerId() { var(_, errorResult) = LockerManagementValidator.PayloadValidator(lockerConfiguration, true, jwtSecret, token, PayloadTypes.CompartmentStatus, "Hasan", transactionId, validCompartmentIds, null); Assert.Equal(ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId), errorResult.Message); }
public static (int, ApplicationException) PayloadValidator(AppSettings lockerConfiguration, bool jwtToken, string jwtSecret, string token, string payloadType, string lockerId, string transactionId, string[] compartmentIds, string captureType) { int statusCode = StatusCode.Status200OK; ApplicationException result = null; try { #region Json Web Token if (jwtToken) { if (string.IsNullOrEmpty(token)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidToken, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidToken) }; return(statusCode, result); } var(isVerified, transactionid) = JwtTokenHandler.VerifyJwtSecurityToken(jwtSecret, token); if ((!isVerified) || string.IsNullOrEmpty(transactionid)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidToken, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidToken) }; return(statusCode, result); } } #endregion #region Payload Validation switch (payloadType) { case PayloadTypes.OpenCompartment: if (string.IsNullOrEmpty(transactionId)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyTransactionId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyTransactionId) }; Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (string.IsNullOrEmpty(lockerId)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId) }; Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (compartmentIds == null || compartmentIds.Length == 0) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyCompartmentId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyCompartmentId) }; Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (compartmentIds.Length > 0 && !compartmentIds.Contains("All")) { if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId) }; Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } if (lockerConfiguration != null && lockerConfiguration.Locker.Compartments.Count() > 0) { foreach (string compartmentId in compartmentIds) { var flag = lockerConfiguration.Locker.Compartments.Any(com => com.CompartmentId == compartmentId); if (flag) { continue; } else { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidCompartmentId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCompartmentId) }; Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } } } } Log.Warning("[HCM][Locker Management Validator][Open Compartment]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); case PayloadTypes.CompartmentStatus: if (string.IsNullOrEmpty(lockerId)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId) }; Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId) }; Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } if (lockerConfiguration != null && lockerConfiguration.Locker.Compartments.Count() > 0 && !compartmentIds.Contains("All")) { foreach (string compartmentId in compartmentIds) { var flag = lockerConfiguration.Locker.Compartments.Any(compartment => compartment.CompartmentId == compartmentId); if (flag) { continue; } else { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidCompartmentId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCompartmentId) }; Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } } } Log.Warning("[HCM][Locker Management Validator][Compartment Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); case PayloadTypes.LockerStatus: if (string.IsNullOrEmpty(lockerId)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId) }; Log.Warning("[HCM][Locker Management Validator][Locker Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId) }; Log.Warning("[HCM][Locker Management Validator][Locker Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } Log.Warning("[HCM][Locker Management Validator][Locker Status]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); case PayloadTypes.CaptureImage: if (string.IsNullOrEmpty(lockerId)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId) }; Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (lockerConfiguration != null && lockerConfiguration.Locker.LockerId != lockerId) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidLockerId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidLockerId) }; Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (string.IsNullOrEmpty(captureType)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyCaptureType, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyCaptureType) }; Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (!(captureType == CaptureType.Photo || captureType == CaptureType.Screen)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.InvalidCaptureType, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCaptureType) }; Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } else if (string.IsNullOrEmpty(transactionId)) { statusCode = StatusCode.Status422UnprocessableEntity; result = new ApplicationException { Code = ApplicationErrorCodes.EmptyTransactionId, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyTransactionId) }; Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } Log.Warning("[HCM][Locker Management Validator][Capture Image]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); return(statusCode, result); } #endregion } catch (Exception) { statusCode = StatusCode.Status502BadGateway; result = new ApplicationException { Code = ApplicationErrorCodes.UnknownError, Message = ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.UnknownError) }; Log.Error("[HCM][Locker Management Validator]" + "[Status Code : " + statusCode + "]" + "[Result : " + result + "]"); } return(statusCode, result); }
public void OpenCompartmentEmptyCompartmentIds() { var(_, errorResult) = LockerManagementValidator.PayloadValidator(lockerConfiguration, true, jwtSecret, token, PayloadTypes.OpenCompartment, lockerId, transactionId, null, null); Assert.Equal(ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyCompartmentId), errorResult.Message); }
public void CaptureImageEmptyTransactionId() { var(_, errorResult) = LockerManagementValidator.PayloadValidator(lockerConfiguration, true, jwtSecret, token, PayloadTypes.CaptureImage, lockerId, string.Empty, null, captureType); Assert.Equal(ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyTransactionId), errorResult.Message); }
public void CaptureImageInvalidCaptureType() { var(_, errorResult) = LockerManagementValidator.PayloadValidator(lockerConfiguration, true, jwtSecret, token, PayloadTypes.CaptureImage, lockerId, transactionId, null, "Hasan"); Assert.Equal(ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.InvalidCaptureType), errorResult.Message); }
public void LockerStatusEmptyLockerId() { var(_, errorResult) = LockerManagementValidator.PayloadValidator(lockerConfiguration, true, jwtSecret, token, PayloadTypes.LockerStatus, string.Empty, transactionId, null, null); Assert.Equal(ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.EmptyLockerId), errorResult.Message); }
/// <summary> /// Gets the compartment status with Json web token credentials. /// Json web token credentials contains enable or disable, if enable then need to provide jwt secret and token. /// </summary> /// <returns> /// List of compartment status with object detection and LED status based requested compartment id's. /// </returns> public CompartmentStatusDto CompartmentStatus(Compartment model) { var compartmentStatusDto = new CompartmentStatusDto(); Log.Information("[HCM][Compartment Status][Req]" + "[" + JsonSerializer.Serialize(model) + "]"); try { if (!PortsHealthCheck.IsLockPortAvailable && !PortsHealthCheck.IsDetectionPortAvailable) { return(CompartmentStatusMapper.ToError(new CompartmentStatusDto { StatusCode = StatusCode.Status503ServiceUnavailable, Error = new Common.Exceptions.ApplicationException(ApplicationErrorCodes.BrokenComPort, ApplicationErrorCodes.GetMessage(ApplicationErrorCodes.BrokenComPort)) })); } var(statusCode, errorResult) = LockerManagementValidator.PayloadValidator(LockerConfiguration, model.JwtCredentials.IsEnabled, model.JwtCredentials.Secret, model.JwtCredentials.Token, PayloadTypes.CompartmentStatus, model.LockerId, model.TransactionId, model.CompartmentIds, null); if (statusCode != StatusCode.Status200OK) { return(CompartmentStatusMapper.ToError(new CompartmentStatusDto { StatusCode = statusCode, Error = errorResult })); } var result = CompartmentManager.CompartmentStatus(model, LockerConfiguration); compartmentStatusDto = CompartmentStatusMapper.ToObject(result); Log.Information("[HCM][Compartment Status][Res]" + "[" + JsonSerializer.Serialize(result) + "]"); } catch (Exception ex) { Log.Error("[HCM][Compartment Status]" + "[" + ex + "]"); return(CompartmentStatusMapper.ToError(new CompartmentStatusDto { StatusCode = StatusCode.Status500InternalServerError, Error = new Common.Exceptions.ApplicationException(ApplicationErrorCodes.InternalServerError, ex.Message) })); } return(compartmentStatusDto); }