Skip to content

Commit

Permalink
Merge pull request #47 from winter-telescope/errors
Browse files Browse the repository at this point in the history
Add Error Handling
  • Loading branch information
robertdstein authored Jul 28, 2022
2 parents 74bd765 + a0f30ee commit adf5c28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
11 changes: 7 additions & 4 deletions winterdrp/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
from winterdrp.paths import base_name_key
from astropy.time import Time


class ProcessorError(BaseException):
pass


class ErrorReport:

def __init__(
self,
error,
processor_name,
images,
headers
contents: list[str]
):
self.error = error
self.processor_name = processor_name
self.filenames = [h[base_name_key] for h in headers]
self.contents = contents
self.t_error = Time.now()

def generate_log_message(self):
return "You messed up somewhere :)"
return f"Error for processor {self.processor_name} at time {self.t_error} UT: " \
f"{type(self.error).__name__} affected batch of length {len(self.contents)}."
21 changes: 17 additions & 4 deletions winterdrp/processors/base_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def save_fits(
logger.info(f"Saving to {path}")
save_to_path(data, header, path)


def save_mask(
self,
data: np.ndarray,
Expand All @@ -59,6 +58,10 @@ def get_hash(headers: list[astropy.io.fits.Header]):
key = "".join(sorted([x[base_name_key] + x[proc_history_key] for x in headers]))
return hashlib.sha1(key.encode()).hexdigest()

def image_batch_error_report(self, exception: Exception, batch):
contents = [x[base_name_key] for x in batch[1]]
return ErrorReport(exception, self.__module__, contents)


class BaseProcessor:

Expand Down Expand Up @@ -120,8 +123,8 @@ def base_apply(
try:
batch = self.apply(batch)
passed_batches.append(batch)
except ProcessingError as e:
err = ErrorReport(e, self.__module__, batch)
except Exception as e:
err = self.generate_error_report(e, batch)
logger.error(err.generate_log_message())
failures.append(err)

Expand All @@ -132,6 +135,9 @@ def base_apply(
def apply(self, batch):
raise NotImplementedError

def generate_error_report(self, exception: Exception, batch) -> ErrorReport:
raise NotImplementedError


class BaseImageProcessor(BaseProcessor, ImageHandler, ABC):

Expand Down Expand Up @@ -166,6 +172,9 @@ def _update_processing_history(
header["REDSOFT"] = "winterdrp"
return headers

def generate_error_report(self, exception: Exception, batch) -> ErrorReport:
return self.image_batch_error_report(exception, batch)


class ProcessorWithCache(BaseImageProcessor, ABC):

Expand Down Expand Up @@ -285,4 +294,8 @@ def _apply_to_candidates(
self,
candidate_table: pd.DataFrame,
) -> pd.DataFrame:
raise NotImplementedError
raise NotImplementedError

def generate_error_report(self, exception: Exception, batch: pd.DataFrame):
contents = batch[base_name_key]
return ErrorReport(exception, self.__module__, contents)

0 comments on commit adf5c28

Please sign in to comment.