Improve compile error handling.

This commit is contained in:
muzam1l
2022-07-13 16:50:47 +05:30
parent fc461ddd0d
commit c26c7c13d1
4 changed files with 30 additions and 13 deletions

View File

@@ -14,7 +14,7 @@ import { ref } from "valtio";
*/
export const compileCode = async (activeId: number) => {
// Save the file to global state
saveFile(false);
saveFile(false, activeId);
if (!process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT) {
throw Error("Missing env!");
}
@@ -26,7 +26,9 @@ export const compileCode = async (activeId: number) => {
// Set loading state to true
state.compiling = true;
state.logs = []
const file = state.files[activeId]
try {
file.containsErrors = false
const res = await fetch(process.env.NEXT_PUBLIC_COMPILE_API_ENDPOINT, {
method: "POST",
headers: {
@@ -40,8 +42,8 @@ export const compileCode = async (activeId: number) => {
{
type: "c",
options: state.compileOptions.optimizationLevel || '-O2',
name: state.files[activeId].name,
src: state.files[activeId].content,
name: file.name,
src: file.content,
},
],
}),
@@ -49,15 +51,15 @@ export const compileCode = async (activeId: number) => {
const json = await res.json();
state.compiling = false;
if (!json.success) {
state.logs.push({ type: "error", message: json.message });
const errors = [json.message]
if (json.tasks && json.tasks.length > 0) {
json.tasks.forEach((task: any) => {
if (!task.success) {
state.logs.push({ type: "error", message: task?.console });
errors.push(task?.console)
}
});
}
return toast.error(`Couldn't compile!`, { position: "bottom-center" });
throw errors
}
state.logs.push({
type: "success",
@@ -67,7 +69,6 @@ export const compileCode = async (activeId: number) => {
});
// Decode base64 encoded wasm that is coming back from the endpoint
const bufferData = await decodeBinary(json.output);
const file = state.files[state.active]
file.compiledContent = ref(bufferData);
file.lastCompiled = new Date();
file.compiledValueSnapshot = file.content
@@ -86,10 +87,23 @@ export const compileCode = async (activeId: number) => {
});
} catch (err) {
console.log(err);
state.logs.push({
type: "error",
message: "Error occured while compiling!",
});
if (err instanceof Array && typeof err[0] === 'string') {
err.forEach(message => {
state.logs.push({
type: "error",
message,
});
})
}
else {
state.logs.push({
type: "error",
message: "Something went wrong, check your connection try again later!",
});
}
state.compiling = false;
toast.error(`Error occured while compiling!`, { position: "bottom-center" });
file.containsErrors = true
}
};

View File

@@ -2,14 +2,15 @@ import toast from "react-hot-toast";
import state from '../index';
// Saves the current editor content to global state
export const saveFile = (showToast: boolean = true) => {
export const saveFile = (showToast: boolean = true, activeId?: number) => {
const editorModels = state.editorCtx?.getModels();
const sought = '/' + state.files[state.active].name;
const currentModel = editorModels?.find((editorModel) => {
return editorModel.uri.path.endsWith(sought);
});
const file = state.files[activeId || state.active]
if (state.files.length > 0) {
state.files[state.active].content = currentModel?.getValue() || "";
file.content = currentModel?.getValue() || "";
}
if (showToast) {
toast.success("Saved successfully", { position: "bottom-center" });

View File

@@ -17,6 +17,7 @@ export interface IFile {
compiledContent?: ArrayBuffer | null;
compiledWatContent?: string | null;
lastCompiled?: Date
containsErrors?: boolean
}
export interface FaucetAccountRes {