From fcf6ef9162406311cc41969ff3c5d067a58bf152 Mon Sep 17 00:00:00 2001 From: Luis Fraguada Date: Tue, 13 Oct 2020 15:27:11 +0200 Subject: [PATCH] added error handling to solve and index routes also handling this in example --- src/app.js | 1 - src/example/app.js | 32 ++++++++++++------ src/routes/index.js | 21 ++++++++---- src/routes/solve.js | 81 ++++++++++++++++++++++----------------------- 4 files changed, 76 insertions(+), 59 deletions(-) diff --git a/src/app.js b/src/app.js index 4102bc31..5f92e69b 100644 --- a/src/app.js +++ b/src/app.js @@ -37,7 +37,6 @@ app.use('/', require('./routes/index')) app.use('/definition', require('./routes/definition')) app.use('/solve', require('./routes/solve')) - // catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)) diff --git a/src/example/app.js b/src/example/app.js index c94f4021..0edd1cfe 100644 --- a/src/example/app.js +++ b/src/example/app.js @@ -38,18 +38,25 @@ async function compute(){ 'headers': {'Content-Type': 'application/json'} } + let headers = null + + await fetch(url, request).then( (response) => { + if(!response.ok) + throw new Error(response.statusText) + else { + headers = response.headers.get('server-timing') + return response.json() + } + }).then( (responseJson) => { - let response = await fetch(url, request) - - // Request finished. Do processing here. - let t1 = performance.now() - const computeSolveTime = t1 - timeComputeStart - t0 = t1 + // Request finished. Do processing here. + let t1 = performance.now() + const computeSolveTime = t1 - timeComputeStart + t0 = t1 - let responseJson = await response.json() - let headers = response.headers.get('server-timing') + //let responseJson = await response.json() + //let headers = response.headers.get('server-timing') - { // hide spinner document.getElementById('loader').style.display = 'none' let data = JSON.parse(responseJson.values[0].InnerTree['{ 0; }'][0].data) @@ -86,7 +93,12 @@ async function compute(){ console.log(` .. ${Math.round(computeSolveTime - sum)} ms: local<->appserver network latency`) console.log(` ${Math.round(decodeMeshTime)} ms: decode json to rhino3dm mesh`) console.log(` ${Math.round(rebuildSceneTime)} ms: create threejs mesh and insert in scene`) - } + + }).catch( (error) => { + console.error(error) + }) + + } /** diff --git a/src/routes/index.js b/src/routes/index.js index 781e2526..9d80f2f3 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -34,7 +34,16 @@ router.get('/:name', computeParams, function(req, res, next){ let fullUrl = req.protocol + '://' + req.get('host') let definitionPath = `${fullUrl}/definition/${definition.id}` - compute.computeFetch('io', {'pointer':definitionPath}).then(result => { + compute.computeFetch('io', {'pointer':definitionPath}, false).then( (response) => { + + // Throw error if response not ok + if(!response.ok) { + throw new Error(response.statusText) + } else { + return response.json() + } + + }).then( (result) => { let inputs = result.Inputs === undefined ? result.InputNames : result.Inputs let outputs = result.Outputs === undefined ? result.OutputNames: result.Outputs @@ -45,12 +54,10 @@ router.get('/:name', computeParams, function(req, res, next){ definition.inputs = inputs definition.outputs = outputs - res.setHeader('Content-Type', 'application/json') - res.send(JSON.stringify(data)) - - }).catch( (error) => console.log(error)) - - + res.json(data) + }).catch( (error) => { + next(error) + }) } else { data.inputs = definition.inputs data.outputs = definition.outputs diff --git a/src/routes/solve.js b/src/routes/solve.js index e029549c..3c8c3ca3 100644 --- a/src/routes/solve.js +++ b/src/routes/solve.js @@ -133,48 +133,47 @@ function commonSolve (req, res, next){ let computeServerTiming = null // call compute server - compute.Grasshopper.evaluateDefinition(definitionPath, trees, false) - .then(computeResponse => { - /* - TODO: Catch server errors from compute - if(computeResponse.status === 500) { - throw new Error(computeResponse.statusText) - } - */ - computeServerTiming = computeResponse.headers - computeResponse.text().then(result=> { - - const timeComputeServerCallComplete = performance.now() - - let computeTimings = computeServerTiming.get('server-timing') - let sum = 0 - computeTimings.split(',').forEach(element => { - let t = element.split('=')[1].trim() - sum += Number(t) - }) - const timespanCompute = timeComputeServerCallComplete - timePreComputeServerCall - const timespanComputeNetwork = Math.round(timespanCompute - sum) - const timespanSetup = Math.round(timePreComputeServerCall - timePostStart) - const timing = `setup;dur=${timespanSetup}, ${computeTimings}, network;dur=${timespanComputeNetwork}` - - if(mc !== null) { - //set memcached - mc.set(res.locals.cacheKey, result, {expires:0}, function(err, val){ - console.log(err) - console.log(val) - }) - } else { - //set node-cache - cache.set(res.locals.cacheKey, result) - } - - res.setHeader('Server-Timing', timing) - res.send(result) - }).catch( (error) => { - console.log(error) - res.send('error in solve') - }) + compute.Grasshopper.evaluateDefinition(definitionPath, trees, false).then( (response) => { + + // Throw error if response not ok + if(!response.ok) { + throw new Error(response.statusText) + } else { + computeServerTiming = response.headers + return response.text() + } + + }).then( (result) => { + + const timeComputeServerCallComplete = performance.now() + + let computeTimings = computeServerTiming.get('server-timing') + let sum = 0 + computeTimings.split(',').forEach(element => { + let t = element.split('=')[1].trim() + sum += Number(t) }) + const timespanCompute = timeComputeServerCallComplete - timePreComputeServerCall + const timespanComputeNetwork = Math.round(timespanCompute - sum) + const timespanSetup = Math.round(timePreComputeServerCall - timePostStart) + const timing = `setup;dur=${timespanSetup}, ${computeTimings}, network;dur=${timespanComputeNetwork}` + + if(mc !== null) { + //set memcached + mc.set(res.locals.cacheKey, result, {expires:0}, function(err, val){ + console.log(err) + console.log(val) + }) + } else { + //set node-cache + cache.set(res.locals.cacheKey, result) + } + + res.setHeader('Server-Timing', timing) + res.send(result) + }).catch( (error) => { + next(error) + }) } }