Skip to content

Commit

Permalink
Remove optional chaining on catch. The error is always going to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-Holbrook committed Feb 8, 2024
1 parent e2d255a commit 8d23ac9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
38 changes: 22 additions & 16 deletions src/RokuDeploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,37 +1196,38 @@ describe('index', () => {
assert.fail('Should not have succeeded');
});

it('should fail with thrown error', async () => {
it('should throw with HPE_INVALID_CONSTANT and then succeed on retry', async () => {
let doPostStub = sinon.stub(rokuDeploy as any, 'doPostRequest');
doPostStub.onFirstCall().throws((params) => {
throw new Error('Some error');
throw new ErrorWithCode();
});
doPostStub.onSecondCall().returns({ body: '..."fileType":"squashfs"...' });
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
expect(e).to.be.instanceof(Error);
return;
assert.fail('Should not have throw');
}
assert.fail('Should not have throw');
});

it('should fail with HTTP_INVALID_CONSTANT and then succeed on retry', async () => {
it('should throw and not retry', async () => {
let doPostStub = sinon.stub(rokuDeploy as any, 'doPostRequest');
doPostStub.onFirstCall().throws((params) => {
throw new HPE_INVALID_CONSTANT_ERROR();
throw new ErrorWithCode('Something else');
});
doPostStub.onSecondCall().returns({ body: '..."fileType":"squashfs"...' });
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
assert.fail('Should not have throw');
expect(e).to.be.instanceof(ErrorWithCode);
expect(e['code']).to.be.eql('Something else');
return;
}
assert.fail('Should not have throw');
});

it('should fail with HTTP_INVALID_CONSTANT and then fail on retry', async () => {
it('should throw with HPE_INVALID_CONSTANT and then fail on retry', async () => {
let doPostStub = sinon.stub(rokuDeploy as any, 'doPostRequest');
doPostStub.onFirstCall().throws((params) => {
throw new HPE_INVALID_CONSTANT_ERROR();
throw new ErrorWithCode();
});
doPostStub.onSecondCall().returns({ body: '..."fileType":"zip"...' });
try {
Expand All @@ -1238,26 +1239,31 @@ describe('index', () => {
assert.fail('Should not have throw');
});

it('should fail with HTTP_INVALID_CONSTANT and then throw on retry', async () => {
it('should fail with HPE_INVALID_CONSTANT and then throw on retry', async () => {
let doPostStub = sinon.stub(rokuDeploy as any, 'doPostRequest');
doPostStub.onFirstCall().throws((params) => {
throw new HPE_INVALID_CONSTANT_ERROR();
throw new ErrorWithCode();
});
doPostStub.onSecondCall().throws((params) => {
throw new Error('Never seen');
});
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
expect(e).to.be.instanceof(HPE_INVALID_CONSTANT_ERROR);
expect(e).to.be.instanceof(ErrorWithCode);
return;
}
assert.fail('Should not have throw');
});
});

class HPE_INVALID_CONSTANT_ERROR extends Error {
code = 'HPE_INVALID_CONSTANT';
class ErrorWithCode extends Error {
code;

constructor(code = 'HPE_INVALID_CONSTANT') {
super();
this.code = code;
}
}

describe('rekeyDevice', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/RokuDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export class RokuDeploy {
//wrong combination. The device fails to respond to our request with a valid response.
//The device successfully converted the zip, so ping the device and and check the response
//for "fileType": "squashfs" then return a happy response, otherwise throw the original error
if ((error as any)?.code === 'HPE_INVALID_CONSTANT') {
if ((error as any).code === 'HPE_INVALID_CONSTANT') {
try {
results = await this.doPostRequest(requestOptions, false);
if (/"fileType"\s*:\s*"squashfs"/.test(results.body)) {
Expand Down

0 comments on commit 8d23ac9

Please sign in to comment.