@@ -160,77 +160,6 @@ function loadExample() {
160160 }
161161}
162162
163- // Common Python errors and their user-friendly messages
164- const errorMessages = {
165- 'SyntaxError' : {
166- 'EOL while scanning string literal' : 'You have an unclosed string. Make sure all quotes are properly closed.' ,
167- 'unexpected EOF while parsing' : 'Your code is incomplete. Check for missing closing brackets or parentheses.' ,
168- 'invalid syntax' : 'There\'s a syntax error in your code. Check for typos or missing colons after statements like if, for, while, etc.' ,
169- 'invalid token' : 'There\'s an invalid character in your code. Check for special characters that aren\'t allowed in Python.'
170- } ,
171- 'IndentationError' : {
172- 'expected an indented block' : 'You need to indent the code block after a colon (:).' ,
173- 'unexpected indent' : 'You have an unexpected indentation. Make sure your indentation is consistent.'
174- } ,
175- 'NameError' : {
176- 'name' : 'You\'re using a variable that hasn\'t been defined. Check for typos or make sure to define the variable before using it.'
177- } ,
178- 'TypeError' : {
179- 'unsupported operand' : 'You\'re trying to perform an operation on incompatible types. Check the data types of your variables.' ,
180- 'can\'t multiply sequence by non-int' : 'You can only multiply sequences (like strings or lists) by integers.' ,
181- 'object is not subscriptable' : 'You\'re trying to use indexing [] on an object that doesn\'t support it.'
182- } ,
183- 'ZeroDivisionError' : {
184- 'division by zero' : 'You\'re trying to divide by zero, which is not allowed in mathematics.'
185- } ,
186- 'IndexError' : {
187- 'list index out of range' : 'You\'re trying to access an index that doesn\'t exist in your list. Remember, indexing starts at 0.'
188- } ,
189- 'KeyError' : {
190- '' : 'You\'re trying to access a key that doesn\'t exist in your dictionary.'
191- } ,
192- 'ImportError' : {
193- 'No module named' : 'You\'re trying to import a module that doesn\'t exist or isn\'t installed.'
194- } ,
195- 'ValueError' : {
196- 'invalid literal for int()' : 'You\'re trying to convert a string to an integer, but the string doesn\'t represent a valid number.'
197- } ,
198- 'FileNotFoundError' : {
199- 'No such file or directory' : 'The file you\'re trying to open doesn\'t exist or the path is incorrect.'
200- } ,
201- 'TimeoutError' : {
202- '' : 'Your code took too long to execute. Check for infinite loops or inefficient algorithms.'
203- }
204- } ;
205-
206- // Function to get a user-friendly error message
207- function getUserFriendlyErrorMessage ( errorText ) {
208- // Extract error type and details
209- const errorMatch = errorText . match ( / ( [ A - Z a - z ] + E r r o r ) : ( .* ?) (?: \n | $ ) / ) ;
210- if ( ! errorMatch ) return errorText ;
211-
212- const errorType = errorMatch [ 1 ] ;
213- const errorDetail = errorMatch [ 2 ] . trim ( ) ;
214-
215- // Check if we have a friendly message for this error type
216- if ( errorMessages [ errorType ] ) {
217- // Look for specific error details
218- for ( const [ pattern , message ] of Object . entries ( errorMessages [ errorType ] ) ) {
219- if ( pattern && errorDetail . includes ( pattern ) ) {
220- return `${ errorType } : ${ message } \n\nOriginal error: ${ errorDetail } ` ;
221- }
222- }
223-
224- // If no specific detail matched but we have a general message for the error type
225- if ( errorMessages [ errorType ] [ '' ] ) {
226- return `${ errorType } : ${ errorMessages [ errorType ] [ '' ] } ` ;
227- }
228- }
229-
230- // Return the original error if no friendly message is found
231- return errorText ;
232- }
233-
234163// Debounce mechanism
235164let isRunning = false ;
236165let lastRunTime = 0 ;
@@ -243,7 +172,6 @@ async function runCode() {
243172 return ;
244173 }
245174
246- // Prevent multiple rapid executions
247175 const now = Date . now ( ) ;
248176 if ( isRunning || ( now - lastRunTime < DEBOUNCE_TIME ) ) {
249177 console . log ( 'Execution already in progress or too soon after last execution, skipping' ) ;
@@ -256,125 +184,54 @@ async function runCode() {
256184 const code = editor . getValue ( ) ;
257185
258186 // Show running status
259- executionStatus . className = 'execution-status running' ;
260- statusMessage . textContent = 'Running code...' ;
261- output . textContent = '' ;
262- output . className = '' ;
187+ executionStatus . style . display = 'block' ;
188+ statusMessage . textContent = 'Running...' ;
189+ statusMessage . style . color = '#ffd700' ;
263190
264191 try {
265192 console . log ( 'Sending code to backend:' , code ) ;
266- const response = await axios . post ( `${ API_URL } /execute` , {
267- code : code
193+ const response = await fetch ( `${ API_URL } /execute` , {
194+ method : 'POST' ,
195+ headers : {
196+ 'Content-Type' : 'application/json' ,
197+ 'Accept' : 'application/json' ,
198+ 'Origin' : 'https://getgit789.github.io'
199+ } ,
200+ body : JSON . stringify ( { code : code } )
268201 } ) ;
269202
270- console . log ( 'Response received:' , response . data ) ;
271-
272- // Handle the response
273- let outputText = '' ;
274- let hasError = false ;
275-
276- // Reset output class immediately to ensure clean state
277- output . className = '' ;
278-
279- // Check if there's any output
280- if ( response . data . output && response . data . output . trim ( ) ) {
281- outputText += response . data . output ;
282- }
283-
284- // Check if there's any error
285- if ( response . data . error && response . data . error . trim ( ) ) {
286- hasError = true ;
287- if ( outputText ) outputText += '\n\n' ;
288- const friendlyError = getUserFriendlyErrorMessage ( response . data . error ) ;
289- outputText += 'Error:\n' + friendlyError ;
290-
291- // Update status to error
292- executionStatus . className = 'execution-status error' ;
293- statusMessage . textContent = 'Error in code execution' ;
294- }
295-
296- // Check exit code (if provided)
297- if ( response . data . exit_code !== undefined && response . data . exit_code !== 0 ) {
298- hasError = true ;
299- if ( ! outputText ) {
300- outputText = 'Program exited with code ' + response . data . exit_code ;
301- }
302-
303- // Update status to error if not already set
304- if ( ! executionStatus . classList . contains ( 'error' ) ) {
305- executionStatus . className = 'execution-status error' ;
306- statusMessage . textContent = 'Error in code execution' ;
307- }
308- }
309-
310- // If no output or error
311- if ( ! outputText ) {
312- outputText = 'Program executed successfully with no output' ;
313- }
314-
315- // This block is now handled in the output class setting section
316-
317- output . textContent = outputText ;
318-
319- console . log ( 'Has error:' , hasError , 'Output text:' , outputText ) ;
320-
321- // Only set a class if there's an error, otherwise clear the class
322- if ( hasError ) {
323- output . className = 'error' ;
324- } else {
325- output . className = '' ;
326- }
203+ const data = await response . json ( ) ;
204+ console . log ( 'Response received:' , data ) ;
327205
328- // Clear the execution status error if we're showing success output
329- if ( hasError ) {
330- executionStatus . className = 'execution-status error' ;
331- statusMessage . textContent = 'Error in code execution' ;
332-
333- // Hide error status after 3 seconds
334- setTimeout ( ( ) => {
335- if ( executionStatus . classList . contains ( 'error' ) ) {
336- executionStatus . className = 'execution-status hidden' ;
337- }
338- } , 3000 ) ;
206+ if ( data . error ) {
207+ output . textContent = data . error ;
208+ output . style . color = '#ff6b6b' ;
209+ statusMessage . textContent = 'Error' ;
210+ statusMessage . style . color = '#ff6b6b' ;
339211 } else {
340- executionStatus . className = 'execution-status success' ;
341- statusMessage . textContent = 'Code executed successfully!' ;
342-
343- // Hide success status after 3 seconds
344- setTimeout ( ( ) => {
345- if ( executionStatus . classList . contains ( 'success' ) ) {
346- executionStatus . className = 'execution-status hidden' ;
347- }
348- } , 3000 ) ;
212+ output . textContent = data . output || 'No output' ;
213+ output . style . color = '#98c379' ;
214+ statusMessage . textContent = 'Success' ;
215+ statusMessage . style . color = '#98c379' ;
349216 }
350217 } catch ( error ) {
351- console . error ( 'Error executing code:' , error ) ;
352-
353- // Update status to error
354- executionStatus . className = 'execution-status error' ;
355- statusMessage . textContent = 'Error connecting to server' ;
356-
357- // Display error
358- const errorMessage = error . response ?. data ?. error || error . message || 'Unknown error' ;
359- output . textContent = 'Error connecting to server: ' + errorMessage ;
360- output . className = 'error' ;
361-
362- // Hide error status after 3 seconds
363- setTimeout ( ( ) => {
364- if ( executionStatus . classList . contains ( 'error' ) ) {
365- executionStatus . className = 'execution-status hidden' ;
366- }
367- } , 3000 ) ;
218+ console . error ( 'Error:' , error ) ;
219+ output . textContent = error . message || 'Unknown error' ;
220+ output . style . color = '#ff6b6b' ;
221+ statusMessage . textContent = 'Error' ;
222+ statusMessage . style . color = '#ff6b6b' ;
368223 } finally {
369- // Reset running flag
370224 isRunning = false ;
225+ setTimeout ( ( ) => {
226+ executionStatus . style . display = 'none' ;
227+ } , 2000 ) ;
371228 }
372229}
373230
374231// Clear output function
375232function clearOutput ( ) {
376233 output . textContent = '' ;
377- output . classList . remove ( 'error' ) ;
234+ output . style . color = '' ;
378235}
379236
380237// Copy code function
0 commit comments