Skip to content

Commit

Permalink
Merge pull request #55 from mcneel/luis/errors
Browse files Browse the repository at this point in the history
added error handling to solve and index routes
  • Loading branch information
fraguada authored Oct 13, 2020
2 parents 2838737 + fcf6ef9 commit bcf34e4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
32 changes: 22 additions & 10 deletions src/example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})


}

/**
Expand Down
21 changes: 14 additions & 7 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
81 changes: 40 additions & 41 deletions src/routes/solve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

Expand Down

0 comments on commit bcf34e4

Please sign in to comment.