Skip to content

Commit

Permalink
Merge pull request #26 from torkildr/master
Browse files Browse the repository at this point in the history
Fix error propagation for `xapi.config.set`
  • Loading branch information
myme committed Dec 14, 2018
2 parents 5faf6d1 + e2797e0 commit 0ebb6ac
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/xapi/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ function assertValidCommandResponse(response) {
case 'Error': {
const body = collapse(root);
const { Error, Reason, XPath } = body;
const reason = Error || Reason || keys[0];

if (XPath) {
throw new InvalidPathError(Reason, XPath);
throw new InvalidPathError(reason, XPath);
}
const reason = Error || Reason || keys[0];

throw new XAPIError(UNKNOWN_ERROR, reason, body);
}
case 'ParameterError':
Expand Down Expand Up @@ -165,6 +167,10 @@ export function createGetResponse(request, response) {


export function createSetResponse(request, response) {
if ({}.hasOwnProperty.call(response, 'CommandResponse')) {
assertValidCommandResponse(response);
}

if (Object.keys(response).length > 1) {
const leaf = digObj(request.params.Path, response);
if (leaf.error === 'True') {
Expand Down
117 changes: 117 additions & 0 deletions test/xapi/rpc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createCommandResponse,
createGetResponse,
createRequest,
createSetResponse,
} from '../../src/xapi/rpc';

import {
Expand Down Expand Up @@ -224,4 +225,120 @@ describe('xapi/rpc', () => {
expect(result).to.not.exist(result);
});
});

describe('createSetResponse', () => {
it('handles Reason error', () => {
const request = {
jsonrpc: '2.0',
method: 'xSet',
id: '1',
params: {
Path: [
'Configuration',
'Video',
'Output',
'Connector',
99,
'MonitorRole',
],
Value: 'foo',
},
};
const response = JSON.parse(`
{
"CommandResponse":{
"Configuration":{
"status":"Error",
"Reason":{
"Value":"No match on address expression."
},
"XPath":{
"Value":"Configuration/Video/Output/Connector[99]/MonitorRole"
}
}
},
"ResultId":"1"
}
`);

expect(() => createSetResponse(request, response))
.to.throw(XAPIError, 'No match on address expression');
});

it('handles Error error', () => {
const request = {
jsonrpc: '2.0',
method: 'xSet',
id: '1',
params: {
Path: [
'Configuration',
'Video',
'Output',
'Connector',
99,
'MonitorRole',
],
Value: 'foo',
},
};
const response = JSON.parse(`
{
"CommandResponse":{
"Configuration":{
"status":"Error",
"Error":{
"Value":"No match on address expression."
},
"XPath":{
"Value":"Configuration/Video/Output/Connector[99]/MonitorRole"
}
}
},
"ResultId":"1"
}
`);

expect(() => createSetResponse(request, response))
.to.throw(XAPIError, 'No match on address expression');
});

it('handles unknown error', () => {
const request = {
jsonrpc: '2.0',
method: 'xSet',
id: '1',
params: {
Path: [
'Configuration',
'Video',
'Output',
'Connector',
99,
'MonitorRole',
],
Value: 'foo',
},
};
const response = JSON.parse(`
{
"CommandResponse":{
"Configuration":{
"status":"Error",
"ThisIsNotKnown":{
"Value":"No match on address expression."
},
"XPath":{
"Value":"Configuration/Video/Output/Connector[99]/MonitorRole"
}
}
},
"ResultId":"1"
}
`);

expect(() => createSetResponse(request, response))
.to.throw(XAPIError, 'Configuration');
});
});
});

0 comments on commit 0ebb6ac

Please sign in to comment.