diff --git a/src/Script/RamCalculationErrorCodes.ts b/src/Script/RamCalculationErrorCodes.ts index 17255506b..181f5617e 100644 --- a/src/Script/RamCalculationErrorCodes.ts +++ b/src/Script/RamCalculationErrorCodes.ts @@ -2,4 +2,5 @@ export enum RamCalculationErrorCode { SyntaxError = -1, ImportError = -2, + InvalidServer = -3, } diff --git a/src/ScriptEditor/ui/ScriptEditorContext.tsx b/src/ScriptEditor/ui/ScriptEditorContext.tsx index 5819740e1..92a290e5d 100644 --- a/src/ScriptEditor/ui/ScriptEditorContext.tsx +++ b/src/ScriptEditor/ui/ScriptEditorContext.tsx @@ -45,6 +45,9 @@ export function ScriptEditorContextProvider({ children }: { children: React.Reac case RamCalculationErrorCode.ImportError: errorType = "Import Error"; break; + case RamCalculationErrorCode.InvalidServer: + errorType = "Invalid server"; + break; default: errorType = "Unknown Error"; break; diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 4e860bec6..6782f0650 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -180,14 +180,6 @@ function Root(props: IProps): React.ReactElement { let decorations: monaco.editor.IEditorDecorationsCollection | undefined; - // Prevent Crash if script is open on deleted server - for (let i = openScripts.length - 1; i >= 0; i--) { - GetServer(openScripts[i].hostname) === null && openScripts.splice(i, 1); - } - if (currentScript && GetServer(currentScript.hostname) === null) { - currentScript = openScripts[0] ?? null; - } - const save = useCallback(() => { if (currentScript === null) { console.error("currentScript is null when it shouldn't be. Unable to save script"); @@ -304,10 +296,17 @@ function Root(props: IProps): React.ReactElement { const debouncedCodeParsing = debounce((newCode: string) => { let server; - if (!currentScript || !hasScriptExtension(currentScript.path) || !(server = GetServer(currentScript.hostname))) { + if (!currentScript || !hasScriptExtension(currentScript.path)) { showRAMError(); return; } + if (!(server = GetServer(currentScript.hostname))) { + showRAMError({ + errorCode: RamCalculationErrorCode.InvalidServer, + errorMessage: `Server ${currentScript.hostname} does not exist`, + }); + return; + } let ast; try { ast = parseAST(currentScript.path, currentScript.hostname, newCode, getFileType(currentScript.path)); @@ -398,7 +397,8 @@ function Root(props: IProps): React.ReactElement { function saveScript(scriptToSave: OpenScript): void { const server = GetServer(scriptToSave.hostname); if (!server) { - throw new Error("Server should not be null but it is."); + dialogBoxCreate(`Server ${scriptToSave.hostname} does not exist.`); + return; } // Show a warning message if the file is on a non-home server. if (scriptToSave.hostname !== SpecialServers.Home) { @@ -478,7 +478,7 @@ function Root(props: IProps): React.ReactElement { const indexOffset = openScripts.length === index ? -1 : 0; currentScript = openScripts[index + indexOffset]; if (editorRef.current !== null) { - if (currentScript.model.isDisposed() || !currentScript.model) { + if (!currentScript.model || currentScript.model.isDisposed()) { currentScript.regenerateModel(); } editorRef.current.setModel(currentScript.model); diff --git a/src/ScriptEditor/ui/utils.ts b/src/ScriptEditor/ui/utils.ts index 30b879b53..8d1145a95 100644 --- a/src/ScriptEditor/ui/utils.ts +++ b/src/ScriptEditor/ui/utils.ts @@ -7,7 +7,9 @@ import { throwIfReachable } from "../../utils/helpers/throwIfReachable"; function getServerCode(scripts: OpenScript[], index: number): string | null { const openScript = scripts[index]; const server = GetServer(openScript.hostname); - if (server === null) throw new Error(`Server '${openScript.hostname}' should not be null, but it is.`); + if (server === null) { + return null; + } const data = server.getContentFile(openScript.path)?.content ?? null; return data; }