/// <summary> /// Method that is executed when an uncaught exception has been thrown from a controller. This method provides /// a centralized place where we can handle and log these exceptions /// </summary> /// <param name="context"></param> public override void OnException(ExceptionContext context) { Exception ex = context.Exception; // For convenience string logMessage = $"{ex.GetType().Name} occurred in action {context.ActionDescriptor.DisplayName} - {ex.Message}"; if (ex is InvalidDataException) { _logger.LogError(new EventId(400), ex, logMessage); var message = new ApiMessageModel() { Message = ex.Message }; context.Result = new BadRequestObjectResult(message); } else if (ex is SecurityException) { _logger.LogError(new EventId(403), ex, logMessage); var message = new ApiMessageModel() { Message = "Access to this resource is forbidden" }; context.Result = new ForbiddenObjectResult(message); } else if (ex is ResourceAlreadyExistsException) { _logger.LogWarning(new EventId(409), ex, logMessage); var message = new ApiMessageModel() { Message = ex.Message }; context.Result = new ConflictObjectResult(message); } else if (ex is ObjectNotFoundException) { _logger.LogWarning(new EventId(404), ex, logMessage); var message = new ApiMessageModel() { Message = ex.Message }; context.Result = new NotFoundObjectResult(message); } else { _logger.LogError(new EventId(500), ex, logMessage); var message = new ApiMessageModel() { Message = "An error has occurred on the server. The error has been logged so it can be investigated by our support teams" }; context.Result = new InternalServerErrorObjectResult(message); } context.ExceptionHandled = true; }
public async Task <ActionResult <ChatMessage> > PostMessage([FromBody] ApiMessageModel message) { /* * BaseClient logicBaseClient = await repository.GetBasicClientInformationByID(message.messageSenderClientID.GetValueOrDefault() != Guid.Empty ? message.messageSenderClientID.GetValueOrDefault() : message.messageRecieverClientID.GetValueOrDefault()); * BaseClient checkerClient = await repository.GetBasicClientInformationByEmail(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value); * List<RelationshipMeta> checkerAssignmentInfo = new List<RelationshipMeta>(); * // If the checker is an admin, there is no need to get the checker's info container as that is * // only used to authorize the caller * if (!checkerClient.isAdmin) * { * checkerAssignmentInfo = await repository.getClientAssignmentsInfoByIDAsync(checkerClient.clientID); * } * * * // If the logic client and checker client have the same Id * if (logicBaseClient.clientID == checkerClient.clientID) * { * // If the message xref is not null, check to make sure the checkerAssignmentInfo has an assignment that matches (Or pass true if null, which implies posting to a general chat * // Or bypass if they are an admin * if ((message.clientRelationXrefID != null ? checkerAssignmentInfo.Any(a => a.clientRelationXrefID == message.clientRelationXrefID) : true) || checkerClient.isAdmin) * { * TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); * * Logic.Objects.Message logicMessage = new Logic.Objects.Message() * { * chatMessageID = Guid.NewGuid(), * recieverClient = new ClientChatMeta() * { * clientId = message.messageRecieverClientID * }, * senderClient = new ClientChatMeta() * { * clientId = message.messageSenderClientID * }, * clientRelationXrefID = message.clientRelationXrefID, * messageContent = message.messageContent, * dateTimeSent = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, easternZone), * isMessageRead = false, * fromAdmin = message.fromAdmin * }; * if (logicMessage.recieverClient.clientId == null && logicMessage.senderClient.clientId == null) * { * return StatusCode(StatusCodes.Status400BadRequest); * } * else * { * try * { * await repository.CreateMessage(logicMessage); * await repository.SaveAsync(); * * Logic.Objects.Message postedLogicMessage = await repository.GetMessageByIDAsync(logicMessage.chatMessageID); * await yuleLogger.logCreatedNewMessage(checkerClient, postedLogicMessage.senderClient, postedLogicMessage.recieverClient); * * // If this message has an eventTypeID * if (message.eventTypeID.HasValue) * { * // If the message is from an admin, get the event for the notification, and send the email * if (message.fromAdmin) * { * Logic.Objects.Event logicEvent = await repository.GetEventByIDAsync(message.eventTypeID.Value); * await mailbag.sendChatNotificationEmail(await repository.GetClientByIDAsync(logicMessage.recieverClient.clientId.Value), logicEvent); * } * } * // Else if it doesnt have an event (It is a general message) * else * { * // If it's from an admin, make a new event object, and send the client a notification * if (message.fromAdmin) * { * Logic.Objects.Event logicEvent = new Logic.Objects.Event(); * await mailbag.sendChatNotificationEmail(await repository.GetClientByIDAsync(logicMessage.recieverClient.clientId.Value), new Logic.Objects.Event()); * } * } * return Ok(); * } * catch (Exception) * { * await yuleLogger.logError(checkerClient, LoggingConstants.CREATED_NEW_MESSAGE_CATEGORY); * return StatusCode(StatusCodes.Status424FailedDependency); * } * * } * } * } * return StatusCode(StatusCodes.Status401Unauthorized); */ return(StatusCode(StatusCodes.Status501NotImplemented)); }