Skip to content

Commit

Permalink
Handle errors in program text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
wkwiatek committed Dec 9, 2024
1 parent b298c54 commit 3dd2e1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const DebuggerContent = () => {
{!!program.length && (
<>
{isProgramEditMode && (
<div className="border-2 rounded-md h-full p-2 pt-8">
<div className="border-2 rounded-md h-full p-2">
{instructionMode === InstructionMode.ASM ? (
<Assembly
program={program}
Expand All @@ -106,13 +106,6 @@ const DebuggerContent = () => {
name: "custom",
});
}
// onProgramLoad({ initial: initialState, program, name: "custom" }, error);
// if (program) {
// setTempProgram(program);
// onProgramLoad({ initial, program, name: "custom" }, error);
// } else {
// onProgramLoad(undefined, error);
// }
}}
/>
)}
Expand Down
27 changes: 24 additions & 3 deletions src/components/ProgramTextLoader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const ProgramTextLoader = ({
}, [program]);

const [programInput, setProgramInput] = useState(defaultProgram?.length ? JSON.stringify(defaultProgram) : "");
const [programError, setProgramError] = useState("");
const isProgramInvalid = useAppSelector(selectIsProgramInvalid);

const handleOnChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
Expand All @@ -27,18 +28,38 @@ export const ProgramTextLoader = ({
if (!newInput.startsWith("[")) {
try {
const parsedBlob = bytes.BytesBlob.parseBlob(newInput);
setProgram(Array.prototype.slice.call(parsedBlob.raw));
} catch (error) {

const parsedBlobArray = Array.prototype.slice.call(parsedBlob.raw);

if (parsedBlobArray.length) {
setProgram(parsedBlobArray);
}

setProgramError("");
} catch (error: unknown) {
logger.error("Wrong binary file", { error, hideToast: true });

setProgram(undefined, "Wrong binary file");

if (error instanceof Error) {
if (error?.message) {
setProgramError(error.message);
}
}
}
} else {
try {
JSON.parse(newInput);
setProgram(JSON.parse(newInput));
setProgramError("");
} catch (error) {
logger.error("Wrong JSON", { error, hideToast: true });

setProgram(undefined, "Wrong JSON");

if (error) {
setProgramError(error.toString());
}
}
}
};
Expand All @@ -59,7 +80,7 @@ export const ProgramTextLoader = ({
value={programInput}
onChange={handleOnChange}
/>
{isProgramInvalid && <span className="text-red-500">Program is not valid</span>}
{isProgramInvalid && <span className="text-red-500">{programError || "Program is not valid"}</span>}
</div>
</div>
);
Expand Down

0 comments on commit 3dd2e1b

Please sign in to comment.