Exemple #1
0
        /// <summary>
        /// Returns an error message for a given <see cref="FormatErrorType"/> value.
        /// </summary>
        /// <param name="formatErrorType">One of the <see cref="FormatErrorType"/> values.</param>
        /// <returns>A string that contains the error message.</returns>
        private static string GetErrorMessage(FormatErrorType formatErrorType)
        {
            switch (formatErrorType)
            {
            case FormatErrorType.MSGMIN:
                return("Largo de mensaje < permitido");

            case FormatErrorType.MSGMAX:
                return("Largo de mensaje > permitido");

            case FormatErrorType.MSGFMT:
                return("Error de formato en mensaje");

            case FormatErrorType.MSGLEN:
                return("Error de tamaño de mensaje");

            case FormatErrorType.MSGCMD:
                return("Comando no válido");

            case FormatErrorType.MSGCKS:
                return("Checksum no válido");

            case FormatErrorType.MSGDAT:
                return("Parámetros no válidos");

            case FormatErrorType.GENSCS:
                return("Error general");

            case FormatErrorType.MSGHDR:
                return("Header no válido");

            default:
                return("Error General");
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates an error message with a given error code and multiple values.
        /// </summary>
        /// <param name="error">The error code of the message.</param>
        /// <param name="values">The values that describes the error.</param>
        /// <returns>The message.</returns>
        public static Message CreateErrorMessage(FormatErrorType error, params object[] values)
        {
            var message = new Message();

            message.Command = Command.Error;
            message.Data    = new List <string>()
            {
                ((int)error).ToString(), string.Join(fieldSeparator, values)
            };
            return(message);
        }
Exemple #3
0
        /// <summary>
        /// Creates an error message with a given error code and message.
        /// </summary>
        /// <param name="error">The error code of the message.</param>
        /// <param name="description">The description of the error.</param>
        /// <returns>The message.</returns>
        public static Message CreateErrorMessage(FormatErrorType error, string description)
        {
            var message = new Message();

            message.Command = Command.Error;
            message.Data    = new List <string>()
            {
                ((int)error).ToString(), description
            };
            return(message);
        }
Exemple #4
0
 public FormatValidateResult(FormatErrorType errorType)
 {
     this.ErrorType = errorType;
 }
Exemple #5
0
 public FormatValidateResult()
 {
     this.ErrorType = FormatErrorType.None;
 }
 public FormatValidateResult(FormatErrorType errorType)
 {
     this.ErrorType = errorType;
 }
Exemple #7
0
        /// <summary>
        /// Converts the string representation of a message to its object equivalent.
        /// </summary>
        /// <param name="s">A string containing a message to convert.</param>
        /// <returns>An object equivalent to the message contained in <see cref="s"/>.</returns>
        public static Message Parse(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            if (s.Equals(string.Empty))
            {
                throw new InvalidCastException("The input string is empty.");
            }

            if (s.Trim().Equals(string.Empty))
            {
                throw new InvalidCastException("The input string contains only spaces.");
            }

            int     messageLength, command;
            Message message = new Message();

            message.Content = s;

            if (s.Length < StructureLength.CMD_POS || s.Substring(0, StructureLength.HEAD_LEN) != header)
            {
                throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGHDR), FormatErrorType.MSGHDR);
            }

            if (!int.TryParse(s.Substring(StructureLength.LEN_POS, StructureLength.LEN_LEN), out messageLength))
            {
                throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGDAT), FormatErrorType.MSGDAT);
            }

            if (messageLength < StructureLength.MIN_MSG_LEN || messageLength > StructureLength.MAX_MSG_LEN)
            {
                FormatErrorType error = (messageLength < StructureLength.MIN_MSG_LEN ? FormatErrorType.MSGMIN : FormatErrorType.MSGMAX);
                throw new CommunicationException(GetErrorMessage(error), error);
            }

            if (s.Length < messageLength)
            {
                throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGLEN), FormatErrorType.MSGLEN);
            }

            if (s.Last() != footer)
            {
                throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGFMT), FormatErrorType.MSGFMT);
            }

            message.Data = s.Substring(StructureLength.DAT_POS, messageLength - StructureLength.FIX_LEN).Split(fieldSeparator[0]).ToList();

            if (!int.TryParse(s.Substring(StructureLength.CMD_POS, StructureLength.CMD_LEN), out command))
            {
                throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGCMD), FormatErrorType.MSGCMD);
            }

            if (Enum.IsDefined(typeof(Command), command) && command != (int)Command.Error)
            {
                message.Command = (Command)Enum.Parse(typeof(Command), command.ToString(), true);
                if (IsChecksumValid(s))
                {
                    return(message);
                }
                else
                {
                    throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGCKS), FormatErrorType.MSGCKS);
                }
            }
            else if (command == (int)Command.Error)
            {
                int    internalErrorCode = Convert.ToInt32(message.Data[0]);
                string internalErrorMessage;

                if (message.Data.Count > 1)
                {
                    internalErrorMessage = message.Data[1];
                }
                else
                {
                    internalErrorMessage = GetInternalErrorMessage(internalErrorCode);
                }

                InternalError internalError = new InternalError()
                {
                    ErrorCode = internalErrorCode,
                    Message   = internalErrorMessage
                };

                throw new CommunicationException(GetErrorMessage(FormatErrorType.GENSCS), FormatErrorType.GENSCS, internalError);
            }
            else
            {
                throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGCMD), FormatErrorType.MSGCMD);
            }
        }
 public CommunicationException(string message, FormatErrorType formatError, InternalError internalError)
     : base(message)
 {
     FormatError   = formatError;
     InternalError = internalError;
 }