Skip to content

Commit

Permalink
Feature/better error messages (#292)
Browse files Browse the repository at this point in the history
* add custom exception to propagate error message to harmony

* numpy back to version 1

* update harmony library
  • Loading branch information
sliu008 authored Oct 7, 2024
1 parent 713e7b8 commit e439631
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 517 deletions.
4 changes: 3 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ confidence=
disable=too-many-lines,
too-many-arguments,
too-many-locals,
no-member
no-member,
too-many-positional-arguments,
too-many-statements

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Added custom exception to progate error messages to harmony.
### Changed
### Deprecated
### Removed
Expand Down
40 changes: 32 additions & 8 deletions podaac/subsetter/subset_harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,44 @@
import subprocess
import shutil
from tempfile import mkdtemp
import traceback
from typing import List, Union

import pystac
from pystac import Asset

import harmony
import harmony_service_lib
import numpy as np
from harmony import BaseHarmonyAdapter
from harmony.util import download, stage, generate_output_filename, bbox_to_geometry
from harmony_service_lib import BaseHarmonyAdapter
from harmony_service_lib.util import download, stage, generate_output_filename, bbox_to_geometry
from harmony_service_lib.exceptions import HarmonyException

from podaac.subsetter import subset
from podaac.subsetter.subset import SERVICE_NAME

DATA_DIRECTORY_ENV = "DATA_DIRECTORY"


class L2SSException(HarmonyException):
"""Base class for exceptions in the Harmony GDAL Adapter."""

def __init__(self, original_exception):
# Extract the last traceback entry (most recent call) for the error location
tb = traceback.extract_tb(original_exception.__traceback__)[-1]

# Get the error details: file, line, function, and message
filename = tb.filename
lineno = tb.lineno
funcname = tb.name
error_msg = str(original_exception)

# Format the error message to be more readable
readable_message = (f"Error in file '{filename}', line {lineno}, in function '{funcname}': "
f"{error_msg}")

super().__init__(readable_message, 'nasa/harmony-gdal-adapter')


def podaac_to_harmony_bbox(bbox: np.ndarray) -> Union[np.ndarray, float]:
"""
Convert PO.DAAC bbox ((west, east), (south, north))
Expand Down Expand Up @@ -89,7 +111,7 @@ def __init__(self, message, catalog=None, config=None):

self.data_dir = os.getenv(DATA_DIRECTORY_ENV, '/home/dockeruser/data')

def process_item(self, item: pystac.Item, source: harmony.message.Source) -> pystac.Item:
def process_item(self, item: pystac.Item, source: harmony_service_lib.message.Source) -> pystac.Item:
"""
Performs variable and bounding box subsetting on the input STAC Item's data, returning
an output STAC item
Expand Down Expand Up @@ -206,6 +228,8 @@ def filter_by_subtype(variables, subtype):

# Return the STAC record
return result
except Exception as ex:
raise L2SSException(ex) from ex
finally:
# Clean up any intermediate resources
shutil.rmtree(temp_dir)
Expand Down Expand Up @@ -240,7 +264,7 @@ def cmd(self, *args) -> List[str]:
return result_str.split("\n")


def main(config: harmony.util.Config = None) -> None:
def main(config: harmony_service_lib.util.Config = None) -> None:
"""Parse command line arguments and invoke the service to respond to
them.
Expand All @@ -254,10 +278,10 @@ def main(config: harmony.util.Config = None) -> None:
"""
parser = argparse.ArgumentParser(prog=SERVICE_NAME,
description='Run the l2_subsetter service')
harmony.setup_cli(parser)
harmony_service_lib.setup_cli(parser)
args = parser.parse_args()
if harmony.is_harmony_cli(args):
harmony.run_cli(parser, args, L2SubsetterService, cfg=config)
if harmony_service_lib.is_harmony_cli(args):
harmony_service_lib.run_cli(parser, args, L2SubsetterService, cfg=config)
else:
parser.error("Only --harmony CLIs are supported")

Expand Down
Loading

0 comments on commit e439631

Please sign in to comment.