|
24 | 24 | lang?: string; |
25 | 25 | lang_version?: string; |
26 | 26 | } |
| 27 | +
|
| 28 | + // API error response type (FastAPI returns {detail: string} or {detail: ValidationError[]}) |
| 29 | + interface ApiErrorResponse { |
| 30 | + detail?: string | Array<{ loc: (string | number)[]; msg: string; type: string }>; |
| 31 | + } |
| 32 | +
|
| 33 | + function getErrorMessage(err: unknown, fallback: string): string { |
| 34 | + if (err && typeof err === 'object' && 'detail' in err) { |
| 35 | + const detail = (err as ApiErrorResponse).detail; |
| 36 | + if (typeof detail === 'string') return detail; |
| 37 | + if (Array.isArray(detail) && detail.length > 0) { |
| 38 | + return detail.map(e => e.msg).join(', '); |
| 39 | + } |
| 40 | + } |
| 41 | + if (err instanceof Error) return err.message; |
| 42 | + return fallback; |
| 43 | + } |
27 | 44 | import {addToast} from "../stores/toastStore"; |
28 | 45 | import Spinner from "../components/Spinner.svelte"; |
29 | 46 | import {goto} from "@mateothegreat/svelte5-router"; |
|
572 | 589 | } |
573 | 590 |
|
574 | 591 | } catch (err) { |
575 | | - apiError = err.response?.data?.detail || "Error initiating script execution."; |
| 592 | + apiError = getErrorMessage(err, "Error initiating script execution."); |
576 | 593 | addToast(apiError, "error"); |
577 | 594 | result = {status: 'error', errors: apiError, execution_id: executionId}; |
578 | | - console.error("Error executing script:", err.response || err); |
| 595 | + console.error("Error executing script:", err); |
579 | 596 | } finally { |
580 | 597 | executing = false; |
581 | 598 | } |
582 | 599 | } |
583 | 600 |
|
584 | 601 | async function loadSavedScripts() { |
585 | 602 | if (!authenticated) return; |
586 | | - try { |
587 | | - const { data, error } = await listSavedScriptsApiV1ScriptsGet({}); |
588 | | - if (error) throw error; |
589 | | - savedScripts = (data || []).map((script, index) => ({ |
590 | | - ...script, |
591 | | - id: script.id || script._id || `temp_${index}_${Date.now()}` |
592 | | - })); |
593 | | - } catch (err) { |
594 | | - console.error("Error loading saved scripts:", err); |
| 603 | + const { data, error, response } = await listSavedScriptsApiV1ScriptsGet({}); |
| 604 | + if (error) { |
| 605 | + console.error("Error loading saved scripts:", error); |
595 | 606 | addToast("Failed to load saved scripts. You might need to log in again.", "error"); |
596 | | - if (err?.status === 401) { |
| 607 | + if (response?.status === 401) { |
597 | 608 | handleLogout(); |
598 | 609 | } |
| 610 | + return; |
599 | 611 | } |
| 612 | + savedScripts = (data || []).map((script, index) => ({ |
| 613 | + ...script, |
| 614 | + id: script.id || script._id || `temp_${index}_${Date.now()}` |
| 615 | + })); |
600 | 616 | } |
601 | 617 |
|
602 | 618 | function loadScript(scriptData: EditorScriptData): void { |
|
644 | 660 | const currentIdValue = get(currentScriptId); |
645 | 661 | let operation = currentIdValue ? 'update' : 'create'; |
646 | 662 |
|
647 | | - try { |
648 | | - const scriptData = { |
649 | | - name: nameValue, |
650 | | - script: scriptValue, |
651 | | - lang: langValue, |
652 | | - lang_version: versionValue |
653 | | - }; |
654 | | -
|
655 | | - if (operation === 'update') { |
656 | | - const { error: updateErr } = await updateSavedScriptApiV1ScriptsScriptIdPut({ |
657 | | - path: { script_id: currentIdValue }, |
658 | | - body: scriptData |
659 | | - }); |
660 | | - if (updateErr) { |
661 | | - if (updateErr?.status === 404) { |
662 | | - console.log('Script not found, falling back to create operation'); |
663 | | - currentScriptId.set(null); |
664 | | - operation = 'create'; |
665 | | - const { data, error } = await createSavedScriptApiV1ScriptsPost({ body: scriptData }); |
666 | | - if (error) throw error; |
667 | | - currentScriptId.set(data.id); |
668 | | - addToast("Script saved successfully.", "success"); |
669 | | - } else { |
670 | | - throw updateErr; |
| 663 | + const scriptData = { |
| 664 | + name: nameValue, |
| 665 | + script: scriptValue, |
| 666 | + lang: langValue, |
| 667 | + lang_version: versionValue |
| 668 | + }; |
| 669 | +
|
| 670 | + if (operation === 'update') { |
| 671 | + const { error: updateErr, response: updateResp } = await updateSavedScriptApiV1ScriptsScriptIdPut({ |
| 672 | + path: { script_id: currentIdValue }, |
| 673 | + body: scriptData |
| 674 | + }); |
| 675 | + if (updateErr) { |
| 676 | + if (updateResp?.status === 404) { |
| 677 | + console.log('Script not found, falling back to create operation'); |
| 678 | + currentScriptId.set(null); |
| 679 | + operation = 'create'; |
| 680 | + const { data, error, response } = await createSavedScriptApiV1ScriptsPost({ body: scriptData }); |
| 681 | + if (error) { |
| 682 | + console.error('Error saving script:', error); |
| 683 | + addToast('Failed to save script. Please try again.', 'error'); |
| 684 | + if (response?.status === 401) handleLogout(); |
| 685 | + return; |
671 | 686 | } |
| 687 | + currentScriptId.set(data.id); |
| 688 | + addToast("Script saved successfully.", "success"); |
| 689 | + } else if (updateResp?.status === 401) { |
| 690 | + console.error('Error updating script:', updateErr); |
| 691 | + addToast('Failed to update script. Please try again.', 'error'); |
| 692 | + handleLogout(); |
| 693 | + return; |
672 | 694 | } else { |
673 | | - addToast("Script updated successfully.", "success"); |
| 695 | + console.error('Error updating script:', updateErr); |
| 696 | + addToast('Failed to update script. Please try again.', 'error'); |
| 697 | + return; |
674 | 698 | } |
675 | 699 | } else { |
676 | | - const { data, error } = await createSavedScriptApiV1ScriptsPost({ body: scriptData }); |
677 | | - if (error) throw error; |
678 | | - currentScriptId.set(data.id); |
679 | | - addToast("Script saved successfully.", "success"); |
| 700 | + addToast("Script updated successfully.", "success"); |
680 | 701 | } |
681 | | - await loadSavedScripts(); |
682 | | - } catch (err) { |
683 | | - console.error(`Error ${operation === 'update' ? 'updating' : 'saving'} script:`, err); |
684 | | - addToast(`Failed to ${operation} script. Please try again.`, "error"); |
685 | | - if (err?.status === 401) { |
686 | | - handleLogout(); |
| 702 | + } else { |
| 703 | + const { data, error, response } = await createSavedScriptApiV1ScriptsPost({ body: scriptData }); |
| 704 | + if (error) { |
| 705 | + console.error('Error saving script:', error); |
| 706 | + addToast('Failed to save script. Please try again.', 'error'); |
| 707 | + if (response?.status === 401) handleLogout(); |
| 708 | + return; |
687 | 709 | } |
| 710 | + currentScriptId.set(data.id); |
| 711 | + addToast("Script saved successfully.", "success"); |
688 | 712 | } |
| 713 | + await loadSavedScripts(); |
689 | 714 | } |
690 | 715 |
|
691 | 716 | async function deleteScript(scriptIdToDelete: string): Promise<void> { |
|
697 | 722 |
|
698 | 723 | if (!confirm(confirmMessage)) return; |
699 | 724 |
|
700 | | - try { |
701 | | - const { error } = await deleteSavedScriptApiV1ScriptsScriptIdDelete({ |
702 | | - path: { script_id: scriptIdToDelete } |
703 | | - }); |
704 | | - if (error) throw error; |
705 | | - addToast("Script deleted successfully.", "success"); |
706 | | - if (get(currentScriptId) === scriptIdToDelete) { |
707 | | - newScript(); |
708 | | - } |
709 | | - await loadSavedScripts(); |
710 | | - } catch (err) { |
711 | | - console.error("Error deleting script:", err); |
| 725 | + const { error, response } = await deleteSavedScriptApiV1ScriptsScriptIdDelete({ |
| 726 | + path: { script_id: scriptIdToDelete } |
| 727 | + }); |
| 728 | + if (error) { |
| 729 | + console.error("Error deleting script:", error); |
712 | 730 | addToast("Failed to delete script.", "error"); |
713 | | - if (err?.status === 401) { |
| 731 | + if (response?.status === 401) { |
714 | 732 | handleLogout(); |
715 | 733 | } |
| 734 | + return; |
| 735 | + } |
| 736 | + addToast("Script deleted successfully.", "success"); |
| 737 | + if (get(currentScriptId) === scriptIdToDelete) { |
| 738 | + newScript(); |
716 | 739 | } |
| 740 | + await loadSavedScripts(); |
717 | 741 | } |
718 | 742 |
|
719 | 743 | function newScript() { |
|
0 commit comments