Skip to content

[Bug] Multiple unhandled errors in code_generation, model_run, and data_upload services #268

@nguyenhungduong

Description

@nguyenhungduong

Summary

Five bugs found while reading the backend services that cause unhandled crashes or silent data corruption.


Bug 1 — services/code_generation.py: AttributeError when model not found

File: tensormap-backend/app/services/code_generation.py

generate_code() accesses model_configs.file_id without checking if model_configs is None first. If the model name doesn't exist in the DB, this raises AttributeError and returns 500 instead of a clean 404.

# Current — crashes if model not found
model_configs = db.exec(...).first()
file = db.exec(select(DataFile).where(DataFile.id == model_configs.file_id)).first()  # AttributeError

# Fix
if model_configs is None:
    raise ValueError(f"Model '{model_name}' not found in database")

Bug 2 — services/code_generation.py: KeyError for unknown problem type

File: tensormap-backend/app/services/code_generation.py

_map_template() uses options[problem_type_id] (dict key access) instead of options.get(problem_type_id). Any unrecognized model_type raises KeyError → 500 with no useful message.

# Current — KeyError on unknown type
return options[problem_type_id]

# Fix
return options.get(problem_type_id)  # returns None, caller raises ValueError with clear message

Bug 3 — services/model_run.py: AttributeError when model not found

File: tensormap-backend/app/services/model_run.py

Same pattern as Bug 1. _run() does not check if model_configs is None before accessing .model_type, crashing with AttributeError.

# Current — crashes if model not found
model_configs = db.exec(...).first()
if model_configs.model_type == ProblemType.IMAGE_CLASSIFICATION:  # AttributeError

Bug 4 — services/model_run.py: KeyError / crash when target_field is None or missing from CSV

File: tensormap-backend/app/services/model_run.py

features.drop(model_configs.target_field, axis=1) crashes with KeyError if:

  • target_field is None (user hasn't configured training yet)
  • target_field doesn't exist as a column in the CSV

The error message is unhelpful and the root cause is unclear to the user.


Bug 5 — services/data_upload.py: Uploading a duplicate filename silently overwrites the file on disk

File: tensormap-backend/app/services/data_upload.py

If a user uploads a file with the same name as an existing file, the new file overwrites the old one on disk, but a new DB record is created. The old DB record now points to a file with different content — causing silent data corruption.

Fix: Check for existing (file_name, file_type) in DB before saving. Return 409 Conflict if duplicate found.


Steps to Reproduce

  • Bug 1 & 2: Call POST /model/code with a model_name that doesn't exist in the DB
  • Bug 3: Call POST /model/run with a model_name that doesn't exist in the DB
  • Bug 4: Call POST /model/run on a model where target_field is None or was deleted from the CSV
  • Bug 5: Upload the same CSV file twice

Expected Behavior

Clean error responses (400/404/409) with descriptive messages instead of unhandled 500 crashes or silent data corruption.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions