Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): Log tracebacks from failed RPC calls #1846

Merged
merged 4 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions api/opentrons/server/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,11 @@ async def make_call(self, func, token):
line_no = 'unknown'
finally:
response['$']['status'] = 'error'
call_result = '{0} [line {1}]: {2}'.format(
e.__class__.__name__, line_no, str(e))
call_result = {
'message': '{0} [line {1}]: {2}'.format(
e.__class__.__name__, line_no, str(e)),
'traceback': traceback.format_exc()
}
finally:
response['data'] = call_result
return response
Expand Down
15 changes: 9 additions & 6 deletions api/tests/opentrons/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,16 @@ async def test_exception_on_call(session, root):

await session.socket.receive_json() # Skip ack
res = await session.socket.receive_json() # Get call result
expected = {'$': {
'token': session.token,
'status': 'error',
'type': rpc.CALL_RESULT_MESSAGE},
'data': 'Exception [line unknown]: Kaboom!'}
expectedMeta = {
'token': session.token,
'status': 'error',
'type': rpc.CALL_RESULT_MESSAGE
}
expectedMessage = 'Exception [line unknown]: Kaboom!'

assert res == expected
assert res['$'] == expectedMeta
assert res['data']['message'] == expectedMessage
assert isinstance(res['data']['traceback'], str)


@pytest.mark.parametrize('root', [Foo(0)])
Expand Down
19 changes: 18 additions & 1 deletion app/src/robot/api-client/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
// robot api client redux middleware
// wraps the api client worker to handle API side effects in a different thread

import createLogger from '../../logger'
import Worker from './worker'

const log = createLogger(__filename)

export default function apiClientMiddleware () {
const worker = new Worker()

return (store) => {
const {getState, dispatch} = store

worker.onmessage = function handleWorkerMessage (event) {
dispatch(event.data)
const action = event.data

// log error actions
if (action && action.payload) {
const error = action.error === true
? action.payload
: action.payload.error

if (error) {
log.warn('Error response from robot', {action})
if (error.traceback) log.warn(error.traceback)
}
}

dispatch(action)
}

// initialize worker
Expand Down
10 changes: 7 additions & 3 deletions app/src/rpc/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ class RpcContext extends EventEmitter {
return new Promise((resolve, reject) => {
let timeout

const handleError = (reason) => {
const handleError = (reason, traceback) => {
cleanup()
reject(new RemoteError(reason, name, args))
reject(new RemoteError(reason, name, args, traceback))
}

const handleFailure = (res) => {
if (typeof res === 'string') return handleError(res)
handleError(res.message, res.traceback)
}

const handleFailure = (result) => handleError(result)
const handleNack = (reason) => handleError(`Received NACK with ${reason}`)

const handleAck = () => {
Expand Down
3 changes: 2 additions & 1 deletion app/src/rpc/remote-error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// remote call error object
export default class RemoteError extends Error {
constructor (message, methodName, args) {
constructor (message, methodName, args, traceback) {
super(message)
this.name = this.constructor.name
this.methodName = methodName
this.args = args
this.traceback = traceback
}
}
19 changes: 18 additions & 1 deletion app/src/rpc/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('rpc client', () => {
})
})

test('rejects if call is unsuccessful', () => {
test('rejects if call is unsuccessful (string response)', () => {
sendControlAndResolveRemote((message) => {
const token = message.$.token
const ack = makeAckResponse(token)
Expand All @@ -222,6 +222,23 @@ describe('rpc client', () => {
message: expect.stringMatching(/ahhh/)
})
})

test('rejects if call is unsuccessful (object response)', () => {
sendControlAndResolveRemote((message) => {
const token = message.$.token
const ack = makeAckResponse(token)
const result = makeCallResponse(token, FAILURE, {message: 'ahhh'})
setTimeout(() => ws.send(ack), 1)
setTimeout(() => ws.send(result), 5)
})

const call = Client(url)
.then((client) => client.callRemote(id, name, args))

return expect(call).rejects.toMatchObject({
message: expect.stringMatching(/ahhh/)
})
})
})

describe('resolveTypeValues', () => {
Expand Down