Narrow exception handlers for better diagnostics
Replace broad `except Exception` with specific types (FileNotFoundError, PermissionError, OSError) so errors are properly categorized in logs. Users reporting bugs will get actionable messages instead of generic ones.
This commit is contained in:
parent
c50106c185
commit
69ce1df5a1
3 changed files with 19 additions and 5 deletions
|
|
@ -91,6 +91,11 @@ async def preview(
|
|||
return Response(content=png_bytes, media_type="image/png")
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e)) from e
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(status_code=404, detail="PDF file not found on disk") from exc
|
||||
except OSError as exc:
|
||||
logger.exception("I/O error generating preview for %s", doc_id)
|
||||
raise HTTPException(status_code=422, detail="Failed to read PDF file") from exc
|
||||
except Exception as exc:
|
||||
logger.exception("Failed to generate preview")
|
||||
logger.exception("Unexpected error generating preview for %s", doc_id)
|
||||
raise HTTPException(status_code=422, detail="Failed to generate preview") from exc
|
||||
|
|
|
|||
|
|
@ -192,5 +192,7 @@ async def _mark_failed(job_id: str, error: str) -> None:
|
|||
if job:
|
||||
job.mark_failed(error)
|
||||
await analysis_repo.update_status(job)
|
||||
except OSError:
|
||||
logger.exception("Database I/O error marking job %s as failed", job_id)
|
||||
except Exception:
|
||||
logger.exception("Could not mark job %s as failed", job_id)
|
||||
logger.exception("Unexpected error marking job %s as failed", job_id)
|
||||
|
|
|
|||
|
|
@ -79,8 +79,12 @@ async def delete(doc_id: str) -> bool:
|
|||
os.unlink(real_path)
|
||||
elif os.path.exists(doc.storage_path):
|
||||
logger.warning("Refused to delete file outside upload dir: %s", doc.storage_path)
|
||||
except FileNotFoundError:
|
||||
logger.info("File already removed: %s", doc.storage_path)
|
||||
except PermissionError:
|
||||
logger.error("Permission denied deleting file: %s", doc.storage_path)
|
||||
except OSError:
|
||||
logger.warning("Could not delete file: %s", doc.storage_path)
|
||||
logger.warning("Could not delete file: %s", doc.storage_path, exc_info=True)
|
||||
|
||||
return await document_repo.delete(doc_id)
|
||||
|
||||
|
|
@ -101,6 +105,9 @@ def _count_pages(file_content: bytes) -> int | None:
|
|||
try:
|
||||
info = pdfinfo_from_bytes(file_content)
|
||||
return info.get("Pages")
|
||||
except Exception:
|
||||
logger.warning("Could not count pages", exc_info=True)
|
||||
except (FileNotFoundError, OSError) as exc:
|
||||
logger.warning("Could not count pages: %s", exc)
|
||||
return None
|
||||
except Exception:
|
||||
logger.warning("Unexpected error counting pages", exc_info=True)
|
||||
return None
|
||||
|
|
|
|||
Loading…
Reference in a new issue