Skip to content

Commit 0f69be9

Browse files
Nakshatra SharmaNakshatra Sharma
authored andcommitted
Fix Tauri Verilog compilation: use HTTP plugin to bypass CORS and improve error handling
1 parent 5fb1a1b commit 0f69be9

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src-tauri/capabilities/default.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
},
1818
{
1919
"url": "https://circuitverse.org/api/v1/auth/signup"
20+
},
21+
{
22+
"url": "https://circuitverse.org/api/v1/simulator/verilogcv"
2023
}
2124
]
2225
},

src/simulator/src/Verilog2CV.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { showError, showMessage } from './utils'
3434
import { showProperties } from './ux'
3535
import { useSimulatorMobileStore } from '#/store/simulatorMobileStore'
3636
import { toRefs } from 'vue'
37+
import { isTauri } from '@tauri-apps/api/core'
3738

3839
var editor
3940
var verilogMode = false
@@ -257,15 +258,30 @@ export function YosysJSON2CV(
257258
}
258259
}
259260

260-
export default function generateVerilogCircuit(
261+
export default async function generateVerilogCircuit(
261262
verilogCode,
262263
scope = globalScope
263264
) {
264265
clearVerilogOutput()
265266
setVerilogOutput('Compiling Verilog code...', 'info')
266267

267268
var params = { code: verilogCode }
268-
fetch('/api/v1/simulator/verilogcv', {
269+
let apiUrl = '/api/v1/simulator/verilogcv'
270+
let fetchFn = window.fetch
271+
272+
if (isTauri()) {
273+
apiUrl = `https://circuitverse.org${apiUrl}`
274+
try {
275+
const http = await import('@tauri-apps/plugin-http')
276+
if (http && http.fetch) {
277+
fetchFn = http.fetch
278+
}
279+
} catch (error) {
280+
console.error('Failed to load Tauri HTTP plugin:', error)
281+
}
282+
}
283+
284+
fetchFn(apiUrl, {
269285
method: 'POST',
270286
headers: {
271287
'Content-Type': 'application/json',
@@ -276,6 +292,10 @@ export default function generateVerilogCircuit(
276292
if (!response.ok) {
277293
throw response
278294
}
295+
const contentType = response.headers.get('content-type')
296+
if (!contentType || !contentType.includes('application/json')) {
297+
throw new Error('Invalid response from server (not JSON)')
298+
}
279299
return response.json()
280300
})
281301
.then((circuitData) => {
@@ -297,14 +317,18 @@ export default function generateVerilogCircuit(
297317
setVerilogOutput('Verilog Circuit Successfully Created', 'success')
298318
})
299319
.catch((error) => {
320+
console.error('Verilog compilation error:', error)
321+
showError('Verilog compilation failed')
300322
if (error.status == 500) {
301-
showError('Could not connect to Yosys')
302323
setVerilogOutput('Could not connect to Yosys server', 'error')
303-
} else {
304-
showError('There is some issue with the code')
324+
} else if (typeof error.json === 'function') {
305325
error.json().then((errorMessage) => {
306-
setVerilogOutput(errorMessage.message, 'error')
326+
setVerilogOutput(errorMessage.message || 'Unknown error', 'error')
327+
}).catch(() => {
328+
setVerilogOutput('Error parsing error response', 'error')
307329
})
330+
} else {
331+
setVerilogOutput(error.message || 'Unknown error occurred', 'error')
308332
}
309333
})
310334
}

0 commit comments

Comments
 (0)