示例#1
0
        public IResult <Movie> Delete(string id)
        {
            IResult <Movie> movieToDeleteResult = moviesRepository.GetById(id);

            switch (movieToDeleteResult)
            {
            case Error <Movie> error:
                Exception exception = new IdNotFoundException("Delete-method id not found exception.", error.Exception);
                return(new Error <Movie>(exception));

            case OK <Movie> ok:
                try
                {
                    moviesRepository.Delete(ok.Result);
                    return(ok);
                }
                catch
                {
                    Exception deleteRepoException = new RepositoryUnknownException("Delete-method repository exception.");
                    return(new Error <Movie>(deleteRepoException));
                }

            default:
                Exception getByIdRepoException = new RepositoryUnknownException("GetById-method repository exception.");
                return(new Error <Movie>(getByIdRepoException));
            }
        }
示例#2
0
        public IResult <Movie> Update(Movie movie)
        {
            ValidateMovie(movie);

            IResult <Movie> movieToUpdateResult = moviesRepository.GetById(movie.MovieId);

            switch (movieToUpdateResult)
            {
            case Error <Movie> error:
                Exception exception = new IdNotFoundException("Update-method id not found exception.", error.Exception);
                return(new Error <Movie>(exception));

            case OK <Movie> ok:
                try
                {
                    moviesRepository.Update(ok.Result);
                    return(ok);
                }
                catch
                {
                    Exception updateRepoException = new RepositoryUnknownException("Update-method repository exception.");
                    return(new Error <Movie>(updateRepoException));
                }

            default:
                Exception getByIdRepoException = new RepositoryUnknownException("GetById-method repository exception.");
                return(new Error <Movie>(getByIdRepoException));
            }
        }
示例#3
0
        /// <summary>
        /// Handles IoT Hub exception and transform it as an <see cref="ApiException"/> with a well-known application error code.
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="iotHubException"></param>
        /// <param name="apiException"></param>
        /// <returns></returns>
        private bool TryHandleIotHubException(string deviceId, IotHubException iotHubException, out ApiException apiException)
        {
            switch (iotHubException)
            {
            // thrown when there's no IoT Hub device registration OR there's an enabled IoT Hub device registration but device did not establish connection yet
            case DeviceNotFoundException ex:
                apiException = new IdNotFoundException(ErrorCodes.DeviceNotFound, deviceId, ex);
                break;

            // thrown when an attempt to communicate with the IoT Hub fails
            case IotHubCommunicationException ex:
                m_Logger.LogWarning($"An IotHubCommunicationException occurred: {ex}");
                apiException = new CommunicationException(ErrorCodes.CommunicationError, ex.Message, ex);
                break;

            // thrown when the IoT Hub returns an error code (i.e. device registration is disable which prevent the actual device from establishing a connection)
            case ServerErrorException ex:
                m_Logger.LogWarning($"A ServerErrorException occurred: {ex}");
                apiException = new CommunicationException(ErrorCodes.GatewayError, ErrorMessages.GetGatewayErrorMessage(), ex);
                break;

            // thrown when the maximum number of IoT Hub messages has been reached
            case QuotaExceededException ex:
                apiException = new CommunicationException(ErrorCodes.QuotaExceeded, ErrorMessages.GetQuotaExceededErrorMessage(), ex);
                break;

            // thrown when the message size is greater than the max size allowed (131072 bytes)
            case MessageTooLargeException ex:
                apiException = new InvalidResultException(ErrorCodes.MessageTooLarge, ErrorMessages.GetMessageTooLargeErrorMessage(), ex);
                break;

            // thrown when an error occurs during device client operation (i.e. device doesn't repond within the configured time out)
            // shall always be kept last
            case IotHubException ex:
                m_Logger.LogWarning($"An IotHubException occurred: {ex}");
                apiException = new OperationException(ErrorCodes.DeviceOperationError, ErrorMessages.GetOperationErrorMessage(), ex);
                break;

            // exception won't be transformed and therefore will be logged accordingly
            default:
                apiException = null;
                break;
            }

            return(apiException != null);
        }