Skip to content

Commit

Permalink
Merge pull request #24 from FlowFuse/23-better-response-notifications
Browse files Browse the repository at this point in the history
Improved messaging for error responses
  • Loading branch information
Steve-Mcl authored Jul 16, 2024
2 parents 91d5229 + d2706d6 commit 7cb17d6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
36 changes: 36 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,42 @@

function processAIErrorResponse (jqXHR, textStatus, errorThrown) {
console.warn('error', jqXHR, textStatus, errorThrown)
if (jqXHR.status === 429) {
// get x- rate limit reset header
const reset = jqXHR.getResponseHeader('x-ratelimit-reset')
if (reset) {
const resetTime = new Date(reset * 1000)
const now = new Date()
const diff = resetTime - now
const seconds = Math.floor(diff / 1000)
const minutes = Math.floor(seconds / 60)
const remainingSeconds = seconds % 60
if (minutes > 0) {
RED.notify(`Sorry, the FlowFuse Assistant is busy. Please try again in ${minutes} minute${minutes > 1 ? 's' : ''}.`, 'warning')
} else {
RED.notify(`Sorry, the FlowFuse Assistant is busy. Please try again in ${remainingSeconds} second${remainingSeconds > 1 ? 's' : ''}.`, 'warning')
}
return
}
RED.notify('Sorry, the FlowFuse Assistant is busy. Please try again later.', 'warning')
return
}
if (jqXHR.status === 404) {
RED.notify('Sorry, the FlowFuse Assistant is not available at the moment', 'warning')
return
}
if (jqXHR.status === 401) {
RED.notify('Sorry, you are not authorised to use the FlowFuse Assistant', 'warning')
return
}
if (jqXHR.status >= 400 && jqXHR.status < 500) {
let message = 'Sorry, the FlowFuse Assistant cannot help with this request'
if (jqXHR.responseJSON?.body?.code === 'assistant_service_denied' && jqXHR.responseJSON?.body?.error) {
message = jqXHR.responseJSON.body.error
}
RED.notify(message, 'warning')
return
}
RED.notify('Sorry, something went wrong, please try again', 'error')
}

Expand Down
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,23 @@ module.exports = (RED) => {
data
})
}).catch((error) => {
const errorData = { status: 'error', message: 'Request to FlowFuse Assistant was unsuccessful', body: error.response?.body }
let body = error.response?.body
if (typeof body === 'string') {
try {
body = JSON.parse(body)
} catch (e) {
// ignore
}
}
let message = 'FlowFuse Assistant request was unsuccessful'
const errorData = { status: 'error', message, body }
const errorCode = error.response?.statusCode || 500
res.status(errorCode).json({ status: 'error', message: errorData })
console.warn('nr-assistant error:', error)
RED.log.warn('FlowFuse Assistant request was unsuccessful')
res.status(errorCode).json(errorData)
RED.log.trace('nr-assistant error:', error)
if (body && typeof body === 'object' && body.error) {
message = `${message}: ${body.error}`
}
RED.log.warn(message)
})
})
}
Expand Down

0 comments on commit 7cb17d6

Please sign in to comment.