Skip to content

Comments

Fix: Internal Server Error When Generating Data File for Restored Cases#27

Open
parthdagia05 wants to merge 1 commit intoOSeMOSYS:masterfrom
parthdagia05:fix/restore-case-without-datafile
Open

Fix: Internal Server Error When Generating Data File for Restored Cases#27
parthdagia05 wants to merge 1 commit intoOSeMOSYS:masterfrom
parthdagia05:fix/restore-case-without-datafile

Conversation

@parthdagia05
Copy link

Bug Fix: Defensive Directory Creation for Data Generation 🛡️

Closes: #5

Summary

This PR resolves a 500 Internal Server Error that occurred when attempting to generate a data file for a restored case where the execution directory (res/<caserunname>/) was missing from the filesystem.

This ensures that the application remains resilient even if the metadata exists in resData.json but the physical directory structure was lost during a backup/restore cycle or manual cleanup.


Root Cause Analysis

The generateDatafile() method attempted to write a data.txt file directly to the path:
res/<caserunname>/data.txt

If the parent directory (<caserunname>/) did not exist, the operation would raise a FileNotFoundError, crashing the Flask route and returning an unhandled 500 error to the UI.

Typical Failure Scenario:

  1. A case run is initialized, and metadata is saved to resData.json.
  2. The system is backed up without the (empty) result directories.
  3. The model is restored on a new machine.
  4. Generating the data file fails because the expected directory path is missing.

Changes Implemented

1. Defensive Directory Provisioning

Added an idempotent directory check using pathlib before the file write operation:

os.makedirs(dataFilePath.parent, exist_ok=True)

…ithout run directory

- Ensure res/<caserunname>/ is created before writing data.txt
- Add defensive os.makedirs(dataFilePath.parent, exist_ok=True)
- Improve route error handling for OSError and unexpected exceptions
- Resolves internal server error when restored case metadata exists but run directory was never created
@parthdagia05
Copy link
Author

can anyone cross check this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant