|
4 | 4 | import click |
5 | 5 | import pandas as pd |
6 | 6 | from pyodk.client import Client |
| 7 | +from pyodk.errors import PyODKError |
7 | 8 | from requests.exceptions import ConnectionError, Timeout |
8 | 9 | from rich.console import Console |
9 | 10 | from rich.progress import Progress |
@@ -173,22 +174,25 @@ def main(project_id: int, center_ids: tuple[int, ...], output_dir: Path, config: |
173 | 174 | output_dir.mkdir(parents=True, exist_ok=True) |
174 | 175 | output_csv = output_dir / "candidate_results.csv" |
175 | 176 |
|
176 | | - with Client(config_path=str(config)) as client: |
177 | | - if not center_ids: |
178 | | - forms = client.forms.list(project_id=project_id) |
179 | | - center_ids = tuple( |
180 | | - int(f.xmlFormId.removeprefix("results_")) |
181 | | - for f in forms |
182 | | - if f.xmlFormId.startswith("results_") |
| 177 | + try: |
| 178 | + with Client(config_path=str(config)) as client: |
| 179 | + if not center_ids: |
| 180 | + forms = client.forms.list(project_id=project_id) |
| 181 | + center_ids = tuple( |
| 182 | + int(f.xmlFormId.removeprefix("results_")) |
| 183 | + for f in forms |
| 184 | + if f.xmlFormId.startswith("results_") |
| 185 | + ) |
| 186 | + console.log(f"Discovered {len(center_ids)} results forms") |
| 187 | + |
| 188 | + results_df = export_center_candidate_results( |
| 189 | + client=client, |
| 190 | + project_id=project_id, |
| 191 | + center_ids=list(center_ids), |
| 192 | + output_dir=output_dir, |
183 | 193 | ) |
184 | | - console.log(f"Discovered {len(center_ids)} results forms") |
185 | | - |
186 | | - results_df = export_center_candidate_results( |
187 | | - client=client, |
188 | | - project_id=project_id, |
189 | | - center_ids=list(center_ids), |
190 | | - output_dir=output_dir, |
191 | | - ) |
| 194 | + except PyODKError as exc: |
| 195 | + raise click.ClickException(str(exc)) from exc |
192 | 196 |
|
193 | 197 | results_df.to_csv(output_csv, index=False) |
194 | 198 | console.log(f"Saved {len(results_df)} rows to {output_csv}") |
|
0 commit comments