""" Contains all error classes """ ############################################################ # read errors ############################################################ class RecordSizeError(Exception): """ Exception raised when assert_eor finds EOR == False. This means that the data read does not match the record length. """ def __init__(self, reclen, pos): self.reclen = reclen self.pos = pos def __str__(self): """Return error message.""" return ( "\n" + "Not at end of data record in\n" + "Position = {:d}\n" + "RecLen = {:d}" ).format( self.pos, self.reclen, ) class ReadError(Exception): """ Exception raised when data could not be read correctly. """ def __init__(self, message): self.message = message def __str__(self): """ Return error message. """ return f"\nError reading record from file: {self.message}" class DataSizeError(Exception): """ Exception raised when assert_eor finds EOR == False. This means that the data read does not match the record length. """ def __init__(self, filename, reclen, pos): self.filename = filename self.reclen = reclen self.pos = pos def __str__(self): """Return error message.""" return ( f"Not at end of data record in file {self.filename!s}.\n" + f"Position = {self.pos:d}\n" + f"RecLen = {self.reclen:d}" ) class DataFileError(Exception): """ Exception raised when assert_eof finds EOF == False. This means that the data file size does not match the file length. """ def __init__(self, filename, filesize, pos): self.filename = filename self.filesize = filesize self.pos = pos def __str__(self): """Return error message.""" return ( f"Not at end of data file in file {self.filename!s}.\n" + f"Position = {self.pos:d}\n" + f"Filesize = {self.filesize:d}" ) class RecLenError(Exception): """ Exception raised when a record was not read correctly. This means that the record header does not match the record trailer (both should contain the length of the data area). """ def __init__(self, filename, message): self.filename = filename # name of file which caused an error self.message = message def __str__(self): """Return error message.""" return ( f"Error reading record from file {self.filename!s}.\n{self.message}" ) class FileReadError(Exception): """ Exception raised when data could not be read correctly. """ def __init__(self, filename, message): self.filename = filename # name of file which caused an error self.message = message def __str__(self): """ Return error message. """ return ( f"Error reading record from file {self.filename!s}: {self.message}" ) class StringReadError(Exception): """ Exception raised string length is not specified. """ def __init__(self, message): self.message = message def __str__(self): """ Return error message. """ return self.message ############################################################ # Write Errors ############################################################ class RecordBeginningError(Exception): """ Exception raised when assert_bor finds BOR == False. This means that the data buffer is not empty. """ def __init__(self, pos): self.pos = pos def __str__(self): """Return error message.""" return ( f"\nNot at beginning of data record:\nPosition = {self.pos:d}" ) class WriteError(Exception): """ Exception raised when data could not be written correctly. """ def __init__(self, filename, message): self.filename = filename self.message = message def __str__(self): """ Return error message. """ return ( f"Error writing record to file {self.filename!s}: {self.message}"\ )