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

Retry the convertToSquahsfs request given the HPE_INVALID_CONSTANT error #145

Merged
merged 5 commits into from
Feb 26, 2024
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
69 changes: 69 additions & 0 deletions src/RokuDeploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,77 @@ describe('index', () => {
}
assert.fail('Should not have succeeded');
});

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 ErrorWithCode();
});
doPostStub.onSecondCall().returns({ body: '..."fileType":"squashfs"...' });
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
assert.fail('Should not have throw');
}
});

it('should throw and not retry', async () => {
let doPostStub = sinon.stub(rokuDeploy as any, 'doPostRequest');
doPostStub.onFirstCall().throws((params) => {
throw new ErrorWithCode('Something else');
});
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
expect(e).to.be.instanceof(ErrorWithCode);
expect(e['code']).to.be.eql('Something else');
return;
}
assert.fail('Should not have throw');
});

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 ErrorWithCode();
});
doPostStub.onSecondCall().returns({ body: '..."fileType":"zip"...' });
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
expect(e).to.be.instanceof(errors.ConvertError);
return;
}
assert.fail('Should not have throw');
});

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 ErrorWithCode();
});
doPostStub.onSecondCall().throws((params) => {
throw new Error('Never seen');
});
try {
await rokuDeploy.convertToSquashfs(options);
} catch (e) {
expect(e).to.be.instanceof(ErrorWithCode);
return;
}
assert.fail('Should not have throw');
});
});

class ErrorWithCode extends Error {
code;

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

describe('rekeyDevice', () => {
beforeEach(() => {
const body = `<device-info>
Expand Down
23 changes: 21 additions & 2 deletions src/RokuDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,27 @@ export class RokuDeploy {
archive: '',
mysubmit: 'Convert to squashfs'
});

let results = await this.doPostRequest(requestOptions);
let results;
try {
results = await this.doPostRequest(requestOptions);
} catch (error) {
//Occasionally this error is seen if the zip size and file name length at the
//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') {
try {
results = await this.doPostRequest(requestOptions, false);
if (/"fileType"\s*:\s*"squashfs"/.test(results.body)) {
return results;
}
} catch (e) {
throw error;
}
} else {
throw error;
}
}
if (results.body.indexOf('Conversion succeeded') === -1) {
throw new errors.ConvertError('Squashfs conversion failed');
}
Expand Down