Skip to content

Commit

Permalink
Moved to thrown errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdp committed Feb 14, 2025
1 parent 83d21ee commit 50778d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 73 deletions.
71 changes: 18 additions & 53 deletions src/RokuECP.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { describe } from 'mocha';
import type { EcpAppStateData, EcpRegistryData } from './RokuECP';
import { AppState, EcpStatus, rokuECP } from './RokuECP';
import { util } from './util';
import { expectThrowsAsync } from './testHelpers.spec';

const sinon = createSandbox();


describe('RokuECP', () => {
describe.only('RokuECP', () => {

Check failure on line 12 in src/RokuECP.spec.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

describe.only not permitted

beforeEach(() => {
sinon.restore();
Expand Down Expand Up @@ -90,6 +91,7 @@ describe('RokuECP', () => {
body: '',
statusCode: 200
});
sinon.stub(rokuECP as any, 'processRegistry').resolves({});

await rokuECP.getRegistry(options);
expect(stub.getCall(0).args).to.eql(['query/registry/dev', options]);
Expand Down Expand Up @@ -214,11 +216,7 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processRegistry'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'Plugin dev not found'
} as EcpRegistryData);
await expectThrowsAsync(() => rokuECP['processRegistry'](response as any), 'Plugin dev not found');
});

it('handles device not keyed', async () => {
Expand All @@ -231,11 +229,7 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processRegistry'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'Device not keyed'
} as EcpRegistryData);
await expectThrowsAsync(() => rokuECP['processRegistry'](response as any), 'Device not keyed');
});

it('handles failed status with missing error', async () => {
Expand All @@ -247,23 +241,15 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processRegistry'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'Unknown error'
} as EcpRegistryData);
await expectThrowsAsync(() => rokuECP['processRegistry'](response as any), 'Unknown error');
});

it('handles error response without xml', async () => {
let response = {
body: `ECP command not allowed in Limited mode.`,
statusCode: 403
};
let result = await rokuECP['processRegistry'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'ECP command not allowed in Limited mode.'
} as EcpRegistryData);
await expectThrowsAsync(() => rokuECP['processRegistry'](response as any), 'ECP command not allowed in Limited mode.');
});
});
});
Expand All @@ -281,12 +267,14 @@ describe('RokuECP', () => {
statusCode: 200
});

sinon.stub(rokuECP as any, 'processAppState').resolves({});

await rokuECP.getAppState(options);
expect(stub.getCall(0).args).to.eql(['query/app-status/dev', options]);
});
});

describe('parseAppState', () => {
describe('processAppState', () => {
describe('non-error responses', () => {
it('handles ok response', async () => {
let response = {
Expand Down Expand Up @@ -377,11 +365,7 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processAppState'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'Unknown error'
} as EcpAppStateData);
await expectThrowsAsync(() => rokuECP['processAppState'](response as any), 'Unknown error');
});

it('handles failed status with populated error', async () => {
Expand All @@ -394,23 +378,15 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processAppState'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'App not found'
} as EcpAppStateData);
await expectThrowsAsync(() => rokuECP['processAppState'](response as any), 'App not found');
});

it('handles error response without xml', async () => {
let response = {
body: `ECP command not allowed in Limited mode.`,
statusCode: 403
};
let result = await rokuECP['processAppState'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'ECP command not allowed in Limited mode.'
} as EcpAppStateData);
await expectThrowsAsync(() => rokuECP['processAppState'](response as any), 'ECP command not allowed in Limited mode.');
});
});
});
Expand All @@ -427,13 +403,14 @@ describe('RokuECP', () => {
body: '',
statusCode: 200
});
sinon.stub(rokuECP as any, 'processExitApp').resolves({});

await rokuECP.exitApp(options);
expect(stub.getCall(0).args).to.eql(['exit-app/dev', options]);
});
});

describe('parseExitApp', () => {
describe('processExitApp', () => {
describe('non-error responses', () => {
it('handles ok response', async () => {
let response = {
Expand Down Expand Up @@ -462,11 +439,7 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processExitApp'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'Unknown error'
});
await expectThrowsAsync(() => rokuECP['processExitApp'](response as any), 'Unknown error');
});

it('handles failed status with populated error', async () => {
Expand All @@ -479,23 +452,15 @@ describe('RokuECP', () => {
`,
statusCode: 200
};
let result = await rokuECP['processExitApp'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'App not found'
});
await expectThrowsAsync(() => rokuECP['processExitApp'](response as any), 'App not found');
});

it('handles error response without xml', async () => {
let response = {
body: `ECP command not allowed in Limited mode.`,
statusCode: 403
};
let result = await rokuECP['processExitApp'](response as any);
expect(result).to.eql({
status: EcpStatus.failed,
errorMessage: 'ECP command not allowed in Limited mode.'
});
await expectThrowsAsync(() => rokuECP['processExitApp'](response as any), 'ECP command not allowed in Limited mode.');
});
});
});
Expand Down
26 changes: 11 additions & 15 deletions src/RokuECP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ export class RokuECP {
return EcpStatus[response?.[rootKey]?.status?.[0]?.toLowerCase()] ?? EcpStatus.failed;
}

private async parseResponse<R>(response: Response, rootKey: string, callback: (parsed: any, status: EcpStatus) => R): Promise<R | { status: EcpStatus; errorMessage: string }> {
private async parseResponse<R>(response: Response, rootKey: string, callback: (parsed: any, status: EcpStatus) => R): Promise<R> {
if (typeof response.body === 'string') {
let parsed: ParsedEcpRoot;
try {
let parsed = await util.parseXml<ParsedEcpRoot>(response.body);
const status = this.getEcpStatus(parsed, rootKey);
if (status === EcpStatus.ok) {
return callback(parsed?.[rootKey], status);
} else {
return {
status: status,
errorMessage: parsed?.[rootKey]?.error?.[0] ?? 'Unknown error'
};
}
parsed = await util.parseXml<ParsedEcpRoot>(response.body);
} catch {
//if the response is not xml, just return the body as-is
return {
status: EcpStatus.failed,
errorMessage: response.body ?? 'Unknown error'
};
throw new Error(response.body ?? 'Unknown error');
}

const status = this.getEcpStatus(parsed, rootKey);
if (status === EcpStatus.ok) {
return callback(parsed?.[rootKey], status);
} else {
throw new Error(parsed?.[rootKey]?.error?.[0] ?? 'Unknown error');
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/debugSession/ecpRegistryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import type { RokuEcpParam } from '../RokuECP';
import type { AugmentedVariable } from './BrightScriptDebugSession';

export async function populateVariableFromRegistryEcp(options: RokuEcpParam<'getRegistry'>, v: AugmentedVariable, variables: Record<number, AugmentedVariable>, refIdFactory: (key: string, frameId: number) => number) {
let result = await rokuECP.getRegistry(options);

if (result.status === EcpStatus.ok) {
try {
let registryData = await rokuECP.getRegistry(options);
// Add registry data to variable list
if (registryData.devId) {
v.childVariables.push(<AugmentedVariable>{
Expand Down Expand Up @@ -114,10 +113,10 @@ export async function populateVariableFromRegistryEcp(options: RokuEcpParam<'get
return sectionItemVariable;
}));
}
} else {
} catch (e) {
v.childVariables.push(<AugmentedVariable>{
name: 'error',
value: `❌ Error: ${registryData.errorMessage ?? 'Unknown error'}`,
value: `❌ Error: ${e.message ?? 'Unknown error'}`,
type: VariableType.String,
variablesReference: 0,
childVariables: []
Expand Down

0 comments on commit 50778d6

Please sign in to comment.