fix: 统一异常处理

This commit is contained in:
sophie committed 2026-05-30 17:06:32 +08:00
1 parent 4daf8dfcaa
commit 0c5cfcaa4a
21 files changed
+2124 -4218

No files matched your search

+13 -33
View File
@@ -1,27 +1,20 @@
from __future__ import annotations
from datetime import datetime
from flask import Blueprint, jsonify, request, send_file
from app.auth_jwt import get_current_user
from app.services.schedule.query_service import can_view_run, get_run, list_runs, serialize_run
from app.errors import AppError, ValidationError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, ServiceUnavailableError, raise_for_status
workspace_schedule_bp = Blueprint('workspace_schedule', __name__)
def _require_user():
user = get_current_user()
if not user:
return None, (jsonify({'error': 'Unauthorized'}), 401)
return user, None
raise UnauthorizedError()
return user
@workspace_schedule_bp.route('/schedule-runs', methods=['GET'])
def workspace_schedule_runs():
user, err = _require_user()
if err:
return err
user = _require_user()
status = str(request.args.get('status') or '').strip() or None
schedule_name = str(request.args.get('schedule_name') or '').strip() or None
started_after_str = request.args.get('started_after')
@@ -29,41 +22,28 @@ def workspace_schedule_runs():
started_after = datetime.fromisoformat(started_after_str) if started_after_str else None
started_before = datetime.fromisoformat(started_before_str) if started_before_str else None
limit = int(request.args.get('limit') or 100)
return jsonify(list_runs(
notify_user_id=user.id,
schedule_name=schedule_name,
status=status,
started_after=started_after,
started_before=started_before,
limit=limit,
))
return jsonify(list_runs(notify_user_id=user.id, schedule_name=schedule_name, status=status, started_after=started_after, started_before=started_before, limit=limit))
@workspace_schedule_bp.route('/schedule-runs/<int:run_id>', methods=['GET'])
def workspace_schedule_run_detail(run_id: int):
user, err = _require_user()
if err:
return err
user = _require_user()
run = get_run(run_id)
if not run:
return jsonify({'error': 'Not found'}), 404
raise NotFoundError('Not found')
if not can_view_run(run, user.id):
return jsonify({'error': 'Forbidden'}), 403
raise ForbiddenError('Forbidden')
return jsonify(serialize_run(run))
@workspace_schedule_bp.route('/schedule-runs/<int:run_id>/excel', methods=['GET'])
def workspace_schedule_run_excel(run_id: int):
from io import BytesIO
user, err = _require_user()
if err:
return err
user = _require_user()
run = get_run(run_id)
if not run:
return jsonify({'error': 'Not found'}), 404
raise NotFoundError('Not found')
if not can_view_run(run, user.id):
return jsonify({'error': 'Forbidden'}), 403
raise ForbiddenError('Forbidden')
if not run.excel_file_data:
return jsonify({'error': 'Excel not found'}), 404
raise NotFoundError('Excel not found')
buf = BytesIO(run.excel_file_data)
return send_file(buf, as_attachment=True, download_name=run.excel_file_name or 'result.xlsx', mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
return send_file(buf, as_attachment=True, download_name=run.excel_file_name or 'result.xlsx', mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')