001/**
002 * Copyright 2017, Digi International Inc.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015 */
016package com.digi.xbee.api.exceptions;
017
018import com.digi.xbee.api.models.XBeeTransmitStatus;
019
020/**
021 * This exception will be thrown when receiving a transmit status different than 
022 * {@code XBeeTransmitStatus#SUCCESS} after sending an XBee API packet.
023 * 
024 * @see CommunicationException
025 * @see com.digi.xbee.api.models.XBeeTransmitStatus
026 */
027public class TransmitException extends CommunicationException {
028
029        // Constants.
030        private static final long serialVersionUID = 1L;
031        private static final String DEFAULT_MESSAGE = "There was a problem transmitting the XBee API packet.";
032        
033        // Variables.
034        private final XBeeTransmitStatus transmitStatus;
035        
036        /**
037         * Creates a {@code TransmitException} with the provided 
038         * {@code XBeeTransmitStatus} indicating the status of the transmission and 
039         * {@value #DEFAULT_MESSAGE} as its error detail message.
040         * 
041         * @param transmitStatus The status of the transmission.
042         * 
043         * @see com.digi.xbee.api.models.XBeeTransmitStatus
044         */
045        public TransmitException(XBeeTransmitStatus transmitStatus) {
046                super(DEFAULT_MESSAGE);
047                this.transmitStatus = transmitStatus;
048        }
049        
050        /**
051         * Creates a {@code TransmitException} with the specified message and 
052         * {@code XBeeTransmitStatus} indicating the status of the transmission.
053         * 
054         * @param message The associated message.
055         * @param transmitStatus The status of the transmission.
056         * 
057         * @see com.digi.xbee.api.models.XBeeTransmitStatus
058         */
059        public TransmitException(String message, XBeeTransmitStatus transmitStatus) {
060                super(message);
061                this.transmitStatus = transmitStatus;
062        }
063        
064        /**
065         * Creates a {@code TransmitException} with the specified message, cause and 
066         * {@code XBeeTransmitStatus} indicating the status of the transmission.
067         * 
068         * @param message The associated message.
069         * @param cause The cause of this exception.
070         * @param transmitStatus The status of the transmission.
071         * 
072         * @see Throwable
073         * @see com.digi.xbee.api.models.XBeeTransmitStatus
074         */
075        public TransmitException(String message, Throwable cause, XBeeTransmitStatus transmitStatus) {
076                super(message, cause);
077                this.transmitStatus = transmitStatus;
078        }
079        
080        /**
081         * Returns the {@code XBeeTransmitStatus} of the exception containing 
082         * information about the transmission.
083         * 
084         * @return The status of the transmission.
085         * 
086         * @see com.digi.xbee.api.models.XBeeTransmitStatus
087         */
088        public XBeeTransmitStatus getTransmitStatus() {
089                return transmitStatus;
090        }
091        
092        /**
093         * Returns the text containing the status of the transmission from 
094         * the exception.
095         * 
096         * @return The text with the status of the transmission.
097         */
098        public String getTransmitStatusMessage() {
099                if (transmitStatus != null)
100                        return transmitStatus.getDescription();
101                return null;
102        }
103        
104        /*
105         * (non-Javadoc)
106         * @see java.lang.Throwable#getMessage()
107         */
108        @Override
109        public String getMessage() {
110                String message = super.getMessage();
111                
112                if (message == null)
113                        message = "";
114                
115                if (transmitStatus != null) {
116                        if (message.length() > 0)
117                                message = message + " > ";
118                        message = message + transmitStatus.toString();
119                }
120                
121                return message;
122        }
123}