Skip to content

Commit

Permalink
chore: Better error messages in ideascale importer | NPG-7753 (#484)
Browse files Browse the repository at this point in the history
# Description

* Makes Ideascale importer log an error instead of throwing an
exception.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

- Ran the ideascale importer with a campaign that has no tagline.

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

Co-authored-by: Oleksandr Prokhorenko <[email protected]>
  • Loading branch information
FelipeRosa and minikin authored Jul 26, 2023
1 parent a526e59 commit a131572
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ async def inner(
ideascale_api_url,
)

await importer.connect()
await importer.run()
await importer.close()
try:
await importer.connect()
await importer.run()
await importer.close()
except Exception as e:
logger.error(e)

asyncio.run(inner(event_id, campaign_group_id, stage_ids, proposals_scores_csv, ideascale_api_url))
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@ def from_json_file(path: str) -> "Config":
class ReadConfigException(Exception):
"""Raised when the configuration file cannot be read."""

...
def __init__(self, cause: str):
super().__init__(f"Failed to read config file: {cause}")


class ReadProposalsScoresCsv(Exception):
"""Raised when the proposals impact scores csv cannot be read."""

...
def __init__(self, cause: str):
super().__init__(f"Failed to read proposals impact score file: {cause}")


class MapObjectiveError(Exception):
"""Raised when mapping an objective from campaign data fails."""

def __init__(self, objective_field: str, campaign_field: str, cause: str):
super().__init__(f"Failed to map objective '{objective_field}' from campaign '{campaign_field}': {cause}")


class Mapper:
Expand All @@ -79,7 +88,10 @@ def __init__(self, vote_options_id: int, config: Config):

def map_objective(self, a: Campaign, event_id: int) -> ideascale_importer.db.models.Challenge:
"""Map a IdeaScale campaign into a objective."""
reward = parse_reward(a.tagline)
try:
reward = parse_reward(a.tagline)
except InvalidRewardsString as e:
raise MapObjectiveError("reward", "tagline", str(e))

return ideascale_importer.db.models.Challenge(
row_id=0,
Expand Down Expand Up @@ -169,7 +181,8 @@ class Reward:
class InvalidRewardsString(Exception):
"""Raised when the reward string cannot be parsed."""

...
def __init__(self):
super().__init__("Invalid rewards string")


def parse_reward(s: str) -> Reward:
Expand Down

0 comments on commit a131572

Please sign in to comment.