Skip to content

Commit

Permalink
Merge pull request #2134 from mirpedrol/handle-json-exception
Browse files Browse the repository at this point in the history
Handle json.load() exceptions
  • Loading branch information
mirpedrol authored Dec 17, 2022
2 parents b33d0c7 + 632b675 commit d45b8e3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

- Only check that a pipeline name doesn't contain dashes if the name is provided by prompt of `--name`. Don't check if a template file is used. ([#2123](https://github.com/nf-core/tools/pull/2123))
- Deprecate `--enable_conda` parameter. Use `conda.enable` instead ([#2131](https://github.com/nf-core/tools/pull/2131))
- Handle `json.load()` exceptions ([#2134](https://github.com/nf-core/tools/pull/2134))

## [v2.7.1 - Mercury Eagle Patch](https://github.com/nf-core/tools/releases/tag/2.7.1) - [2022-12-08]

Expand Down
6 changes: 5 additions & 1 deletion nf_core/modules/modules_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,11 @@ def load(self):
"""
try:
with open(self.modules_json_path, "r") as fh:
self.modules_json = json.load(fh)
try:
self.modules_json = json.load(fh)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{self.modules_json_path}' due to error {e}")

except FileNotFoundError:
raise UserWarning("File 'modules.json' is missing")

Expand Down
5 changes: 4 additions & 1 deletion nf_core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ def load_input_params(self, params_path):
# First, try to load as JSON
try:
with open(params_path, "r") as fh:
params = json.load(fh)
try:
params = json.load(fh)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{params_path}' due to error {e}")
self.input_params.update(params)
log.debug(f"Loaded JSON input params: {params_path}")
except Exception as json_e:
Expand Down
5 changes: 4 additions & 1 deletion nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ def fetch_wf_config(wf_path, cache_config=True):
if os.path.isfile(cache_path):
log.debug(f"Found a config cache, loading: {cache_path}")
with open(cache_path, "r") as fh:
config = json.load(fh)
try:
config = json.load(fh)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{cache_path}' due to error {e}")
return config
log.debug("No config cache found")

Expand Down
10 changes: 8 additions & 2 deletions tests/modules/modules_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def test_get_modules_json(self):
"""Checks that the get_modules_json function returns the correct result"""
mod_json_path = os.path.join(self.pipeline_dir, "modules.json")
with open(mod_json_path, "r") as fh:
mod_json_sb = json.load(fh)
try:
mod_json_sb = json.load(fh)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{mod_json_path}' due to error {e}")

mod_json_obj = ModulesJson(self.pipeline_dir)
mod_json = mod_json_obj.get_modules_json()
Expand Down Expand Up @@ -212,7 +215,10 @@ def test_mod_json_dump(self):

# Check that the dump function writes the correct content
with open(mod_json_path, "r") as f:
mod_json_new = json.load(f)
try:
mod_json_new = json.load(f)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{mod_json_path}' due to error {e}")
assert mod_json == mod_json_new


Expand Down
6 changes: 5 additions & 1 deletion tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ def test_build_command_params(self):
)
# Check saved parameters file
with open(self.nf_params_fn, "r") as fh:
saved_json = json.load(fh)
try:
saved_json = json.load(fh)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{self.nf_params_fn}' due to error {e}")

assert saved_json == {"input": "custom_input"}

def test_build_command_params_cl(self):
Expand Down
5 changes: 4 additions & 1 deletion tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ def test_json_output(self, tmp_dir):

# Load created JSON file and check its contents
with open(json_fn, "r") as fh:
saved_json = json.load(fh)
try:
saved_json = json.load(fh)
except json.JSONDecodeError as e:
raise UserWarning(f"Unable to load JSON file '{json_fn}' due to error {e}")
assert saved_json["num_tests_pass"] > 0
assert saved_json["num_tests_warned"] > 0
assert saved_json["num_tests_ignored"] == 0
Expand Down

0 comments on commit d45b8e3

Please sign in to comment.